-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
66 lines (48 loc) · 1.82 KB
/
app.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import signal
import atexit
from flask import Flask, request
from slack_bolt.adapter.flask import SlackRequestHandler
from slack_bolt import App
from dotenv import load_dotenv
from slack.award_points_events import construct_award_points_events
from slack.github_integration_events import construct_github_events
from slack.leaderboard_events import construct_leaderboard_events
from services import github_integration_service
from settings import mysql_settings
# Initialize MySQL connection globally once and pass along
mysql_settings.init()
mysql_connection = mysql_settings.connection
load_dotenv()
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
app = construct_award_points_events(app, mysql_connection)
app = construct_github_events(app)
app = construct_leaderboard_events(app, mysql_connection)
# Handle slack events through Flask
flask_app = Flask(__name__)
handler = SlackRequestHandler(app)
@flask_app.route("/hook/github", methods=["POST"])
def handle_github_hook():
body = request.json
if github_integration_service.is_pull_request_merged(body):
github_integration_service.store_points(body, mysql_connection)
github_integration_service.send_message(body)
return "200"
@flask_app.route("/slack/events", methods=["POST"])
def slack_events():
return handler.handle(request)
@flask_app.route("/slack/command", methods=["POST"])
def slack_command():
return handler.handle(request)
@flask_app.route("/slack/interactive-endpoint", methods=["POST"])
def slack_interactive():
return handler.handle(request)
def handle_exit():
mysql_connection.close()
# Handle graceful shutdown by closing MySQL connection
atexit.register(handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)