Skip to content

Commit b24a376

Browse files
committed
switch to fastapi, fingers crossed
1 parent cb061fd commit b24a376

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

app.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from sys import exit
55

66
from slack_bolt import App
7+
from slack_bolt.adapter.fastapi import SlackRequestHandler
78

89
from slack_bolt.oauth.oauth_settings import OAuthSettings
910
from 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

99102
error = "Sorry, the forum id hasn't been set! "
100103
error += "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()

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
slack-bolt==1.15.1
22
PyMySQL==1.0.2
33
SQLAlchemy==1.4.44
4+
fastapi==0.89.1
5+
uvicorn==0.20.0

0 commit comments

Comments
 (0)