简体   繁体   中英

Slack interactive message: POST response is not in json format

@csrf_exempt
def slack(request):
print("Testing slack")
if request.method == 'POST':
    print('request', str(request.body))
    webhook_url = 'xxxxxxxx'
    text = "Would you recommend it to customers?"
    request = unquote(unquote(request.body.decode(encoding='ascii')))
    print('url', request)
    slack_data = {
        "attachments": [
            {
                "fallback": "Would you recommend it to customers?",
                "title": request,
                "callback_id": "comic_1234_xyz",
                "color": "#3AA3E3",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "recommend",
                        "text": "Recommend",
                        "type": "button",
                        "value": "recommended"
                         }
                ],
            }
        ]
    }
    test = slack_data
    print('slack_data', type(slack_data))
    response = requests.post(
        webhook_url, data=json.dumps(test),
        headers={'Content-Type': 'application/json'}
    )
return HttpResponse("New comic book alert!")

In this str(request.body) I am getting output like: b'payload=%7B%22type%22%3A%22 interactive message%22%2C%

So I encoded it using unquote(unquote(request.body.decode(encoding='ascii'))) and I am able to get the payload in this format:

payload={ "here I got all details of POST message" }

How do I parse this in Json ?

There's no need to get request.body in the first place. It looks like you are posting standard form data, with a payload field which contains the JSON data. So just get that:

data = json.loads(request.POST['payload'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM