python - 使用 python flask 的松弛事件 API 响应

标签 python slack-api

我正在使用松弛事件 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.

关于python - 使用 python flask 的松弛事件 API 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52224205/

相关文章:

python - 无法在Python中获取 'channel_list'

python - Python 字典是否保证排序键?

Python:使用子进程在没有死锁的情况下流式传输数据?

Pythonunittest检测circleci下运行测试

go - 使用 slack bot 处理多个数据竞争

javascript - 是否可以使用 slack API 删除没有付费版本的用户

python - 通过 Slack API 将文件发布到聊天

slack-api - 如何在 Slack OAuth 请求中指定机器人和客户端范围

python - 找到直方图最大值对应的x值

python - 如何单击列表框中需要滚动到的元素?