From 9f7ecd740466c8653d4d82831f951ad3a94745bd Mon Sep 17 00:00:00 2001 From: Jason Litton Date: Tue, 22 Oct 2024 16:14:19 -0600 Subject: [PATCH] bug(slack/api): Make slack /event respond to challenge (#5375) * fix(slack): Make slack /events respond to challenge When configuring event listening for a slack app, your endpoint is required to respond correctly to the slack challenge that is sent. If your endpoint does not respond correctly, slack will not let you use it. Currently, the events endpoint does not respond correctly to the challenge because it is using a background thread to process all incoming events and responding with a static response. In order to use the events endpoint correctly (including using an @ in the incident channel to add an observer), that endpoint has to respond. I altered the event post to check the body for a "url_verification" type and, if it exists, process the event synchronously in the same way the `actions` endpoint does. Otherwise, process async like it always has. * add reformatting --------- Co-authored-by: Marc Vilanova <39573146+mvilanova@users.noreply.github.com> --- src/dispatch/plugins/dispatch_slack/endpoints.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/dispatch/plugins/dispatch_slack/endpoints.py b/src/dispatch/plugins/dispatch_slack/endpoints.py index 05785a844c57..9ccdf425c215 100644 --- a/src/dispatch/plugins/dispatch_slack/endpoints.py +++ b/src/dispatch/plugins/dispatch_slack/endpoints.py @@ -81,7 +81,18 @@ def get_request_handler(request: Request, body: bytes, organization: str) -> Sla ) async def slack_events(request: Request, organization: str, body: bytes = Depends(get_body)): """Handle all incoming Slack events.""" + handler = get_request_handler(request=request, body=body, organization=organization) + try: + body_json = json.loads(body) + # if we're getting the url verification request, + # handle it synchronously so that slack api verification works + if body_json.get("type") == "url_verification": + return handler.handle(req=request, body=body) + except json.JSONDecodeError: + pass + + # otherwise, handle it asynchronously task = BackgroundTask(handler.handle, req=request, body=body) return JSONResponse( background=task,