Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Slack's Interactive Message request payload parameter?

I would like to access the payload parameter sent by Slack to Interactive Message Request Url when a user clicks on a button in one of these messages.

How do I do this?

like image 235
ypicard Avatar asked Apr 20 '17 07:04

ypicard


People also ask

What is slack payload?

All of the Slack APIs that publish messages use a common base structure, called a message payload. This is a JSON object that is used to define metadata about the message, such as where it should be published, as well as its visual composition.

What is message payload?

In computing and telecommunications, the payload is the part of transmitted data that is the actual intended message. Headers and metadata are sent only to enable payload delivery. In the context of a computer virus or worm, the payload is the portion of the malware which performs malicious action.

Can slack BOT send direct message to user?

Bot users can't post to a direct message conversation between two users using chat. postMessage . If your app was involved in the conversation, then it would be a multi-person direct message instead.

What is an interactive message?

Interactive messages are much like other messages, only they contain buttons, a variety of menus types, or they have some custom actions available. Rather than remaining mostly static, interactive messages evolve over time. Message buttons and menus may travel almost anywhere a message goes.


2 Answers

I managed getting the info this way:

data = request.form.to_dict()
payload = json.loads(data['payload'])
print(payload["actions"][0]["name"])

Hope it helps someone in the future.

The first line converts the ImmutableDict to a mutable dictionary.

The second line is necessary because the payload is still JSON.

The third line is simply accessing the payload action details as seen in the Block Link Builder, often most people will only have one relevant action to process.

like image 128
Jose Gutierrez Avatar answered Oct 13 '22 22:10

Jose Gutierrez


On your server side, check your request url route is allowed to receive POST. As said in theirs docs (https://api.slack.com/docs/message-buttons) :

Your Action URL will receive a HTTP POST request, including a payload body parameter, itself containing an application/x-www-form-urlencoded JSON string.

You first have to decode the x-www-form-urlencoded format of the request, then json decode it.

In python, I end up with this line of code :

payload = json.loads(urlparse.parse_qs(request.get_data())['payload'][0])

Hope it helps someone else one day !

like image 31
ypicard Avatar answered Oct 13 '22 21:10

ypicard