Last active
February 7, 2018 17:41
-
-
Save Jcpetrucci/56bd6eaa30432eba0d32e2f6f8282968 to your computer and use it in GitHub Desktop.
pagerduty "who's on-call" for slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( $_POST["token"] != "REDACTED" ) { | |
header("HTTP/1.1 403 Forbidden"); | |
die(); | |
} | |
$SLACK_TEXT = preg_replace ( "/$_POST[trigger_word]/", '', $_POST[text] ); | |
function whos_on_call ( $when ) { | |
$PD_API_TOKEN = 'REDACTED'; | |
$SINCE = date( "Y-m-d", strtotime( $when ) ) ."T20:00:00.0600" ; | |
$UNTIL = date ( "Y-m-d", strtotime( $when ." +1 day" ) ); | |
//return "since: ". $SINCE . ".... until: ". $UNTIL; | |
$ON_CALL_PRIMARY = json_decode ( exec ( "curl --get --header 'Authorization: Token token=$PD_API_TOKEN' --header 'Accept: application/vnd.pagerduty+json;version=2' \"https://api.pagerduty.com/schedules/REDACTED/users/?since=$SINCE&until=$UNTIL\" 2>/dev/null "), true); | |
$ON_CALL_SECONDARY = json_decode ( exec ( "curl --get --header 'Authorization: Token token=$PD_API_TOKEN' --header 'Accept: application/vnd.pagerduty+json;version=2' \"https://api.pagerduty.com/schedules/REDACTED/users/?since=$SINCE&until=$UNTIL\" 2>/dev/null "), true); | |
//print_r($ON_CALL); | |
//die(); | |
return $ON_CALL_PRIMARY['users']['0']['name']." is primary on-call. ". $ON_CALL_SECONDARY['users']['0']['name'] ." is secondary."; | |
} | |
if ( preg_match ( '/(on call)|(oncall)/i', $SLACK_TEXT ) ) { | |
$WHEN = ( preg_replace ( "/ ((on call)|(oncall))/i", '', $SLACK_TEXT ) ) ?: "today"; | |
$SLACK_RESPONSE = whos_on_call ( $WHEN ); | |
} else { | |
$SLACK_RESPONSE = "Possible commands: | |
oncall [when] .................... Show who is on call per PagerDuty"; | |
} | |
?> | |
{ | |
"attachments": [ | |
{ | |
"text": "```<?php print $SLACK_RESPONSE; ?>```", | |
"mrkdwn_in": [ | |
"text" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment