def slash_command(request): """ Handles slash commands from slack More details here: https://api.slack.com/slash-commands @param request the request from slack containing the slash command @return: return a HTTP response to indicate the request was handled """ report = request.POST.get('text') if not report: return HttpResponse("It looks like you forgot to provide any details 🤔 \ \n\nCan you try again with something like `/incident something has happened`?") user_id = request.POST.get('user_id') Incident.objects.create_incident( report=report, reporter=user_id, report_time=datetime.now(), ) incidents_channel_ref = channel_reference(settings.INCIDENT_CHANNEL_ID) text = f"Thanks for raising the incident 🙏\n\nHead over to {incidents_channel_ref} to complete the report and/or help deal with the issue" return HttpResponse(text)
def report_incident(user_id: str, channel_id: str, submission: json, response_url: str, state: json): report = submission['report'] summary = submission['summary'] impact = submission['impact'] lead_id = submission['lead'] severity = submission['severity'] name = get_user_profile(user_id)['name'] reporter = GetOrCreateSlackExternalUser(external_id=user_id, display_name=name) lead = None if lead_id: lead_name = get_user_profile(lead_id)['name'] lead = GetOrCreateSlackExternalUser(external_id=lead_id, display_name=lead_name) Incident.objects.create_incident( report=report, reporter=reporter, report_time=datetime.now(), summary=summary, impact=impact, lead=lead, severity=severity, ) incidents_channel_ref = channel_reference(settings.INCIDENT_CHANNEL_ID) text = f"Thanks for raising the incident 🙏\n\nHead over to {incidents_channel_ref} to complete the report and/or help deal with the issue" send_ephemeral_message(channel_id, user_id, text)
def update_in_slack(self): "Creates/updates the slack headline post with the latest incident info" msg = Message() # Set the fallback text so notifications look nice msg.set_fallback_text(f"{self.incident.report} reported by {user_reference(self.incident.reporter)}") # Add report/people msg.add_block(Section(block_id="report", text=Text(f"*{self.incident.report}*"))) msg.add_block(Section(block_id="reporter", text=Text(f"🙋🏻♂️ Reporter: {user_reference(self.incident.reporter)}"))) incident_lead_text = user_reference(self.incident.lead) if self.incident.lead else "-" msg.add_block(Section(block_id="lead", text=Text(f"👩🚒 Incident Lead: {incident_lead_text}"))) squad_text = self.incident.squad_text() msg.add_block(Section(block_id="squad", text=Text(f":office: Squad: {squad_text}"))) msg.add_block(Divider()) # Add additional info msg.add_block(Section(block_id="status", text=Text(f"{self.incident.status_emoji()} Status: {self.incident.status_text().capitalize()}"))) # severity_text = self.incident.severity_text() # msg.add_block(Section(block_id="severity", text=Text(f":bar_chart: Severity: {severity_text}"))) doc_url = urljoin( settings.SITE_URL, reverse('incident_doc', kwargs={'incident_id': self.incident.pk}) ) msg.add_block(Section(block_id="incident_doc", text=Text(f"📄 Document: <{doc_url}|Incident {self.incident.pk}>"))) channel_ref = channel_reference(self.comms_channel.channel_id) if self.comms_channel else None if channel_ref: msg.add_block(Section(block_id="comms_channel", text=Text(f"🗣 Comms Channel: {channel_ref or '-'}"))) # Add buttons (if the incident is open) if not self.incident.is_closed(): msg.add_block(Section(text=Text("Need something else?"))) actions = Actions(block_id="actions") if not self.comms_channel: actions.add_element(Button(":speaking_head_in_silhouette: Create Comms Channel", self.CREATE_COMMS_CHANNEL_BUTTON, value=self.incident.pk)) if settings.PAGERDUTY_ENABLED: confirm = Confirm("Page an On-caller", "Are you sure you want to page someone?", "Yes - I need help now!", "No") actions.add_element(Button(":pager: Page On-call", self.PAGE_ON_CALL_BUTTON, value=self.incident.pk, confirm=confirm)) actions.add_element(Button(":pencil2: Edit", self.EDIT_INCIDENT_BUTTON, value=self.incident.pk)) actions.add_element(Button(":white_check_mark: Close", self.CLOSE_INCIDENT_BUTTON, value=self.incident.pk)) msg.add_block(actions) # Post / update the slack message response = msg.send(settings.INCIDENT_CHANNEL_ID, self.message_ts) # Save the message ts identifier if not already set if not self.message_ts: self.message_ts = response['ts'] self.save()
def report_incident(user_id: str, channel_id: str, submission: json, response_url: str, state: json): report = submission['report'] summary = submission['summary'] impact = submission['impact'] lead = submission['lead'] severity = submission['severity'] Incident.objects.create_incident( report=report, reporter=user_id, report_time=datetime.now(), summary=summary, impact=impact, lead=lead, severity=severity, ) incidents_channel_ref = channel_reference(settings.INCIDENT_CHANNEL_ID) text = f"Thanks for raising the incident 🙏\n\nHead over to {incidents_channel_ref} to complete the report and/or help deal with the issue" send_ephemeral_message(channel_id, user_id, text)
def update_in_slack(self): "Creates/updates the slack headline post with the latest incident info" msg = Message() # Set the fallback text so notifications look nice msg.set_fallback_text( f"{self.incident.report} reported by {user_reference(self.incident.reporter)}" ) # Incident title msg.add_block( Section( block_id="report", text=Text( f":rotating_light: *{self.incident.report}* :rotating_light:" ))) # Incident lead and status incident_lead_text = user_reference( self.incident.lead.external_id) if self.incident.lead else "-" msg.add_block( Section( block_id="second_row", fields=[ Text( f":male-firefighter: Incident Lead: {incident_lead_text}" ), Text( f"{self.incident.status_emoji()} Status: {self.incident.status_text().capitalize()}" ) ])) # Incident reporter and severity severity_text = self.incident.severity_text().capitalize( ) if self.incident.severity_text() else "-" msg.add_block( Section( block_id="third_row", fields=[ Text( f":man-raising-hand: Reporter: {user_reference(self.incident.reporter.external_id)}" ), Text( f"{self.incident.severity_emoji()} Severity: {severity_text}" ) ])) # Incident comms channel document # Generate document doc_url = urljoin( settings.SITE_URL, reverse('incident_doc', kwargs={'incident_id': self.incident.pk})) channel_ref = channel_reference( self.comms_channel.channel_id) if self.comms_channel else None if channel_ref: msg.add_block( Section( block_id="fourth_row", fields=[ Text( f":speech_balloon: Comms Channel: {channel_ref or '-'}" ), Text( f":newspaper: Document: <{doc_url}|Incident {self.incident.pk}>" ) ])) # Incident impact and summary msg.add_block( Section( fields=[Text(f":world_map: Impact: {self.incident.impact}")])) msg.add_block( Section( fields=[Text(f":rainbow: Summary: {self.incident.summary}")])) # Divider msg.add_block(Divider()) # Add buttons (if the incident is open) if not self.incident.is_closed(): msg.add_block(Section(text=Text("Need something else?"))) actions = Actions(block_id="actions") if not self.comms_channel: actions.add_element( Button( ":speaking_head_in_silhouette: Create Comms Channel", self.CREATE_COMMS_CHANNEL_BUTTON, value=self.incident.pk)) if settings.PAGERDUTY_ENABLED: confirm = Confirm("Page an On-caller", "Are you sure you want to page someone?", "Yes - I need help now!", "No") actions.add_element( Button(":pager: Page On-call", self.PAGE_ON_CALL_BUTTON, value=self.incident.pk, confirm=confirm)) actions.add_element( Button(":pencil2: Edit", self.EDIT_INCIDENT_BUTTON, value=self.incident.pk)) actions.add_element( Button(":white_check_mark: Close", self.CLOSE_INCIDENT_BUTTON, value=self.incident.pk)) msg.add_block(actions) # Post / update the slack message response = msg.send(settings.INCIDENT_CHANNEL_ID, self.message_ts) # Save the message ts identifier if not already set if not self.message_ts: self.message_ts = response['ts'] self.save()
def update_in_slack(self): "Creates/updates the slack headline post with the latest incident info" msg = Message() # Add report/people msg.add_block( Section(block_id="report", text=Text(f"*{self.incident.report}*"))) msg.add_block( Section( block_id="reporter", text=Text( f"🙋🏻♂️ Reporter: {user_reference(self.incident.reporter)}" ))) incident_lead_text = user_reference( self.incident.lead) if self.incident.lead else "-" msg.add_block( Section(block_id="lead", text=Text(f"👩🚒 Incident Lead: {incident_lead_text}"))) msg.add_block(Divider()) # Add additional info severity_text = self.incident.severity_text().capitalize( ) if self.incident.severity_text() else "-" msg.add_block( Section( block_id="severity", text=Text( f"{self.incident.severity_emoji()} Severity: {severity_text}" ))) doc_url = urljoin( settings.SITE_URL, reverse('incident_doc', kwargs={'incident_id': self.incident.pk})) msg.add_block( Section( block_id="incident_doc", text=Text( f"📄 Document: <{doc_url}|Incident {self.incident.pk}>"))) channel_ref = channel_reference( self.comms_channel.channel_id) if self.comms_channel else None msg.add_block( Section(block_id="comms_channel", text=Text(f"🗣 Comms Channel: {channel_ref or '-'}"))) # Add buttons (if the incident is open) if not self.incident.is_closed(): msg.add_block(Section(text=Text("Need something else?"))) actions = Actions(block_id="actions") if not self.comms_channel: actions.add_element( Button( ":speaking_head_in_silhouette: Create Comms Channel", self.CREATE_COMMS_CHANNEL_BUTTON, value=self.incident.pk)) actions.add_element( Button(":white_check_mark: Close", self.CLOSE_INCIDENT_BUTTON, value=self.incident.pk)) msg.add_block(actions) # Post / update the slack message response = msg.send(settings.INCIDENT_CHANNEL_ID, self.message_ts) # Save the message ts identifier if not already set if not self.message_ts: self.message_ts = response['ts'] self.save()