-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathsocket_mode_healthcheck.py
47 lines (33 loc) · 1.08 KB
/
socket_mode_healthcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import logging
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
logging.basicConfig(level=logging.DEBUG)
#
# Socket Mode Bolt app
#
# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"])
socket_mode_handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
@app.event("app_mention")
def event_test(event, say):
say(f"Hi there, <@{event['user']}>!")
#
# Web app for hosting the healthcheck endpoint for k8s etc.
#
# pip install Flask
from flask import Flask, make_response
flask_app = Flask(__name__)
@flask_app.route("/health", methods=["GET"])
def slack_events():
if socket_mode_handler.client is not None and socket_mode_handler.client.is_connected():
return make_response("OK", 200)
return make_response("The Socket Mode client is inactive", 503)
#
# Start the app
#
# export SLACK_APP_TOKEN=xapp-***
# export SLACK_BOT_TOKEN=xoxb-***
if __name__ == "__main__":
socket_mode_handler.connect() # does not block the current thread
flask_app.run(port=8080)