-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain.py
329 lines (273 loc) · 12.5 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import os
import re
import uvicorn
import logging
from typing import Any
import sentry_sdk
from dotenv import load_dotenv
from fastapi import FastAPI, Request, Response
from slack_bolt.context.async_context import AsyncBoltContext
from slack_bolt.async_app import AsyncApp
from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.interval import IntervalTrigger
from modules.handlers.channel_join_handler import (
handle_channel_join_request,
handle_channel_join_request_claim,
handle_channel_join_request_claim_reset,
)
from modules.handlers.mentorship_handler import (
handle_mentor_request,
handle_mentorship_request_form_submit,
handle_mentorship_request_claim,
handle_mentorship_request_claim_reset,
)
from modules.handlers.greeting_handler import (
handle_new_member_join,
handle_greeting_new_user_claim,
handle_resetting_greeting_new_user_claim,
)
from modules.handlers.report_handler import (
handle_report,
handle_report_submit,
handle_report_claim,
handle_reset_report_claim,
)
from modules.handlers.daily_programmer import handle_daily_programmer_post
from modules.models.slack_models.event_models import (
MemberJoinedChannelEvent,
MessageReceivedChannelEvent,
)
from modules.models.slack_models.slack_models import (
SlackResponseBody,
SlackUserInfo,
)
from modules.models.slack_models.command_models import SlackCommandRequestBody
from modules.models.slack_models.view_models import SlackViewRequestBody
from modules.models.slack_models.action_models import SlackActionRequestBody
from modules.utils.message_scheduler import schedule_messages
load_dotenv()
logging.basicConfig(level=os.getenv("LOGGING_LEVEL", "INFO"))
logger = logging.getLogger(__name__)
# TODO: Change mentorship view to dynamically add descriptions for the mentorship service block - will require dispatching an action on select and updating the block
# TODO: Allow matching mentor to mentee based on time zone, number of mentees a mentor already has (will need integration with Dreami to track long term relationships)
# TODO: Integrate with current backend to grab information about the mentee after a request is sent to allow for better matching (could be related to time zone, zip code, etc)
# TODO: On startup, check for mentor request threads that haven't been claimed that have been open for more than 24 hours - if there are any, tag @mentor-coordinators in the thread
# TODO: Related to the above TODO, spawn a job when a mentorship request is received to check to make sure it's been claimed in 24 hours - if not, ping @mentor-coordinators
# TODO: Evaluate the above TODOs and maybe decide on a job that checks twice a day? 10 AM CDT and 7 PM CDT? Use Airtable instead of threads to check
# TODO: Track view closures to see when people open and then close without submission
# TODO: Use discriminators and Unions to conditionally return different types depending on a particular field - see https://github.com/samuelcolvin/pydantic/issues/619
# TODO: Flush the cache using a webhook from Airtable when records are added or updated on various tables
# TODO: Check the membership of the mentors internal channel when linking for a mentorship request
# Start an asynchronous Slack Bolt application
app = AsyncApp(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
)
# Currently, the app create functionality is not released in the Slack SDK for Python
# see https://github.com/slackapi/python-slack-sdk/issues/1119
# try:
# bot_name = os.getenv("BOT_NAME")
# bot_username = os.getenv("BOT_USERNAME")
# ngrok_url = os.getenv("NGROK_URL")
# app_config_token = os.getenv("SLACK_APP_CONFIGURATION_TOKEN")
# if bot_name is None or bot_username is None or ngrok_url is None or app_config_token is None:
# raise "Please ensure you have all required environment variables set..."
#
# synchronous_app = App(
# token=os.environ.get("SLACK_BOT_TOKEN"),
# signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
# )
# with open('bot_manifest.yml', 'r') as yaml_manifest:
# yaml_obj = yaml.safe_load(yaml_manifest)
# json_manifest = json.dumps(yaml_obj)
# synchronous_app.client.apps_manifest_create(manifest=json_manifest)
#
# except Exception as generic_error:
# sys.exit(1)
# Define the application handler for the async Slack Bolt application - this adapter is specific to FastAPI
app_handler = AsyncSlackRequestHandler(app)
# Sentry monitoring
if "SENTRY_DSN" in os.environ:
# pylint: disable=abstract-class-instantiated
sentry_sdk.init(dsn=os.getenv("SENTRY_DSN"), request_bodies="medium", environment=os.getenv("RUN_ENV", "development"))
# Define the API
api = FastAPI()
# Initialize an AsyncIOScheduler object to schedule tasks
Scheduler = AsyncIOScheduler({"apscheduler.timezone": "UTC"})
MessageTrigger = IntervalTrigger(minutes=10)
# DailyProgrammerTrigger = IntervalTrigger(hours=24)
# Start up our job scheduler on FastAPI startup and schedule jobs as needed
@api.on_event("startup")
async def startup_event() -> None:
messages = await app.client.chat_scheduledMessages_list()
for message in messages["scheduled_messages"]:
await app.client.chat_deleteScheduledMessage(channel=message["channel_id"], scheduled_message_id=message["id"])
job = Scheduler.add_job(schedule_messages, trigger=MessageTrigger, kwargs={"async_app": app})
Scheduler.start()
logging.debug(f"Scheduled {job.name} with job_id: {job.id}")
# On shutdown, shutdown the scheduler service first
@api.on_event("shutdown")
async def shutdown_event():
Scheduler.shutdown()
# Currently, handled by the old Pybot and can't be handled by us without some legacy token usage
# @api.post("/pybot/api/v1/slack/invite")
# async def invite_new_user(
# email: str = Body(
# ..., example="Test@test.com", description="Email address of the user to invite"
# )
# ) -> None:
# await app.client.admin_users_invite(
# team_id=SlackTeam.slack_id,
# channel_ids=f"{SlackTeam.general_channel.id}",
# email=email,
# )
# The base URI for Slack to communicate with our application - this URI is used for events, commands, and any other interaction
@api.post("/slack/events")
async def base_endpoint(req: Request) -> Response:
return await app_handler.handle(req)
@app.command("/mentor_request")
async def handle_mentor_request_command(
context: AsyncBoltContext,
body: dict[str, Any],
) -> None:
logger.info("STAGE: Processing mentorship request...")
await handle_mentor_request(SlackCommandRequestBody(**body), context)
@app.view("mentorship_request_form_submit")
async def handle_mentorship_request_form_view_submit(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing mentorship form submission...")
await handle_mentorship_request_form_submit(SlackViewRequestBody(**body), context)
@app.action("claim_mentorship_request")
async def handle_mentorship_request_claim_click(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing a mentorship request claim...")
await handle_mentorship_request_claim(SlackActionRequestBody(**body), context)
@app.action("reset_mentorship_request_claim")
async def handle_mentorship_request_claim_reset_click(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing a mentorship request claim reset...")
await handle_mentorship_request_claim_reset(SlackActionRequestBody(**body), context)
@app.command("/new_join") # This is used specifically for testing in staging
@app.event("member_joined_channel")
async def handle_new_member_join_event(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing new member joining...")
if "command" in body.keys() and os.getenv("RUN_ENVIRONMENT") != "production":
await handle_new_member_join(SlackCommandRequestBody(**body), context)
else:
await handle_new_member_join(MemberJoinedChannelEvent(**body), context)
@app.action("greet_new_user_claim")
async def handle_greeting_new_user_claim_action(
context: AsyncBoltContext,
body: dict[str, Any],
) -> None:
logger.info("STAGE: Processing new claim on new user for greetings...")
await handle_greeting_new_user_claim(SlackActionRequestBody(**body), context)
@app.action("reset_greet_new_user_claim")
async def handle_resetting_greeting_new_user_claim_action(
context: AsyncBoltContext, body: dict[str, Any]
) -> None:
logger.info("STAGE: Resetting claim on new user greeting...")
await handle_resetting_greeting_new_user_claim(
SlackActionRequestBody(**body), context
)
@app.command("/report")
async def handle_report_command(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing report command...")
await handle_report(body, context)
@app.view("report_form_submit")
async def handle_report_view_submit(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing report view submission...")
await handle_report_submit(body, context)
@app.action("report_claim")
async def handle_report_claim_action(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing report claim....")
await handle_report_claim(
SlackResponseBody(**body, originating_user=SlackUserInfo(**body["user"])),
context,
)
@app.action("reset_report_claim")
async def handle_reset_report_claim_action(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing reset of report claim...")
await handle_reset_report_claim(
SlackResponseBody(**body, originating_user=SlackUserInfo(**body["user"])),
context,
)
@app.command("/join-pride")
@app.command("/join-blacks-in-tech")
async def handle_channel_join_request_command(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Handling channel join request...")
await handle_channel_join_request(SlackCommandRequestBody(**body), context)
@app.action("invite_to_channel_click")
async def handle_invite_to_channel_click_action(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Handling invite button click for channel join...")
await handle_channel_join_request_claim(SlackActionRequestBody(**body), context)
@app.action("reset_channel_invite")
async def handle_invite_to_channel_reset_action(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Handling reset to invite button click for channel join...")
await handle_channel_join_request_claim_reset(
SlackActionRequestBody(**body), context
)
@app.message(re.compile(r"(={2}.*={3})|(\[.*?])"))
async def handle_daily_programmer(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing daily programmer post...")
await handle_daily_programmer_post(MessageReceivedChannelEvent(**body), context)
@app.event("message")
async def handle_message_event(body: dict[str, Any], context: AsyncBoltContext) -> None:
logger.info("STAGE: Processing message event...")
await context.ack()
@app.event("app_mention")
async def handle_app_mention_event(body: dict[str, Any], context: AsyncBoltContext) -> None:
logger.info("STAGE: Processing app mention event...")
await context.ack()
@app.action("oc_greeting_homepage_click")
async def handle_oc_greeting_homepage_click_action(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing OC greeting homepage click...")
await context.ack()
@app.action("oc_greeting_slack_download_click")
async def handle_oc_greeting_slack_download_click_action(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing OC greeting slack download click...")
await context.ack()
@app.action("oc_greeting_coc_click")
async def handle_oc_greeting_coc_click_action(
body: dict[str, Any], context: AsyncBoltContext
) -> None:
logger.info("STAGE: Processing OC greeting coc click...")
await context.ack()
if __name__ == "__main__":
if os.environ.get("RUN_ENVIRONMENT") == "development":
uvicorn.run(
"main:api",
host="0.0.0.0",
port=8010,
reload=True,
reload_dirs=["./models", "./tests"],
workers=1
)
else:
raise ValueError("RUN_ENVIRONMENT must be set to 'development'")