44from sys import exit
55
66from slack_bolt import App
7+ from slack_bolt .adapter .fastapi import SlackRequestHandler
78
89from slack_bolt .oauth .oauth_settings import OAuthSettings
910from slack_sdk .oauth .installation_store .sqlalchemy import SQLAlchemyInstallationStore
@@ -95,6 +96,8 @@ def __repr__(self):
9596 ]
9697 )
9798)
99+ app_handler = SlackRequestHandler (app )
100+
98101
99102error = "Sorry, the forum id hasn't been set! "
100103error += "You can set it via slash command:\n "
@@ -175,7 +178,7 @@ def regex_message_match(say, context, event, client, logger, body):
175178
176179
177180# exit handler
178- def cleanup (signal_received , frame ):
181+ def cleanup ():
179182 logging .info ("Shutting down PiazzaBot..." )
180183
181184 global cache
@@ -187,13 +190,15 @@ def cleanup(signal_received, frame):
187190 session .commit ()
188191
189192 logging .info ("goodbye!" )
190- exit (0 )
191193
192194
193- # Run the app
194- if __name__ == "__main__" :
195- signal (SIGINT , cleanup )
196195
196+ from fastapi import FastAPI , Request
197+
198+ api = FastAPI ()
199+
200+ @app .on_event ("startup" )
201+ async def startup_event ():
197202 with Session (engine ) as session :
198203 courses = session .query (Course )
199204 for course in courses :
@@ -202,4 +207,20 @@ def cleanup(signal_received, frame):
202207 logger .info ("cache built:" )
203208 logger .info (cache )
204209
205- app .start (port = int (os .environ .get ("PORT" , 443 )))
210+ @api .post ("/slack/events" )
211+ async def endpoint (req : Request ):
212+ return await app_handler .handle (req )
213+
214+
215+ @api .get ("/slack/install" )
216+ async def install (req : Request ):
217+ return await app_handler .handle (req )
218+
219+
220+ @api .get ("/slack/oauth_redirect" )
221+ async def oauth_redirect (req : Request ):
222+ return await app_handler .handle (req )
223+
224+ @api .on_event ("shutdown" )
225+ def shutdown_event ():
226+ cleanup ()
0 commit comments