【问题标题】:Slack API invalid_blockSlack API 无效块
【发布时间】:2022-03-31 01:43:48
【问题描述】:

我正在构建一个简单的 slack 机器人,并且正在使用 checkboxes 元素。

当我在对斜杠命令的 JSON 响应中从 API 返回以下内容时,我收到错误 failed with the error "invalid_blocks",但是,当我将其放入 block-kit-builder 时,它可以正常工作(包括“发送到 slack”按钮)

任何想法为什么在我运行斜杠命令时失败 - 是否可以从 slack 中看到更详细的错误消息?

{
  "blocks": [
    {
      "elements": [
        {
          "style": "primary",
          "text": {
            "emoji": true,
            "text": "Create new TODO list",
            "type": "plain_text"
          },
          "type": "button",
          "value": "value"
        },
        {
          "style": "primary",
          "text": {
            "emoji": true,
            "text": "Help",
            "type": "plain_text"
          },
          "type": "button",
          "value": "value"
        }
      ],
      "type": "actions"
    },
    {
      "text": {
        "text": "Today",
        "type": "mrkdwn"
      },
      "type": "section"
    },
    {
      "elements": [
        {
          "initial_options": [
            {
              "text": {
                "text": "Get Into the garden",
                "type": "mrkdwn"
              },
              "value": "foo"
            }
          ],
          "options": [
            {
              "text": {
                "text": "Get Into the garden",
                "type": "mrkdwn"
              },
              "value": "foo"
            }
          ],
          "type": "checkboxes"
        },
        {
          "style": "primary",
          "text": {
            "emoji": true,
            "text": "Add new Task",
            "type": "plain_text"
          },
          "type": "button",
          "value": "value"
        }
      ],
      "type": "actions"
    }
  ],
  "type": "home"
}

【问题讨论】:

  • 我刚刚注意到日期选择器确实通过块构建器工具包中的“通过 slack 发送”按钮工作,但当我通过 Python 将其作为发布请求发送时却没有......其余块很好。

标签: slack-api


【解决方案1】:

我正在使用 Slack Web API。我遇到了类似的错误。经过大量环顾后,这就是我解决它的方法。

import json


blocks = [{...}]

payload = {
    "blocks": json.dumps(blocks)
}

然后您将发送此有效负载。

【讨论】:

    【解决方案2】:

    在 Block Kit Builder 中,数据是带有 blocks 键的 JSON。 在 Slack API 中,blocks 参数只是JSON 对象中的list

    blocks = [
        {
            "text": {
                "text": "Its the list of your blocks",
                "type": "mrkdwn"
            },
            "type": "section"
        }
    ]
    text = 'Alternative data in text'
    client.chat_postMessage(channel=channel_id, blocks=blocks, text=text)
    

    【讨论】:

      【解决方案3】:

      在api中,“blocks”参数需要是字符串类型。您是否将其转换为字符串或将其用作 JSON ? https://api.slack.com/methods/chat.postMessage

      【讨论】:

        猜你喜欢
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-10
        相关资源
        最近更新 更多