繁体   English   中英

我无法使用python松弛API发送布局块

[英]I can't send layout blocks using python slack API

我使用slack的最新bot工具包构建器为我的邮件附件生成了以下按钮。 邮件已发送,没有任何问题,但是我看不到附件。 我一直在努力解决这个问题,并感谢您的帮助。 我的附件是Slack API所需的列表。

attachment_json = [
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "Hello, Please select your environment"
        }
    },
    {
        "type": "actions",
        "elements": [
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "production"
                },
                "value": "production"
            },
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "staging"
                },
                "value": "staging"
            },
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "demo"
                },
                "value": "demo"
            }
        ]
    }
]

我正在使用slackclient SDK发送消息。

  slack_client.api_call("chat.postMessage", channel="D4KU1DGUB", text='Hello World',
                          attachments=json.dumps(attachment_json))

目前,我没有看到任何按钮。 任何帮助将不胜感激。 谢谢。

即使从示例发送此附件也不起作用

{
    "text": "Would you like to play a game?",
    "attachments": [
        {
            "text": "Choose a game to play",
            "fallback": "You are unable to choose a game",
            "callback_id": "wopr_game",
            "color": "#3AA3E3",
            "attachment_type": "default",
            "actions": [
                {
                    "name": "game",
                    "text": "Chess",
                    "type": "button",
                    "value": "chess"
                },
                {
                    "name": "game",
                    "text": "Falken's Maze",
                    "type": "button",
                    "value": "maze"
                },
                {
                    "name": "game",
                    "text": "Thermonuclear War",
                    "style": "danger",
                    "type": "button",
                    "value": "war",
                    "confirm": {
                        "title": "Are you sure?",
                        "text": "Wouldn't you prefer a good game of chess?",
                        "ok_text": "Yes",
                        "dismiss_text": "No"
                    }
                }
            ]
        }
    ]
}

这个附件对我有用

   attachment_json = [
    {
        "fallback": "Upgrade your Slack client to use messages like these.",
        "color": "#CC0000",
        "actions": [
            {
                "type": "button",
                "text": ":red_circle:   Complete Task: ",
                "url": "https://roach.ngrok.io/workflow/",
            }
        ]
    }
    ]

但是我不知道为什么第一个例子不起作用。

第一个示例不起作用的原因是您在方法调用中混合了附件和布局块的语法。 这些是不同的功能,因此在chat.postMessage的调用中每个都有其自己的参数。

  1. (第二个)附件与attachments参数一起传递。
  2. 布局块与blocks参数一起传递。

因此,第一个示例中正确的代码版本为:

slack_client.api_call("chat.postMessage", channel="D4KU1DGUB", text='Hello World',
                          blocks=json.dumps(attachment_json))

您可能还希望将变量重命名为blocks_json以避免造成混淆。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM