Slack API invalid_block
Asked Answered
T

9

17

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"
}
Testes answered 21/2, 2020 at 19:5 Comment(1)
I've just noticed that the datepicker did work via the "send via slack" button in the block builder kit, but not when I sent it as a post request via Python ... the remaining blocks were fine.Chimene
L
6

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.

Liberticide answered 11/7, 2022 at 6:10 Comment(1)
That's what I've found out. Unfortunately, I couldn't find an official answer on Slack docs or elsewhere which is baffling.Tapes
D
5

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)
Depew answered 30/3, 2022 at 17:43 Comment(2)
Gives invalid_blocks for some reason for Example in Block Kit BuilderBeghard
How many blocks do you have? Try adding each block one by one, then you could have a clear approach of which block is making the error. LMK about your problem and if persists please attach the blocks that you're trying to add.Depew
G
4

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.

Grube answered 20/5, 2021 at 10:47 Comment(0)
T
4

I kept getting the invalid_blocks error. It turned out that the Slack API seems to have a pretty arbitrary limit of 2958 characters in the text block (using markdown). It seems like a very odd number, so maybe it's some combination of the total size of the json body. In any case if it's not working try decreasing the size of the text.

Transcribe answered 9/1 at 13:44 Comment(0)
T
2

in the api, the "blocks" parameter need to be string type. Did you convert it to string or you use it as a JSON ? https://api.slack.com/methods/chat.postMessage enter image description here

Tommy answered 24/11, 2020 at 8:35 Comment(0)
I
1

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

Ixtle answered 19/8, 2022 at 21:16 Comment(0)
B
0

Facing a similar issue,

I resolved it by removing certain sections in the payload and testing if it works.

For me the tags section in the below payload was creating a problem, even though the block-kit builder shows it without any issues.

Possibly because we need to update the value to True in python payload instead of true

Block kit url example I was testing

https://app.slack.com/block-kit-builder/T09D77D4P#%7B%22blocks%22:%5B%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Hello,%20Assistant%20to%20the%20Regional%20Manager%20Dwight!%20*Michael%20Scott*%20wants%20to%20know%20where%20you'd%20like%20to%20take%20the%20Paper%20Company%20investors%20to%20dinner%20tonight.%5Cn%5Cn%20*Please%20select%20a%20restaurant:*%22%7D%7D,%7B%22type%22:%22divider%22%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Farmhouse%20Thai%20Cuisine*%5Cn:star::star::star::star:%201528%20reviews%5Cn%20They%20do%20have%20some%20vegan%20options,%20like%20the%20roti%20and%20curry,%20plus%20they%20have%20a%20ton%20of%20salad%20stuff%20and%20noodles%20can%20be%20ordered%20without%20meat!!%20They%20have%20something%20for%20everyone%20here%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Kin%20Khao*%5Cn:star::star::star::star:%201638%20reviews%5Cn%20The%20sticky%20rice%20also%20goes%20wonderfully%20with%20the%20caramelized%20pork%20belly,%20which%20is%20absolutely%20melt-in-your-mouth%20and%20so%20soft.%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Ler%20Ros*%5Cn:star::star::star::star:%202082%20reviews%5Cn%20I%20would%20really%20recommend%20the%20%20Yum%20Koh%20Moo%20Yang%20-%20Spicy%20lime%20dressing%20and%20roasted%20quick%20marinated%20pork%20shoulder,%20basil%20leaves,%20chili%20&%20rice%20powder.%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22divider%22%7D,%7B%22type%22:%22actions%22,%22elements%22:%5B%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Farmhouse%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22%7D,%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Kin%20Khao%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22,%22url%22:%22https://google.com%22%7D,%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Ler%20Ros%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22,%22url%22:%22https://google.com%22%7D%5D%7D%5D%7D

Payload with the tags section below gives an error, this issue is python related. The code needed json.dumps to correctly format the payload to slack api as Json String, but with code changes since 2021, it only allows you to send a dictionary, which doesn't convert well to a valid Json Payload.

Incorrect payload --> with tags section creating a problem below, as it doesn't recognise True, I believe.

blocks = [
     {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
            },
            "accessory": {
                "type": "image",
                "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
                "alt_text": "alt text for image"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."
            },
            "accessory": {
                "type": "image",
                "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",
                "alt_text": "alt text for image"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the  Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."
            },
            "accessory": {
                "type": "image",
                "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",
                "alt_text": "alt text for image"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Farmhouse",
                        "emoji": True
                    },
                    "value": "click_me_123"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Kin Khao",
                        "emoji": True
                    },
                    "value": "click_me_123",
                    "url": "https://google.com"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Ler Ros",
                        "emoji": True
                    },
                    "value": "click_me_123",
                    "url": "https://google.com"
                }
            ]
        }
    ]

After removing tags section, this payload works

blocks = [
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "Hello, <lob> Team, Varanus wants you to know the Seo Anomalies Found.\n\n *Please review the Notifications below:*"
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
        },
        "accessory": {
            "type": "image",
            "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
            "alt_text": "alt text for image"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."
        },
        "accessory": {
            "type": "image",
            "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",
            "alt_text": "alt text for image"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the  Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."
        },
        "accessory": {
            "type": "image",
            "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",
            "alt_text": "alt text for image"
        }
    },
    {
        "type": "divider"
    }
]
repo = client.chat_postMessage(blocks=blocks, text="A New Notification", channel="Channel-ID")
Beghard answered 3/7, 2023 at 4:19 Comment(0)
E
0

I faced the same problem as I used type: mrkdwn and emoji: true at the same time. Beware that emoji flag can only be set with type: plain_text or else the API will throw the invalid_block error. https://api.slack.com/reference/block-kit/composition-objects#text

Exile answered 2/4 at 11:52 Comment(0)
M
0

Just for does who did the same mistake as me: The text in each block can't be empty!!!

So if you have:

blocks = [
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": ""
        }
    }
]

You will have the 'invalid_blocks' error (Including the "header" block).

Mesenchyme answered 23/5 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.