Slack Bot互动消息

时间:2016-10-02 14:22:44

标签: python django bots slack-api

我是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

为什么会发生这种情况?

由于

1 个答案:

答案 0 :(得分:0)

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

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)

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

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