def after_hours(user_email: str, incident_id: int, db_session=None): """Notifies the user that this incident is current in after hours mode.""" incident = incident_service.get(db_session=db_session, incident_id=incident_id) user_id = dispatch_slack_service.resolve_user(slack_client, user_email)["id"] # NOTE Limitations: Does not sync across instances. Does not survive webserver restart cache_key = create_cache_key(user_id, incident.conversation.channel_id) try: once_a_day_cache[cache_key] return except Exception: pass # we don't care if there is nothing here # bail early if we don't care for a given severity priority_types = [ IncidentPriorityType.info, IncidentPriorityType.low, IncidentPriorityType.medium, ] if incident.incident_priority.name.lower() not in priority_types: return # get their timezone from slack commander_info = dispatch_slack_service.get_user_info_by_email( slack_client, email=incident.commander.email) commander_tz = commander_info["tz"] if not is_business_hours(commander_tz): # send ephermal message blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": ((f"Responses may be delayed. The current incident severity is *{incident.incident_severity.name}*" f" and your message was sent outside of the incident commander's working hours (Weekdays, 9am-5pm, {commander_tz})." )), }, }] dispatch_slack_service.send_ephemeral_message( slack_client, incident.conversation.channel_id, user_id, "", blocks=blocks) once_a_day_cache[cache_key] = True
def after_hours( config: SlackConversationConfiguration, user_id: str, user_email: str, channel_id: str, incident_id: int, event: EventEnvelope = None, db_session=None, slack_client=None, ): """Notifies the user that this incident is current in after hours mode.""" # we ignore user channel and group join messages if event.event.subtype in ["channel_join", "group_join"]: return incident = incident_service.get(db_session=db_session, incident_id=incident_id) # get their timezone from slack commander_info = dispatch_slack_service.get_user_info_by_email( slack_client, email=incident.commander.individual.email) commander_tz = commander_info["tz"] if not is_business_hours(commander_tz): # send ephermal message blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": ((f"Responses may be delayed. The current incident priority is *{incident.incident_priority.name}*" f" and your message was sent outside of the Incident Commander's working hours (Weekdays, 9am-5pm, {commander_tz} timezone)." )), }, }] participant = participant_service.get_by_incident_id_and_email( db_session=db_session, incident_id=incident_id, email=user_email) if not participant.after_hours_notification: dispatch_slack_service.send_ephemeral_message(slack_client, channel_id, user_id, "", blocks=blocks) participant.after_hours_notification = True db_session.add(participant) db_session.commit()
def after_hours(user_email: str, incident_id: int, event: dict = None, db_session=None): """Notifies the user that this incident is current in after hours mode.""" incident = incident_service.get(db_session=db_session, incident_id=incident_id) # get their timezone from slack commander_info = dispatch_slack_service.get_user_info_by_email( slack_client, email=incident.commander.email) commander_tz = commander_info["tz"] if not is_business_hours(commander_tz): # send ephermal message blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": ((f"Responses may be delayed. The current incident priority is *{incident.incident_priority.name}*" f" and your message was sent outside of the Incident Commander's working hours (Weekdays, 9am-5pm, {commander_tz} timezone)." )), }, }] participant = participant_service.get_by_incident_id_and_email( incident_id=incident_id, email=user_email) if not participant.after_hours_notification: user_id = dispatch_slack_service.resolve_user( slack_client, user_email)["id"] dispatch_slack_service.send_ephemeral_message( slack_client, incident.conversation.channel_id, user_id, "", blocks=blocks) participant.after_hours_notification = True db_session.add(participant) db_session.commit()