使用 python flask 响应 Slack 事件 API

Slack event API response using python flask

我正在处理松弛事件 API。我正在获取有关订阅事件的事件。 但是如何使用 python 请求发送响应。几秒钟后,slack 再次发回相同的事件 json 我需要将什么发送回 slack 作为停止接收相同响应的响应? 如果你知道代码,提前非常感谢你:)

@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)

您需要做的就是在 3 秒内用 HTTP 200 OK 直接响应 Slack 的请求。如果您的应用程序在该时间内终止,这将自动发生。

如果您需要更多处理时间,您应该考虑将事件排队以供稍后处理或启动异步进程进行处理。

这是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.

直接return成功收到所需信息的状态。

return 'HTTP 200 OK'

试试这个:

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

我发现这个老话题没有为我的问题提供适当的解决方案。通过尝试和失败的方法,我想出了这个:

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

它成功了 :) 请求 URL 已被验证。