diff --git a/README.md b/README.md index 1cb93ef..6ddacfa 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,8 @@ It needs a few environment variables defined to work: PAGERDUTY_SCHEDULE_ID PAGERDUTY_API_TOKEN - PAGERDUTY_DOMAIN -Those names should be fairly self-explanatory. The domain is the piece of your PagerDuty URL that is specific to you -i.e. https://[PAGERDUTY_DOMAIN].pagerduty.com/ +Those names should be fairly self-explanatory. You can also optionally set PHONEDUTY_ANNOUNCE_TIME, which if set to a TRUEish value will include the current time of the engineer being called as part of the answering message. This may help raise awareness that you are potentially getting diff --git a/app.json b/app.json index 42b6d0c..8ef71b1 100644 --- a/app.json +++ b/app.json @@ -14,9 +14,6 @@ }, "PAGERDUTY_API_TOKEN": { "description": "PagerDuty API token" - }, - "PAGERDUTY_DOMAIN": { - "description": "The name of your PagerDuty a/c, as it appears in the URL" } } } diff --git a/composer.json b/composer.json index e7e75cc..2563252 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "require": { "php": ">=5.3.8", "twilio/sdk": "dev-master", - "guzzlehttp/guzzle": "~4.0" + "guzzlehttp/guzzle": "~6.0" }, "config": { diff --git a/composer.lock b/composer.lock index 70020a7..33112ee 100644 --- a/composer.lock +++ b/composer.lock @@ -1,51 +1,51 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "hash": "ec9a1474cc6bc44287f209ae972a9715", + "content-hash": "b91dd007773f546c262b1b91a29f2729", "packages": [ { "name": "guzzlehttp/guzzle", - "version": "4.1.7", + "version": "6.3.3", "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", - "reference": "448f2c2076cf0fb756230611491c4f7ecb735a29" + "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/448f2c2076cf0fb756230611491c4f7ecb735a29", - "reference": "448f2c2076cf0fb756230611491c4f7ecb735a29", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/407b0cb880ace85c9b63c5f9551db498cb2d50ba", + "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba", "shasum": "" }, "require": { - "ext-json": "*", - "guzzlehttp/streams": "~1.4", - "php": ">=5.4.0" + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" }, "require-dev": { "ext-curl": "*", - "phpunit/phpunit": "~4.0", - "psr/log": "~1.0" + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0", + "psr/log": "^1.0" }, "suggest": { - "ext-curl": "Guzzle will use specific adapters if cURL is present" + "psr/log": "Required for using the Log middleware" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "4.1-dev" + "dev-master": "6.3-dev" } }, "autoload": { + "files": [ + "src/functions_include.php" + ], "psr-4": { "GuzzleHttp\\": "src/" - }, - "files": [ - "src/functions.php" - ] + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -58,7 +58,7 @@ "homepage": "https://github.com/mtdowling" } ], - "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", + "description": "Guzzle is a PHP HTTP client library", "homepage": "http://guzzlephp.org/", "keywords": [ "client", @@ -69,27 +69,83 @@ "rest", "web service" ], - "time": "2014-08-08 01:30:43" + "time": "2018-04-22T15:46:56+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" }, { - "name": "guzzlehttp/streams", - "version": "1.5.1", + "name": "guzzlehttp/psr7", + "version": "1.5.2", "source": { "type": "git", - "url": "https://github.com/guzzle/streams.git", - "reference": "fb0d1ee29987c2bdc59867bffaade6fc88c2675f" + "url": "https://github.com/guzzle/psr7.git", + "reference": "9f83dded91781a01c63574e387eaa769be769115" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/streams/zipball/fb0d1ee29987c2bdc59867bffaade6fc88c2675f", - "reference": "fb0d1ee29987c2bdc59867bffaade6fc88c2675f", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/9f83dded91781a01c63574e387eaa769be769115", + "reference": "9f83dded91781a01c63574e387eaa769be769115", "shasum": "" }, "require": { - "php": ">=5.4.0" + "php": ">=5.4.0", + "psr/http-message": "~1.0", + "ralouphie/getallheaders": "^2.0.5" + }, + "provide": { + "psr/http-message-implementation": "1.0" }, "require-dev": { - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8" }, "type": "library", "extra": { @@ -99,10 +155,10 @@ }, "autoload": { "psr-4": { - "GuzzleHttp\\Stream\\": "src/" + "GuzzleHttp\\Psr7\\": "src/" }, "files": [ - "src/functions.php" + "src/functions_include.php" ] }, "notification-url": "https://packagist.org/downloads/", @@ -114,15 +170,114 @@ "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" } ], - "description": "Provides a simple abstraction over streams of data (Guzzle 4+)", - "homepage": "http://guzzlephp.org/", + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "psr-7", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2018-12-04T20:46:45+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", "keywords": [ - "Guzzle", - "stream" + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + }, + { + "name": "ralouphie/getallheaders", + "version": "2.0.5", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/getallheaders.git", + "reference": "5601c8a83fbba7ef674a7369456d12f1e0d0eafa" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/5601c8a83fbba7ef674a7369456d12f1e0d0eafa", + "reference": "5601c8a83fbba7ef674a7369456d12f1e0d0eafa", + "shasum": "" + }, + "require": { + "php": ">=5.3" + }, + "require-dev": { + "phpunit/phpunit": "~3.7.0", + "satooshi/php-coveralls": ">=1.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/getallheaders.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" ], - "time": "2014-08-10 23:57:01" + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "A polyfill for getallheaders.", + "time": "2016-02-11T07:05:27+00:00" }, { "name": "twilio/sdk", @@ -130,25 +285,25 @@ "source": { "type": "git", "url": "https://github.com/twilio/twilio-php.git", - "reference": "a876df2cd5bc873beb450a56e6e410680a5b4e4c" + "reference": "e54f54597849344edd40f86e12b17c1ac815e65b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twilio/twilio-php/zipball/a876df2cd5bc873beb450a56e6e410680a5b4e4c", - "reference": "a876df2cd5bc873beb450a56e6e410680a5b4e4c", + "url": "https://api.github.com/repos/twilio/twilio-php/zipball/e54f54597849344edd40f86e12b17c1ac815e65b", + "reference": "e54f54597849344edd40f86e12b17c1ac815e65b", "shasum": "" }, "require": { - "php": ">=5.2.1" + "php": ">=5.5.0" }, "require-dev": { - "mockery/mockery": ">=0.7.2", - "phpunit/phpunit": "3.7.*" + "apigen/apigen": "^4.1", + "phpunit/phpunit": "4.5.*" }, "type": "library", "autoload": { - "psr-0": { - "Services_Twilio": "" + "psr-4": { + "Twilio\\": "Twilio/" } }, "license": [ @@ -156,12 +311,8 @@ ], "authors": [ { - "name": "Kevin Burke", - "email": "kevin@twilio.com" - }, - { - "name": "Kyle Conroy", - "email": "kyle+pear@twilio.com" + "name": "Twilio API Team", + "email": "api@twilio.com" } ], "description": "A PHP wrapper for Twilio's API", @@ -172,25 +323,25 @@ "twilio" ], "support": { - "source": "https://github.com/twilio/twilio-php/tree/master", + "source": "https://github.com/twilio/twilio-php/tree/5.30.2", "issues": "https://github.com/twilio/twilio-php/issues" }, - "time": "2014-07-15 17:50:20" + "time": "2019-03-15T20:38:12+00:00" } ], "packages-dev": [ { "name": "heroku/heroku-buildpack-php", - "version": "v33", + "version": "v153", "source": { "type": "git", "url": "https://github.com/heroku/heroku-buildpack-php.git", - "reference": "5b715c36a6e0d3d1c9803e6409256633cdadf05b" + "reference": "59b9c4daeebd70eadc4ae3d394429e3f357c56e8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/heroku/heroku-buildpack-php/zipball/5b715c36a6e0d3d1c9803e6409256633cdadf05b", - "reference": "5b715c36a6e0d3d1c9803e6409256633cdadf05b", + "url": "https://api.github.com/repos/heroku/heroku-buildpack-php/zipball/59b9c4daeebd70eadc4ae3d394429e3f357c56e8", + "reference": "59b9c4daeebd70eadc4ae3d394429e3f357c56e8", "shasum": "" }, "bin": [ @@ -211,7 +362,7 @@ } ], "description": "Toolkit for starting a PHP application locally, with or without foreman, using the same config for PHP/HHVM and Apache2/Nginx as on Heroku", - "homepage": "http://github.com/heroku/heroku-buildpack-php", + "homepage": "https://github.com/heroku/heroku-buildpack-php", "keywords": [ "apache", "apache2", @@ -221,21 +372,18 @@ "nginx", "php" ], - "time": "2014-08-07 21:25:54" + "time": "2019-03-19T00:24:16+00:00" } ], - "aliases": [ - - ], + "aliases": [], "minimum-stability": "stable", "stability-flags": { "twilio/sdk": 20 }, "prefer-stable": false, + "prefer-lowest": false, "platform": { "php": ">=5.3.8" }, - "platform-dev": [ - - ] + "platform-dev": [] } diff --git a/src/Pagerduty.php b/src/Pagerduty.php index ddc2c3f..b69c2d8 100644 --- a/src/Pagerduty.php +++ b/src/Pagerduty.php @@ -14,32 +14,28 @@ class Pagerduty { - const DEFAULT_TIMEZONE = 'Pacific/Auckland'; + const DEFAULT_TIMEZONE = 'Europe/Paris'; protected $APItoken; protected $URL; protected $httpClient; /** - * Constructor. Expects an API token and PagerDuty domain. + * Constructor. Expects a PagerDuty API token. * * @param string $APItoken - * @param string $domain * */ - public function __construct($APItoken, $domain) { + public function __construct($APItoken) { $this->APItoken = $APItoken; - $this->URL = "https://{$domain}.pagerduty.com/api/v1"; + $this->URL = "https://api.pagerduty.com"; $this->httpClient = new \GuzzleHttp\Client( - array('defaults' => - array('headers' => - array( - 'Content-Type' => 'application/json', - 'Authorization' => "Token token={$APItoken}" - ) - ) - ) + ['headers' => + [ 'Accept' => 'application/vnd.pagerduty+json;version=2', + 'Authorization' => "Token token=$APItoken" + ] + ] ); } @@ -62,7 +58,7 @@ public function getOncallUserForSchedule($scheduleID) $datetime->add(new DateInterval('PT1S')); $oneSecondLater = urlencode($datetime->format(DateTime::ISO8601)); - $pagerDutyScheduleURL = "{$this->URL}/schedules/{$scheduleID}?since={$now}&until={$oneSecondLater}"; + $pagerDutyScheduleURL = "{$this->URL}/schedules/{$scheduleID}?time_zone=UTC&since={$now}&until={$oneSecondLater}"; $userID = null; @@ -92,7 +88,11 @@ public function getUserDetails($userID) { // See http://developer.pagerduty.com/documentation/rest/users/show $pagerDutyUserURL = "{$this->URL}/users/{$userID}"; - $queryString = array('query' => array('include[]' => 'contact_methods')); + $queryString = [ + 'query' => [ + 'include[]' => 'contact_methods' + ] + ]; $response = $this->httpClient->get($pagerDutyUserURL, $queryString); @@ -102,14 +102,14 @@ public function getUserDetails($userID) $json = json_decode($response->getBody(), true); foreach($json['user']['contact_methods'] as $method) { - if($method['type'] == 'phone') { - $user = array( + if($method['type'] == 'phone_contact_method') { + $user = [ 'full_name' => $json['user']['name'], 'first_name' => $this->extractFirstName($json['user']['name']), 'local_time' => $this->getCurrentTimeForTimezone( $this->convertFriendlyTimezoneToFull($json['user']['time_zone'])), - 'phone_number' => "+{$method['country_code']}{$method['phone_number']}", - ); + 'phone_number' => "+{$method['country_code']}{$method['address']}", + ]; break; } } @@ -331,3 +331,4 @@ protected function convertFriendlyTimezoneToFull($tz) return (array_key_exists($tz, $timezones) ? $timezones[$tz] : null); } } +?> diff --git a/web/index.php b/web/index.php index 9b3d877..ff6fd27 100644 --- a/web/index.php +++ b/web/index.php @@ -16,31 +16,30 @@ // Set these Heroku config variables $scheduleID = getenv('PAGERDUTY_SCHEDULE_ID'); $APItoken = getenv('PAGERDUTY_API_TOKEN'); -$domain = getenv('PAGERDUTY_DOMAIN'); // Should we announce the local time of the on-call person? // (helps raise awareness you might be getting somebody out of bed) $announceTime = getenv('PHONEDUTY_ANNOUNCE_TIME'); -$pagerduty = new \Vend\Phoneduty\Pagerduty($APItoken, $domain); +$pagerduty = new \Vend\Phoneduty\Pagerduty($APItoken); $userID = $pagerduty->getOncallUserForSchedule($scheduleID); if (null !== $userID) { $user = $pagerduty->getUserDetails($userID); - $attributes = array( - 'voice' => 'alice', - 'language' => 'en-GB' - ); + $attributes = [ + 'voice' => 'man', + 'language' => 'en' + ]; $time = ""; if ($announceTime && $user['local_time']) { $time = sprintf("The current time in their timezone is %s.", $user['local_time']->format('g:ia')); } - $twilioResponse = new Services_Twilio_Twiml(); + $twilioResponse = new \Twilio\TwiML\VoiceResponse(); $response = sprintf("The current on-call engineer is %s. %s " . "Please hold while we connect you.", $user['first_name'], @@ -48,7 +47,7 @@ ); $twilioResponse->say($response, $attributes); - $twilioResponse->dial( $user['phone_number']); + $twilioResponse->dial($user['phone_number']); // send response if (!headers_sent()) { @@ -57,3 +56,4 @@ echo $twilioResponse; } +?>