Skip to content

Instantly share code, notes, and snippets.

@ryanhoskin
Last active November 22, 2019 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanhoskin/7951269 to your computer and use it in GitHub Desktop.
Save ryanhoskin/7951269 to your computer and use it in GitHub Desktop.
Sample PHP code to accept PagerDuty webhooks and send out notifications by email on state changes.
For more information, see http://developer.pagerduty.com/documentation/rest/webhooks
This example sends emails to the assigned user for all status changes except resolved. Resolved incidents will email the user who last changed the incident status.
This code is unsupported by PagerDuty.
<?php
$messages = json_decode($HTTP_RAW_POST_DATA); // alternately, try file_get_contents('php://input');
if ($messages) foreach ($messages->messages as $webhook) {
$service = $webhook->data->incident->service->name;
$description = $webhook->data->incident->trigger_summary_data->subject." ".$webhook->data->incident->trigger_summary_data->description;
$status = $webhook->data->incident->status;
$subject = "$status: $description on $service";
$link = $webhook->data->incident->html_url;
$emailAddress = ($webhook->type == "incident.resolve" ? $webhook->data->incident->last_status_change_by->email : $webhook->data->incident->assigned_to_user->email);
$body = "PagerDuty Update\n\nAn incident has been $status.\n\nDetails: $description on service $service\n\nClick here for more details: $link\n";
mail($emailAddress, $subject, $body);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment