slackapi/python-slack-events-api

Stable acknowledgement for incoming requests

seratch opened this issue · 1 comments

Description

This library runs an event listener before responding to an incoming request from Slack with 200 OK. This means if an event handler takes over 3 seconds, the event request times out and the Slack API server resends the same event up to 3 times. You can easily reproduce this issue with the following code snippet.

import time
@slack_events_adapter.on("app_mention")
def handle_app_mentions(event_data):
    time.sleep(5)
    print("Done!")

As a workaround, developers can use threads to run time-consuming tasks asynchronously. Mixing asyncio may not be a great idea as this is a Flask extension and Flask does not support the mechanism.

import time
from concurrent.futures.thread import ThreadPoolExecutor
executor = ThreadPoolExecutor(5)
@slack_events_adapter.on("app_mention")
def handle_app_mentions(event_data):
    def do_something():
        time.sleep(5)
        print("Done!")
    executor.submit(do_something)
    print("Accepted!")

Improving this library is of course worth considering but in the short run, it's not a priority. Also, not only due to this matter, we recommend using Bolt for Python. The new library supports not only Events API but also all the latest features including interactivity.

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion

Requirements

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

I'm sorry to say this, but we are not going to implement this feature.

If you build a Slack app with time-consuming event listeners, we recommend using Bolt for Python instead. The novel framework has great consideration of 3 second timeouts out-of-the-box. You can call ack() method first and then do whatever takes longer than 3 seconds in the same method (under the hood, it uses threads). In addition to that, Bolt has a built-in adapter for Flask. You can find working examples here: https://github.com/slackapi/bolt-python/tree/main/examples/flask

I hope developers in the community will like Bolt too.