根据 Slack 的文档,每次只能通过 API 发送一个文件。方法是这样的:https://api.slack.com/methods/files.upload.
使用 Slack 的桌面和 Web 应用程序,我们可以一次发送多个文件,这很有用,因为文件被分组,当我们有多个具有相同上下文的图像时,有助于可视化。请参阅下面的示例:
你们知道是否可以通过 API 一次发送多个文件或以某种方式达到与上图相同的结果?
提前致谢!
我也遇到过同样的问题。但我尝试用多个 pdf 文件撰写一封邮件。
我如何解决这个任务
channel
参数(这会阻止发布)并从响应中收集永久链接。请检查文件对象引用。 https://api.slack.com/types/file。通过“files.upload”方法,您可以上传仅一个文件。因此,您需要根据要上传的文件多次调用此方法。 使用 Slack 撰写消息
<{permalink1_from_first_step}| ><{permalink2_from_first_step}| >
- Slack 解析链接并自动重新格式化消息
def post_message_with_files(message, file_list, channel):
import slack_sdk
SLACK_TOKEN = "slackTokenHere"
client = slack_sdk.WebClient(token=SLACK_TOKEN)
for file in file_list:
upload = client.files_upload(file=file, filename=file)
message = message + "<" + upload["file"]["permalink"] + "| >"
out_p = client.chat_postMessage(channel=channel, text=message)
post_message_with_files(
message="Here is my message",
file_list=["1.jpg", "1-Copy1.jpg"],
channel="myFavoriteChannel",
)
client.files_upload_v2
(于2022年12月21日在slack-sdk-3.19.5上测试):
import slack_sdk
def slack_msg_with_files(message, file_uploads_data, channel):
client = slack_sdk.WebClient(token='your_slack_bot_token_here')
upload = client.files_upload_v2(
file_uploads=file_uploads_data,
channel=channel,
initial_comment=message,
)
print("Result of Slack send:\n%s" % upload)
file_uploads = [
{
"file": path_to_file1,
"title": "My File 1",
},
{
"file": path_to_file2,
"title": "My File 2",
},
]
slack_msg_with_files(
message='Text to add to a slack message along with the files',
file_uploads_data=file_uploads,
channel=SLACK_CHANNEL_ID # can be found in Channel settings in Slack. For some reason the channel names don't work with `files_upload_v2` on slack-sdk-3.19.5
)
(一些额外的错误处理不会有什么坏处)
import pkg from '@slack/bolt';
const { App } = pkg;
import axios from 'axios'
// In Bolt, you can get channel ID in the callback from the `body` argument
const channelID = 'C000000'
// Sample Data - URLs of images to post in a gallery view
const imageURLs = ['https://source.unsplash.com/random', 'https://source.unsplash.com/random']
const uploadFile = async (fileURL) {
const image = await axios.get(fileURL, { responseType: 'arraybuffer' });
return await app.client.files.upload({
file: image.data
// Do not use "channels" here for image gallery view to work
})
}
const permalinks = await Promise.all(imageURLs.map(async (imageURL) => {
return (await uploadImage(imageURL)).file.permalink
}))
const images = permalinks.map((permalink) => `<${permalink}| >`).join('')
const message = `Check out the images below: ${images}`
// Post message with images in a "gallery" view
// In Bolt, this is the same as doing `await say({`
// If you use say(, you don't need a channel param.
await app.client.chat.postMessage({
text: message,
channel: channelID,
// Do not use blocks here for image gallery view to work
})
上面的示例包含一些附加功能 - 从图像 URL 列表下载图像,然后将这些图像上传到 Slack。请注意,这是未经测试的,我修剪了我正在使用的功能齐全的代码。
Slack 的
image.upload
API 文档会提到文件格式需要采用
multipart/form-data
。不用担心这部分,Bolt(通过 Slack 的 Node API)将自动处理该转换(如果您向其提供 FormData,甚至可能无法工作)。它接受 arraybuffer
(此处使用)、stream
以及可能的其他格式的文件数据。如果您要上传本地文件,请考虑将 fs
读取流传递到 Slack 上传功能。
permalink_list = []
file_list=['file1.csv', 'file2.csv', 'file3.csv']
for file in file_list:
response = client.files_upload(file=file)
permalink = response['file']['permalink']
permalink_list.append(permalink)
text = ""
for permalink in permalink_list:
text_single_link = "<{}| >".format(permalink)
text = text + text_single_link
response = client.chat_postMessage(channel=channelid, text=text)
import slack_sdk
client = slack_sdk.WebClient(token=SLACK_TOKEN)
file_uploads = []
for file in list_of_files:
file_uploads.append({
'file' : file['path'],
'filename' : file['name'],
'title' : file['title']
})
new_file = client.files_upload_v2(
file_uploads=file_uploads,
channel=message['channel'],
initial_comment=text_message
)