Is it possible to post a Slack message with markdown/blocks to a webhook without an app?

592 views Asked by At

I am trying to develop a notification routine in python to post a formatted message into a channel. I can post plain text with no problem but markdown or blocks either give me missing required field errors or block format errors. If I create an app (on a free slack account) with a webhook it post easily. Can you only send blocks via an app. The docs say that is is possible to send via a webhook but I cannot make it work.

I am not using chat.post_message as it required an app to be created and my organization makes it VERY difficult to get an app approved. So I'm just using a workflow with a webhook in a channel.

Simple example:

import requests
import json

slack_channel = 'https://hooks.slack.com/workflows/my/slack/hook'
blocks = [
    {
        "type": "section",
        "text": {
                "type": "mrkdwn",
                "text": "testing"
        }
    }
]

slack_data = json.dumps(blocks)
req = requests.Session()
response = req.post(slack_channel,
                    data=slack_data,
                    headers={"Content-Type": "application/json;utf-8"}
                    ).json()
print(response)

The response error:

400 {"error":"invalid_post_body","ok":false,"response_metadata":{"messages":[]}}```

I can post fine with a plain text message. Is formatting only supported if I have an app? What am I missing?
1

There are 1 answers

0
Jason On

It is possible to send in blocks via a webhook, the data portion is defined as so:

POST https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Content-type: application/json
{
    "text": "Danny Torrence left a 1 star review for your property.",
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Danny Torrence left the following review for your property"
            }
        }
    ]
}

More information on this can be found here: https://api.slack.com/messaging/webhooks#advanced_message_formatting