Slack API invalid_block

Viewed 12180

I'm building a simple slack bot and I am playing with the checkboxes element.

When I return the following from my API in a JSON response to a slash-command I get an error failed with the error "invalid_blocks", however, when I put this in the block-kit-builder it works perfectly (including "sending to slack" button)

Any ideas why this is failing when I run my slash command - and is it possible to see more detailed error messages from 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"
}
5 Answers

I am using the Slack Web API. I was getting the similar error. After a lot of looking around, here's how I solved it.

import json


blocks = [{...}]

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

You will then send this payload.

In the Block Kit Builder, the data is a JSON with a blocks key. In the Slack API, the blocks param is only the list of JSON objects.

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)

Another cause of this problem seems to be too many blocks being returned. I can't find any documentation about this whatsoever, but personal experience seems to indicate about 20 blocks is the maximum.

An alternative is to return fewer blocks, with paging actions -- paging works well with the "replace" message so that the content being paged through does not result in many separate messages.

It appears that not all valid elements in block kit tool can be posted as a message, despite the fact that message preview works fine in the Block Tool.

In my case, the code failed when I included an input block and passed when i removed it. The input block was generated by the Block Kit tool.

        {
            "type": "input",
            "element": {
                "type": "plain_text_input",
                "action_id": "plain_text_input-action"
            },
            "label": {
                "type": "plain_text",
                "text": "Feedback",
                "emoji": true
            }
        }

The error was {'ok': False, 'error': 'invalid_blocks'}

Also, although the documentation for python says you need to urlEncode the JSON-based array, there is no example, and it is incorrect. https://api.slack.com/methods/chat.postMessage

You can see on line 29 in the SDK test code below that blocks= takes a regular list of dicts not a string. https://github.com/slackapi/python-slack-sdk/blob/c9dc6aa0907a72c16cf36aa15e7e80031a9fdce2/integration_tests/samples/basic_usage/sending_a_message.py

Related