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?
It is possible to send in
blocks
via a webhook, thedata
portion is defined as so:More information on this can be found here: https://api.slack.com/messaging/webhooks#advanced_message_formatting