Search code examples
pythonpython-3.xslackslack-apislack-commands

I can't send layout blocks using python slack API


I used the latest bot kit builder by slack to generate the following buttons for my message attachment. The message is getting sent without any problem and but I don't see the attachment. I have been trying to figure this out and appreciate some help. My attachment is a list as required by 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"
            }
        ]
    }
]

I am using slackclient SDK to send the message.

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

Currently I am not seeing any buttons. Any help will be appreciated. Thank you.

Even sending this attachment from the example is not working

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

This attachment is working for me

   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/",
            }
        ]
    }
    ]

But I am can't figure out why the first example is not working.


Solution

  • The reason the first example is not working is that you are mixing the syntax for attachments and layout blocks in your method call. Those are different features and therefore each have their own parameter in the call to chat.postMessage.

    1. (Secondary) attachments are passed with the attachments parameter.
    2. Layout blocks are passed with the blocks parameter.

    So the corrected version of your code from the first example would be:

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

    You may also want to rename the variable to blocks_json to avoid any confusion.