async def handle_slack_event(*, client, event, background_tasks): """Handles slack event message.""" user_id = event.event.user channel_id = get_channel_id_from_event(event) if user_id and channel_id: db_session = get_organization_from_channel_id(channel_id=channel_id) if not db_session: log.info( f"Unable to determine organization associated with channel id. ChannelId: {channel_id}" ) return {"ok": ""} conversation = conversation_service.get_by_channel_id_ignoring_channel_type( db_session=db_session, channel_id=channel_id) if conversation and dispatch_slack_service.is_user(user_id): # We resolve the user's email user_email = await dispatch_slack_service.get_user_email_async( client, user_id) # Dispatch event functions to be executed in the background for f in event_functions(event): background_tasks.add_task( f, user_id, user_email, channel_id, conversation.incident_id, event=event, ) return {"ok": ""}
def member_joined_channel( user_email: str, incident_id: int, event: EventEnvelope, db_session=None, ): """Handles the member_joined_channel slack event.""" participant = incident_flows.incident_add_or_reactivate_participant_flow( user_email=user_email, incident_id=incident_id, db_session=db_session) if event.event.inviter: # we update the participant's metadata if not dispatch_slack_service.is_user(event.event.inviter): # we default to the incident commander when we don't know how the user was added added_by_participant = participant_service.get_by_incident_id_and_role( db_session=db_session, incident_id=incident_id, role=ParticipantRoleType.incident_commander, ) participant.added_by = added_by_participant participant.added_reason = ( f"Participant added by {added_by_participant.individual.name}") else: inviter_email = get_user_email(client=slack_client, user_id=event.event.inviter) added_by_participant = participant_service.get_by_incident_id_and_email( db_session=db_session, incident_id=incident_id, email=inviter_email) participant.added_by = added_by_participant participant.added_reason = event.event.text db_session.add(participant) db_session.commit()
async def handle_event( event: EventEnvelope, request: Request, response: Response, background_tasks: BackgroundTasks, x_slack_request_timestamp: int = Header(None), x_slack_signature: str = Header(None), db_session: Session = Depends(get_db), ): """Handle all incomming Slack events.""" raw_request_body = bytes.decode(await request.body()) # We verify the timestamp verify_timestamp(x_slack_request_timestamp) # We verify the signature verify_signature(raw_request_body, x_slack_request_timestamp, x_slack_signature) # Echo the URL verification challenge code back to Slack if event.challenge: return {"challenge": event.challenge} event_body = event.event if (event_body.type == "message" and event_body.subtype): # We ignore messages that have a subtype # Parse the Event payload and emit the event to the event listener response.headers["X-Slack-Powered-By"] = create_ua_string() return {"ok"} user_id = event_body.user # Fetch conversation by channel id channel_id = ( # We ensure channel_id always has a value event_body.channel_id if event_body.channel_id else event_body.channel) conversation = get_by_channel_id(db_session=db_session, channel_id=channel_id) if conversation and dispatch_slack_service.is_user(user_id): # We create an async Slack client slack_async_client = dispatch_slack_service.create_slack_client( run_async=True) # We resolve the user's email user_email = await dispatch_slack_service.get_user_email_async( slack_async_client, user_id) # Dispatch event functions to be executed in the background for f in event_functions(event): background_tasks.add_task(f, user_email, conversation.incident_id) # We add the user-agent string to the response headers response.headers["X-Slack-Powered-By"] = create_ua_string() return {"ok"}
async def handle_event( event: EventEnvelope, request: Request, response: Response, background_tasks: BackgroundTasks, x_slack_request_timestamp: int = Header(None), x_slack_signature: str = Header(None), db_session: Session = Depends(get_db), ): """Handle all incomming Slack events.""" raw_request_body = bytes.decode(await request.body()) # We verify the timestamp verify_timestamp(x_slack_request_timestamp) # We verify the signature verify_signature(raw_request_body, x_slack_request_timestamp, x_slack_signature) # Echo the URL verification challenge code back to Slack if event.challenge: return {"challenge": event.challenge} event_body = event.event user_id = event_body.user channel_id = get_channel_id_from_event(event_body) if user_id and channel_id: conversation = conversation_service.get_by_channel_id_ignoring_channel_type( db_session=db_session, channel_id=channel_id) if conversation and dispatch_slack_service.is_user(user_id): # We create an async Slack client slack_async_client = dispatch_slack_service.create_slack_client( run_async=True) # We resolve the user's email user_email = await dispatch_slack_service.get_user_email_async( slack_async_client, user_id) # Dispatch event functions to be executed in the background for f in event_functions(event): background_tasks.add_task(f, user_email, conversation.incident_id, event=event) # We add the user-agent string to the response headers response.headers["X-Slack-Powered-By"] = create_ua_string() return {"ok"}
async def handle_slack_event(*, db_session, client, event, background_tasks): """Handles slack event message.""" user_id = event.event.user channel_id = get_channel_id_from_event(event) if user_id and channel_id: conversation = conversation_service.get_by_channel_id_ignoring_channel_type( db_session=db_session, channel_id=channel_id) if conversation and dispatch_slack_service.is_user(user_id): # We resolve the user's email user_email = await dispatch_slack_service.get_user_email_async( client, user_id) # Dispatch event functions to be executed in the background for f in event_functions(event): background_tasks.add_task(f, user_email, conversation.incident_id, event=event) return {"ok": ""}