Skip to content

Commit cc194b7

Browse files
authored
Fix #622 by adding Socket Mode healthcheck endpoint examples (#623)
1 parent 6c868f3 commit cc194b7

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import logging
2+
import os
3+
from typing import Optional
4+
5+
from slack_sdk.socket_mode.aiohttp import SocketModeClient
6+
7+
from slack_bolt.app.async_app import AsyncApp
8+
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler
9+
10+
logging.basicConfig(level=logging.DEBUG)
11+
12+
#
13+
# Socket Mode Bolt app
14+
#
15+
16+
# Install the Slack app and get xoxb- token in advance
17+
app = AsyncApp(token=os.environ["SLACK_BOT_TOKEN"])
18+
socket_mode_client: Optional[SocketModeClient] = None
19+
20+
21+
@app.event("app_mention")
22+
async def event_test(event, say):
23+
await say(f"Hi there, <@{event['user']}>!")
24+
25+
26+
#
27+
# Web app for hosting the healthcheck endpoint for k8s etc.
28+
#
29+
30+
from aiohttp import web
31+
32+
33+
async def healthcheck(_req: web.Request):
34+
if socket_mode_client is not None and socket_mode_client.is_connected():
35+
return web.Response(status=200, text="OK")
36+
return web.Response(status=503, text="The Socket Mode client is inactive")
37+
38+
39+
web_app = app.web_app()
40+
web_app.add_routes([web.get("/health", healthcheck)])
41+
42+
43+
#
44+
# Start the app
45+
#
46+
47+
if __name__ == "__main__":
48+
49+
async def start_socket_mode(_web_app: web.Application):
50+
handler = AsyncSocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
51+
await handler.connect_async()
52+
global socket_mode_client
53+
socket_mode_client = handler.client
54+
55+
app.web_app().on_startup.append(start_socket_mode)
56+
app.start(8080)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import logging
2+
import os
3+
from slack_bolt import App
4+
from slack_bolt.adapter.socket_mode import SocketModeHandler
5+
6+
logging.basicConfig(level=logging.DEBUG)
7+
8+
#
9+
# Socket Mode Bolt app
10+
#
11+
12+
# Install the Slack app and get xoxb- token in advance
13+
app = App(token=os.environ["SLACK_BOT_TOKEN"])
14+
socket_mode_handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
15+
16+
17+
@app.event("app_mention")
18+
def event_test(event, say):
19+
say(f"Hi there, <@{event['user']}>!")
20+
21+
22+
#
23+
# Web app for hosting the healthcheck endpoint for k8s etc.
24+
#
25+
26+
# pip install Flask
27+
from flask import Flask, make_response
28+
29+
flask_app = Flask(__name__)
30+
31+
32+
@flask_app.route("/health", methods=["GET"])
33+
def slack_events():
34+
if (
35+
socket_mode_handler.client is not None
36+
and socket_mode_handler.client.is_connected()
37+
):
38+
return make_response("OK", 200)
39+
return make_response("The Socket Mode client is inactive", 503)
40+
41+
42+
#
43+
# Start the app
44+
#
45+
# export SLACK_APP_TOKEN=xapp-***
46+
# export SLACK_BOT_TOKEN=xoxb-***
47+
48+
if __name__ == "__main__":
49+
socket_mode_handler.connect() # does not block the current thread
50+
flask_app.run(port=8080)

0 commit comments

Comments
 (0)