使用 Slack WebClient 发送包含上传图像的消息
我正在尝试使用 Slack Web 客户端将消息从机器人发送到私人频道。该消息将包括一些文本和图像。阅读当前的 Slack 文档后,似乎实现此目的的最佳方法是使用 file.upload 方法将文件上传到 Slack,然后使用 chat.PostMessage< /code> 方法将包含 URL 的消息发送到托管图像。虽然我似乎能够上传文件,但当我发送消息时,我收到有关我上传的文件的错误。我不确定我是否传递了错误的 URL,或者上传图像后是否需要执行其他操作。我能够在没有文件的情况下成功发送消息,因此我知道问题与图像具体有关。
Error: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'invalid_blocks', 'errors': ['downloading image failed [json-pointer:/blocks/1/image_url]'], 'response_metadata': {'messages': ['[ERROR] downloading image failed [json-pointer:/blocks/1/image_url]']}}
以下是我用来创建 Web 客户端、上传文件,然后发送消息的过程。
import os
import requests
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from pprint import pprint
# create Slack web client
client = WebClient(token="xoxb-123456789")
# find the IDs of the Slack channels
for result in client.conversations_list():
for channel in result["channels"]:
if channel['name'] == 'my_channel':
channel_id = channel['id']
break
# upload image to my Slack channel
image = client.files_upload(
channel = channel_id,
initial_comment = "This is my image",
file = "~/image.png"
)
# write my message
block = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Guess what? I don't know"
}
},
{
"type": "image",
"image_url": image['file']['permalink'],
"alt_text": "inspiration"
}
]
# try to send message with image
try:
result = client.chat_postMessage(
channel = channel_id,
text = "New message for you",
blocks = block
)
except SlackApiError as e:
print(f"Error: {e}")
此时,我遇到以下错误消息:
Error: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'invalid_blocks', 'errors': ['downloading image failed [json-pointer:/blocks/1/image_url]'], 'response_metadata': {'messages': ['[ERROR] downloading image failed [json-pointer:/blocks/1/image_url]']}}
出于故障排除的目的,这里是我取回的数据
# print the details about the file uploaded
pprint(image['file'])
{'channels': [],
'comments_count': 0,
'created': 1648070852,
'display_as_bot': False,
'editable': False,
'external_type': '',
'filetype': 'png',
'groups': [],
'has_rich_preview': False,
'id': 'FHBB87462378',
'ims': [],
'is_external': False,
'is_public': False,
'is_starred': False,
'media_display_type': 'unknown',
'mimetype': 'image/png',
'mode': 'hosted',
'name': 'image.png',
'original_h': 1004,
'original_w': 1790,
'permalink': 'https://sandbox.enterprise.slack.com/files/123456789/ABC/image.png',
'permalink_public': 'https://slack-files.com/123456789',
'pretty_type': 'PNG',
'public_url_shared': False,
'shares': {},
'size': 1623063,
'thumb_1024': 'https://files.slack.com/files-tmb/123456789/image_1024.png',
'thumb_1024_h': 574,
'thumb_1024_w': 1024,
'thumb_160': 'https://files.slack.com/files-tmb/123456789/image_160.png',
'thumb_360': 'https://files.slack.com/files-tmb/123456789/image_360.png',
'thumb_360_h': 202,
'thumb_360_w': 360,
'thumb_480': 'https://files.slack.com/files-tmb/123456789/image_480.png',
'thumb_480_h': 269,
'thumb_480_w': 480,
'thumb_64': 'https://files.slack.com/files-tmb/123456789/image_64.png',
'thumb_720': 'https://files.slack.com/files-tmb/123456789/image_720.png',
'thumb_720_h': 404,
'thumb_720_w': 720,
'thumb_80': 'https://files.slack.com/files-tmb/123456789/image_80.png',
'thumb_800': 'https://files.slack.com/files-tmb/123456789/image_800.png',
'thumb_800_h': 449,
'thumb_800_w': 800,
'thumb_960': 'https://files.slack.com/files-tmb/123456789/image_960.png',
'thumb_960_h': 538,
'thumb_960_w': 960,
'thumb_tiny': 'AoinfgvoindwoidnasQOJWQNWOIQONQqoinoiQQ/2Q==',
'timestamp': 1648070852,
'title': 'image',
'url_private': 'https://files.slack.com/files-pri/123456789/image.png',
'url_private_download': 'https://files.slack.com/files-pri/123456789/download/image.png',
'user': 'U123456789',
'username': ''}
I'm trying to use the Slack Web Client to send a message from a bot to a private channel. The message would include some text and an image. After reading the current Slack documentation, it seems like the best way to accomplish this would be to use the file.upload
method to upload the file to Slack, and then use the chat.PostMessage
method to send the message including a URL to the hosted image. While it seems that I'm able to upload the file, when I go to send the message, I get an error regarding the file that I've uploaded. I'm not sure if I'm passing the wrong URL or if there is something else that I need to do after uploading the image. I'm able to successfully send a message without a file, so I know the issue has to do with the image specifically.
Error: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'invalid_blocks', 'errors': ['downloading image failed [json-pointer:/blocks/1/image_url]'], 'response_metadata': {'messages': ['[ERROR] downloading image failed [json-pointer:/blocks/1/image_url]']}}
Below is the process that I'm using to create the web client, upload the file, then send the message.
import os
import requests
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from pprint import pprint
# create Slack web client
client = WebClient(token="xoxb-123456789")
# find the IDs of the Slack channels
for result in client.conversations_list():
for channel in result["channels"]:
if channel['name'] == 'my_channel':
channel_id = channel['id']
break
# upload image to my Slack channel
image = client.files_upload(
channel = channel_id,
initial_comment = "This is my image",
file = "~/image.png"
)
# write my message
block = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Guess what? I don't know"
}
},
{
"type": "image",
"image_url": image['file']['permalink'],
"alt_text": "inspiration"
}
]
# try to send message with image
try:
result = client.chat_postMessage(
channel = channel_id,
text = "New message for you",
blocks = block
)
except SlackApiError as e:
print(f"Error: {e}")
At this point, I experience the following error message:
Error: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'invalid_blocks', 'errors': ['downloading image failed [json-pointer:/blocks/1/image_url]'], 'response_metadata': {'messages': ['[ERROR] downloading image failed [json-pointer:/blocks/1/image_url]']}}
For the purpose of troubleshoot, here is the data that I get back
# print the details about the file uploaded
pprint(image['file'])
{'channels': [],
'comments_count': 0,
'created': 1648070852,
'display_as_bot': False,
'editable': False,
'external_type': '',
'filetype': 'png',
'groups': [],
'has_rich_preview': False,
'id': 'FHBB87462378',
'ims': [],
'is_external': False,
'is_public': False,
'is_starred': False,
'media_display_type': 'unknown',
'mimetype': 'image/png',
'mode': 'hosted',
'name': 'image.png',
'original_h': 1004,
'original_w': 1790,
'permalink': 'https://sandbox.enterprise.slack.com/files/123456789/ABC/image.png',
'permalink_public': 'https://slack-files.com/123456789',
'pretty_type': 'PNG',
'public_url_shared': False,
'shares': {},
'size': 1623063,
'thumb_1024': 'https://files.slack.com/files-tmb/123456789/image_1024.png',
'thumb_1024_h': 574,
'thumb_1024_w': 1024,
'thumb_160': 'https://files.slack.com/files-tmb/123456789/image_160.png',
'thumb_360': 'https://files.slack.com/files-tmb/123456789/image_360.png',
'thumb_360_h': 202,
'thumb_360_w': 360,
'thumb_480': 'https://files.slack.com/files-tmb/123456789/image_480.png',
'thumb_480_h': 269,
'thumb_480_w': 480,
'thumb_64': 'https://files.slack.com/files-tmb/123456789/image_64.png',
'thumb_720': 'https://files.slack.com/files-tmb/123456789/image_720.png',
'thumb_720_h': 404,
'thumb_720_w': 720,
'thumb_80': 'https://files.slack.com/files-tmb/123456789/image_80.png',
'thumb_800': 'https://files.slack.com/files-tmb/123456789/image_800.png',
'thumb_800_h': 449,
'thumb_800_w': 800,
'thumb_960': 'https://files.slack.com/files-tmb/123456789/image_960.png',
'thumb_960_h': 538,
'thumb_960_w': 960,
'thumb_tiny': 'AoinfgvoindwoidnasQOJWQNWOIQONQqoinoiQQ/2Q==',
'timestamp': 1648070852,
'title': 'image',
'url_private': 'https://files.slack.com/files-pri/123456789/image.png',
'url_private_download': 'https://files.slack.com/files-pri/123456789/download/image.png',
'user': 'U123456789',
'username': ''}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现除了块之外,您还需要拥有顶级文本属性。下面的示例按预期工作,现在我可以将图像上传到 Slack 并将该图像包含在消息中。
请参阅 https://github.com/slackapi/python-slack-sdk/issues /1194 了解更多信息。
I found out that you need to have the top-level text property in addition to the blocks. The example below works as expected and now I'm able upload an image to Slack and the include that image in a message.
See https://github.com/slackapi/python-slack-sdk/issues/1194 for more info.
我不确定为什么您没有收到
KeyError
,但看起来您正在使用response["permalink_public"]
而不是response["file"][ “permalink_public”]
。请参阅 Slack API:https://api.slack.com/methods/files.uploadI am not sure why you do not get a
KeyError
, but looks like you are usingresponse["permalink_public"]
instead ofresponse["file"]["permalink_public"]
. See Slack API: https://api.slack.com/methods/files.upload