使用slack API发布图像

2024-05-15 17:42:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图发布一个带有松弛API的图像。我目前有两个api是开放的(使用python),即rtm&slackbot。

sc = SlackClient(API_KEY)
sc.api_call('chat.postMessages', channel=, text=) #post as bot

sc.rtm_connect():
sc.rtm_send_message(channel=..., message=...) #post as desired user

这些示例可以很好地将文本发布到频道,但我需要将图像发布到频道。

我想用rtm方法发布一个图片,但是我不知道怎么做。我看到的对图像的唯一引用是https://api.slack.com/docs/attachments,但是我看不到使用rtm API的方法。

如有任何帮助,我们将不胜感激。蒂亚。

更新/编辑

所以我尝试使用推荐的方法Jon,即:

sc.api_call("chat.postMessage", channel='D0K7P9MCJ', text='postMessage test',
            attachments='[{"image_url":"http://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"}]')

但这似乎不起作用(没有发布图像),也没有抛出错误。

解决方案

我是通过在附件部分加上一个标题来完成这项工作的。希望它抛出一个错误,=/


Tags: 方法text图像comapimessageaschat
2条回答

如问题中的更新所述,要使此功能正常工作,必须在附件中包含标题。

image_url = "http://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"
attachments = [{"title": "Cat", "image_url": image_url}]
sc.api_call("chat.postMessage", channel='D0K7P9MCJ', text='postMessage test',
            attachments=attachments)

您必须使用postMessage选项——不能使用RTM(fromhttps://api.slack.com/rtm)来执行此操作:

The RTM API only supports posting simple messages formatted using our default message formatting mode. It does not support attachments or other message formatting modes. To post a more complex message as a user clients can call the chat.postMessage Web API method with as_user set to true.

相关问题 更多 >