ypicard
ypicard

Reputation: 3693

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?

Upvotes: 13

Views: 3836

Answers (3)

Jose Gutierrez
Jose Gutierrez

Reputation: 41

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.

Upvotes: 4

Rahul Mishra
Rahul Mishra

Reputation: 621

If you are using AWS lamdba as backend, use following. (python3)

import json
from urllib.parse import parse_qs

payload = json.loads(parse_qs(event['body'])['payload'][0])

Upvotes: 4

ypicard
ypicard

Reputation: 3693

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 !

Upvotes: 11

Related Questions