def show_close_incident_modal(self, team_id, channel_id, trigger_id): """Show a slack modal with an input to add incident resolution text""" slack_access_token = DynamoUtils.get_slack_access_token(team_id) client = WebClient(token=slack_access_token) formatter = CloseIncidentFormatter() client.views_open(trigger_id=trigger_id, view=formatter.format(channel_id).get("view"))
def interaction_handler(message, _context): """ Handles interactive events from Slack elements like buttons """ response_url = message.get("response_url") interaction_type = message.get("type") team = message.get("team") team_id = team.get("id") slack_access_token = DynamoUtils.get_slack_access_token(team_id) client = WebClient(token=slack_access_token) if interaction_type == "view_submission": view = message.get("view") block_id = view.get("blocks")[0].get("block_id") action_id = view.get("blocks")[0].get("element").get("action_id") if action_id == "incident_resolution": incident_id = "" try: resolution_text = (view.get("state").get("values").get( block_id).get("incident_resolution").get("value")) incident_id = view.get("private_metadata") slack_events_handler = SlackEventsHandler(client, team_id) slack_events_handler.close_incident_and_add_resolution( incident_id, resolution_text) return "ok" except Exception as ex: logger.error( f"error closing incident for team {team_id} and incident {incident_id} {ex}" ) client.chat_postMessage( channel=incident_id, text= "There was an error closing the incident. Please contact support", ) return "failed" else: action = message.get("actions")[0] action_id = action.get("action_id") if action_id == "cancel": __update_original_message(response_url, "Incident creation cancelled") elif action_id == "create_incident": if "team" in message and "id" in message.get("team"): try: container = message.get("container") channel = container.get("channel_id") incident_name = action.get("value") slack_events_handler = SlackEventsHandler(client, team_id) slack_events_handler.create_new_incident( channel, incident_name) __delete_original_message(response_url) except Exception as e: logger.error(f"exception during incident creation {e}") return "error" return "ok"
def handle_message(msg, _context): """ Handles and reacts to messages sent in slack channels """ team_id = msg.get("team") slack_access_token = DynamoUtils.get_slack_access_token(team_id) client = WebClient(token=slack_access_token) channel = msg["channel"] slack_events_handler = SlackEventsHandler(client, team_id) if msg.get("subtype") is None and "parca" in msg.get("text"): response = "You probably meant Lisandro, <@%s>! :tada:" % msg["user"] client.chat_postMessage(channel=channel, text=response) elif (msg.get("subtype") is None and re.search("^log:", msg.get("text"), re.IGNORECASE) is not None): text_to_log = msg.get("text").replace("LOG: ", "") slack_events_handler.log_comment(channel, text_to_log) return
def handle_mention(message, _context): """ Handle bots mentions """ team_id = message.get("team") slack_access_token = DynamoUtils.get_slack_access_token(team_id) client = WebClient(token=slack_access_token) channel = message["channel"] slack_events_handler = SlackEventsHandler(client, team_id) if message.get("subtype") is None and "alive" in message.get("text"): message = "Yes, <@%s>! I am!" % message["user"] client.chat_postMessage(channel=channel, text=message) elif message.get("subtype") is None and "create ticket" in message.get( "text"): ticket: Integration = slack_events_handler.create_ticket() if ticket is not None: client.chat_postMessage(channel=channel, text="ticket created: " + ticket.get_link()) else: client.chat_postMessage( channel=channel, text="Ticket not created. Do you have a ticket integration?", ) elif message.get("subtype") is None and "create call" in message.get( "text"): call: Integration = slack_events_handler.create_call() if call is not None: client.chat_postMessage(channel=channel, text="call created: " + call.get_link()) else: client.chat_postMessage( channel=channel, text="Call not created. Do you have a call integration?", ) elif message.get("subtype") is None and "get call" in message.get("text"): call_link = slack_events_handler.get_call(channel) if bool(call_link): client.chat_postMessage(channel=channel, text="Call: " + call_link) else: client.chat_postMessage(channel=channel, text="No call available in this channel") elif message.get("subtype") is None and ( "new incident" in message.get("text") or "create incident" in message.get("text")): incident_name = sanitise_incident_name(message.get("text")) slack_events_handler.handle_new_incident_creation( channel, incident_name) elif message.get("subtype") is None and ( "who is oncall" in message.get("text") or "who’s oncall" in message.get("text")): user_id = slack_events_handler.get_oncall(team_id) if user_id is None: client.chat_postMessage( channel=channel, text= "An oncall hasn't been set yet.```/sereno set oncall <user>```", ) return client.chat_postMessage(channel=channel, text=f"<@{user_id}> is oncall") elif message.get("subtype") is None and ( "ongoing" in message.get("text") and ("incident" in message.get("text") or "incidents" in message.get("text"))): response = slack_events_handler.get_ongoing_incidents_from_today( team_id) client.chat_postMessage(channel=channel, blocks=response)
def command_handler(message, _context): """ Handles commands sent to slackbot """ team_id = message["team_id"] user_id = message["user_id"] command = message["text"] trigger_id = message.get("trigger_id") channel_id = message.get("channel_id") slack_access_token = DynamoUtils.get_slack_access_token(team_id) client = WebClient(token=slack_access_token) slack_commands_handler = SlackCommandsHandler(team_id) if "register" in command: client.chat_postEphemeral( channel=channel_id, user=user_id, blocks=slack_commands_handler.build_register_response(user_id).get( "blocks"), ) elif "responders" in command: if "add" in command: responders_list_users = re.findall(USER_ID_REGEX, command) responders_list_channels = re.findall(CHANNEL_ID_REGEX, command) responders_list = responders_list_users + responders_list_channels if len(responders_list) == 0: client.chat_postEphemeral( channel=channel_id, user=user_id, text="List of responders cannot be empty!", ) response = slack_commands_handler.add_responders(responders_list) client.chat_postEphemeral(channel=channel_id, user=user_id, text=response) elif "remove" in command: responders_list_users = re.findall(USER_ID_REGEX, command) responders_list_channels = re.findall(CHANNEL_ID_REGEX, command) responders_list = responders_list_users + responders_list_channels response = slack_commands_handler.remove_responders( responders_list) client.chat_postEphemeral(channel=channel_id, user=user_id, text=response) elif "list" in command: response = slack_commands_handler.list_responders() client.chat_postEphemeral(channel=channel_id, user=user_id, text=response) else: client.chat_postEphemeral( channel=channel_id, user=user_id, text= "Sorry I did not understand the command: options are add, remove, list", ) elif "oncall" in command: if "set" in command: user_matched = re.search(USER_ID_REGEX, command) if user_matched is not None: user_id_to_set = user_matched.group(1) slack_commands_handler.set_oncall(user_id_to_set) client.chat_postEphemeral( channel=channel_id, user=user_id, text="<@%s> set as oncall" % user_id_to_set, ) else: client.chat_postEphemeral( channel=channel_id, user=user_id, text="Sorry, wrong format. Do `/sereno set oncall <user>`", ) elif "help" in command: formatter = HelpFormatter() client.chat_postEphemeral(channel=channel_id, user=user_id, blocks=formatter.format().get("blocks")) elif "close incident" in command: slack_commands_handler.show_close_incident_modal( team_id, channel_id, trigger_id) else: client.chat_postEphemeral( channel=channel_id, user=user_id, text="Sorry I did not understand the command. " "Type `/sereno help` to see a list of available commands", )