Slack event API response using python flask

Viewed 2305

I am working with slack event API. I am getting events on the subscribed events. But how to send respond using python requests. slack sends the same event back again after few seconds what json I need to send back to slack as a response to stop getting the same response? If you know the code, thank you so much in advance :)

@flask.route("/slack_webhook")
def slack_webhook():
    print("Slack Webhook.....!!!")

    data = json.loads(request.data.decode("utf-8"))
    if 'challenge' in data:
    return(data['challenge'])

    if data['type'] == 'event_callback':
        response = make_response("", 200)
        response.headers['X-Slack-No-Retry'] = 1
        print("returning response")
        return response

    else:
        slack_event_handler.delay(data)
4 Answers

All you need to do is to respond to the request from Slack directly with a HTTP 200 OK within 3 seconds. This will happen automatically if your app terminates within that time.

In case you need more processing time you should look into queuing the event for later processing or starting an asynchronous process for the processing.

Here is what is says in the documentation:

Your app should respond to the event request with an HTTP 2xx within three seconds. If it does not, we'll consider the event delivery attempt failed. After a failure, we'll retry three times, backing off exponentially.

Maintain a response success rate of at least 5% of events per 60 minutes to prevent automatic disabling.

Respond to events with a HTTP 200 OK as soon as you can. Avoid actually processing and reacting to events within the same process. Implement a queue to handle inbound events after they are received.

directly return the status if successfully received required information.

return 'HTTP 200 OK'

Try this:

return {"isBase64Encoded": True, "statusCode": 200, "headers": { }, "body": ""}

I've found this old topic which didn't provide a proper solution to my problem. With the trial and fail method, I've came up with this:

@app.route("/slack_verification", methods=('GET', 'POST'))
def slack_verification():
    if request.method == 'POST':
        return request.get_json()

And it worked :) The Request URL has been verified.

Related