如何通过 slackclient 将图像作为 slack bot 发送到 Slack?

mon*_*kut 5 python slack-api slack

我正在构建一个简单的机器人,定期生成图表作为图像,并通过 python slackclient包发布到 slack。

我已经成功使用以下代码发送了一条机器人消息:

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'
    )
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试对文件上传执行相同操作时,图像已正确发送,但它显示我的名字,而不是指定的机器人名称:

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',               
            )
Run Code Online (Sandbox Code Playgroud)

查看files.upload 的 slack API 文档,似乎username不可用。

如何使用机器人名称发送files.upload图像?

小智 4

您必须使用机器人用户的机器人用户令牌发送文件。您可以在官方文档中找到更多信息https://api.slack.com/bot-users