如何通过 slackclient 将图像作为 slack bot 发送到 Slack?
How to send Image to Slack as slack bot via slackclient?
我正在构建一个简单的机器人来定期生成图表作为图像,并 post 通过 python slackclient 包进行松弛。
我已经成功发送了一条带有以下代码的机器人消息:
def post_message_to_channel(self, channel_id, message=DEFAULT_TEST_MESSAGE):
sc = SlackClient(TOKEN)
sc.api_call(
"chat.postMessage",
channel=channel_id,
username='mybot',
text='test text message'
)
但是,当我尝试对文件上传执行相同操作时,图像已正确发送,但显示的是我的名字,而不是指定的机器人名称:
def post_image_to_channel(self, channel_name, filepath, tile='Test Upload'):
sc = SlackClient(TOKEN)
with open(filepath, 'rb') as file_content:
sc.api_call(
"files.upload",
channels=channel_id,
file=file_content,
title=tile,
username='mybot',
)
查看 files.upload, 的 slack API 文档,似乎 username
不可用。
如何使用机器人名称发送 files.upload 图片?
您必须使用机器人用户的机器人用户令牌发送文件。您可以在官方文档中找到更多信息 https://api.slack.com/bot-users
另一种方法是将您的图像作为 message attachments 的一部分发送。这样您还可以在图表周围包含一些上下文信息。由于您正在发送消息,因此您仍然可以完全控制用户名等。
import slack
import json
import os
def pureimg(data1):
data1 = '[{"text": "", "image_url": "'+data1+'"}]'
data1 = [json.loads(data1[1:-1])]
return data1
#This function will make the image url to correct format.
slacker = slack.WebClient(token='your-token-here')
payoff=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'filename.png')
#It gives cross OS compatibility on filepath.
response=slacker.files_upload(channel='#theta',file=payoff)
payoff=response['file']['permalink']
#First We upload the local file to Slack and fetch permalink.
#If you do not have any local file just put the external image URL in the payoff.
response=slacker.chat_postMessage(channel='#channel_name', text="Sample Text", username='Bot name', attachments=pureimg(payoff), icon_emoji=':emoji:')
#Then, We post to Slack Channel as a bot!
我正在构建一个简单的机器人来定期生成图表作为图像,并 post 通过 python slackclient 包进行松弛。
我已经成功发送了一条带有以下代码的机器人消息:
def post_message_to_channel(self, channel_id, message=DEFAULT_TEST_MESSAGE):
sc = SlackClient(TOKEN)
sc.api_call(
"chat.postMessage",
channel=channel_id,
username='mybot',
text='test text message'
)
但是,当我尝试对文件上传执行相同操作时,图像已正确发送,但显示的是我的名字,而不是指定的机器人名称:
def post_image_to_channel(self, channel_name, filepath, tile='Test Upload'):
sc = SlackClient(TOKEN)
with open(filepath, 'rb') as file_content:
sc.api_call(
"files.upload",
channels=channel_id,
file=file_content,
title=tile,
username='mybot',
)
查看 files.upload, 的 slack API 文档,似乎 username
不可用。
如何使用机器人名称发送 files.upload 图片?
您必须使用机器人用户的机器人用户令牌发送文件。您可以在官方文档中找到更多信息 https://api.slack.com/bot-users
另一种方法是将您的图像作为 message attachments 的一部分发送。这样您还可以在图表周围包含一些上下文信息。由于您正在发送消息,因此您仍然可以完全控制用户名等。
import slack
import json
import os
def pureimg(data1):
data1 = '[{"text": "", "image_url": "'+data1+'"}]'
data1 = [json.loads(data1[1:-1])]
return data1
#This function will make the image url to correct format.
slacker = slack.WebClient(token='your-token-here')
payoff=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'filename.png')
#It gives cross OS compatibility on filepath.
response=slacker.files_upload(channel='#theta',file=payoff)
payoff=response['file']['permalink']
#First We upload the local file to Slack and fetch permalink.
#If you do not have any local file just put the external image URL in the payoff.
response=slacker.chat_postMessage(channel='#channel_name', text="Sample Text", username='Bot name', attachments=pureimg(payoff), icon_emoji=':emoji:')
#Then, We post to Slack Channel as a bot!