松弛API invalid_block

时间:2020-02-21 19:05:11

标签: slack-api

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

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

有什么想法为什么我运行斜线命令时失败了?是否有可能从松弛中看到更详细的错误消息?

{
  "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"
}

3 个答案:

答案 0 :(得分:0)

除非您要构建主页视图,否则我认为您不需要"type": "home"与块相同的级别。这可能会导致错误。

答案 1 :(得分:0)

在api中,“ blocks”参数必须为字符串类型。您将其转换为字符串还是将其用作JSON? https://api.slack.com/methods/chat.postMessage enter image description here

答案 2 :(得分:0)

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

import json


blocks = [{...}]

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

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