PagerDuty Webhooks V3 Inbound Webhook Script

I created this script when setting up an inbound webhook to receive notifications from PagerDuty via their new Webhooks V3 API:

const CHANNEL = "#incidents"

class Script {
  process_incoming_request({ request }) {
    console.log("PagerDuty Webhook Request", JSON.stringify(request.content))

    const event = request.content.event
    const eventType = event.event_type
    const eventName = s.humanize(eventType.split(".")[1])
    const data = event.data

    switch (eventType) {
      case "incident.acknowledged":
      case "incident.delegated":
      case "incident.escalated":
      case "incident.priority_updated":
      case "incident.reassigned":
      case "incident.reopened":
      case "incident.reopened":
      case "incident.resolved":
      case "incident.triggered":
      case "incident.unacknowledged":
        return {
          content: {
            text: `[#${data.number}] ${data.title}`,
            channel: CHANNEL,
            attachments: [
              {
                color: getAttachmentColor(data.status),
                title: eventName,
                title_link: data.html_url,
                text: data.content,
                fields: [
                  data.service && {
                    title: "Service",
                    value: data.service.summary,
                    short: true,
                  },
                  data.status && {
                    title: "Status",
                    value: s.humanize(data.status),
                    short: true,
                  },
                  event.agent && {
                    title: "From",
                    value: event.agent.summary,
                    short: true,
                  },
                  data.assignees &&
                    data.assignees.length && {
                      title: "Assignees",
                      value: data.assignees
                        .map((assignee) => assignee.summary)
                        .join(", "),
                      short: true,
                    },
                  data.urgency && {
                    title: "Urgency",
                    value: s.humanize(data.urgency),
                    short: true,
                  },
                  data.priority && {
                    title: "Priority",
                    value: data.priority.summary,
                    short: true,
                  },
                  data.escalation_policy && {
                    title: "Escalation Policy",
                    value: data.escalation_policy.summary,
                    short: true,
                  },
                ].filter(Boolean),
              },
            ],
          },
        }
      case "incident.annotated":
        return {
          content: {
            text: data.incident.summary,
            channel: CHANNEL,
            attachments: [
              {
                color: getAttachmentColor(),
                title: `Note from ${event.agent.summary}`,
                title_link: data.incident.html_url,
                text: data.content,
              },
            ],
          },
        }
    }
  }
}

function getAttachmentColor(status) {
  switch (status) {
    case "triggered":
      return "#f4452a"
    case "acknowledged":
      return "#f4a02a"
    case "resolved":
      return "#05b870"
    default:
      return "#c9d0d9"
  }
}
1 Like

Thanks for sharing, @elliot

Thanks for this V3 webhook script. It has worked very well for us until we upgraded from RC 4.8 to 6.3. Now we get an error: TypeError: s.humanize is not a function. Seems RC has removed the humanize function. Is there a new version of the script that works on RC 6.3 or a workaround?