【问题标题】:I can't send layout blocks using python slack API我无法使用 python slack API 发送布局块
【发布时间】:2019-08-01 05:07:13
【问题描述】:

我使用最新的bot kit builder by slack 为我的邮件附件生成以下按钮。邮件发送没有任何问题,但我没有看到附件。我一直在努力解决这个问题并感谢一些帮助。我的附件是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/",
            }
        ]
    }
    ]

但我无法弄清楚为什么第一个示例不起作用。

【问题讨论】:

  • 我已经更新了我的问题。即使是有效的 JSON 附件也没有使用 API 方法发送。
  • 使用curl是否有效?
  • 更新了@Selcuk 的问题。我添加了一个适合我的示例。

标签: python python-3.x slack slack-api slack-commands


【解决方案1】:

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

  1. (次要)附件使用attachments 参数传递。
  2. 布局块使用blocks 参数传递。

因此,第一个示例中代码的更正版本将是:

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

您可能还想将变量重命名为 blocks_json 以避免任何混淆。

【讨论】:

  • 你拯救了我的一天。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
  • 1970-01-01
  • 2022-11-11
  • 2021-05-10
  • 2023-01-04
  • 2021-11-08
相关资源
最近更新 更多