【问题标题】:Slack Bot Interactive MessagesSlack Bot 交互式消息
【发布时间】:2016-10-02 14:22:44
【问题描述】:

我是 Slack Bot 集成的新手。我想在我的消息上有按钮,所以我的代码是

message = {
        "text": "Would you like to play a game?",
        "attachments": [
            {
                "text": "Choose a game to play",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "chess",
                        "text": "Chess",
                        "type": "button",
                        "value": "chess"
                    }
                ]
            }
        ]
    }


return sc.api_call("chat.postMessage",
                    as_user="true",
                    channel=channel_id,
                    text=message)

但在 Slack 频道我看到了这个

text=Would+you+like+to+play+a+game%3F&attachments=%5B%7B%27text%27%3A+%27Choose+a+game+to+play%27%2C+%27attachment_type%27%3A+%27default%27%2C+%27actions%27%3A+%5B%7B%27text%27%3A+%27Chess%27%2C+%27type%27%3A+%27button%27%2C+%27name%27%3A+%27chess%27%2C+%27value%27%3A+%27chess%27%7D%5D%7D%5

为什么会这样??

谢谢

【问题讨论】:

  • 这看起来可能与您正在使用的 slack API 包装器有关。您是否尝试过使用带有requests 的HTTP 调用手动执行此操作?你也在格式化消息looks fine.
  • 请记住,chat.postMessage 不接受 JSON 帖子正文。您需要将消息作为x-www-form-urlencoded 参数发送。此外,attachments 字段实际上需要 JSON,但它必须是 URL 编码的 JSON。像这样的纯 JSON 消息体不适用于 Web API。

标签: python django bots slack-api


【解决方案1】:

我自己没有测试过,但我认为你可以尝试做这样的事情:

message = "Would you like to play a game?"
attachments = [{
                "text": "Choose a game to play",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "chess",
                        "text": "Chess",
                        "type": "button",
                        "value": "chess"
                    }
                ]}]

sc.api_call("chat.postMessage", 
             channel=channel_id, 
             text=message,
             as_user="true",
             attachments=attachments)

如果它不起作用,我会尝试在附件中包含一个“标题”字段,如下所示:

attachments = [{
                    "title": "new title",
                    ...

【讨论】:

  • 一个愚蠢的问题要问 - 在哪里为 Slack 机器人添加这个脚本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多