From e5bdd02fa2bea34f9014f8b76f224773696ba095 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Wed, 19 Jul 2017 11:19:04 +0200 Subject: [PATCH] Initial commit --- .gitattributes | 12 ++ .gitignore | 5 + .php_cs | 8 ++ .styleci.yml | 6 + .travis.yml | 19 +++ CONTRIBUTING.md | 22 ++++ LICENSE | 21 ++++ README.md | 16 +++ composer.json | 42 +++++++ discovery.json | 8 ++ phpunit.xml | 23 ++++ src/HipChatDriver.php | 158 ++++++++++++++++++++++++ stubs/hipchat.php | 15 +++ tests/HipChatDriverTest.php | 232 ++++++++++++++++++++++++++++++++++++ 14 files changed, 587 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .php_cs create mode 100644 .styleci.yml create mode 100644 .travis.yml create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 discovery.json create mode 100644 phpunit.xml create mode 100644 src/HipChatDriver.php create mode 100644 stubs/hipchat.php create mode 100644 tests/HipChatDriverTest.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..4b17e1c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,12 @@ +* text=auto + +/.github export-ignore +/tests export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.php_cs export-ignore +.styleci.yml export-ignore +.travis.yml export-ignore +phpunit.xml export-ignore +CHANGELOG.md export-ignore +CONTRIBUTING.md export-ignore \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb0edf3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea/ +.DS_Store +composer.lock +.php_cs.cache +/vendor/ \ No newline at end of file diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..1327dc4 --- /dev/null +++ b/.php_cs @@ -0,0 +1,8 @@ +> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini + +after_success: + - codecov \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..66f7971 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,22 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. We accept contributions via Pull Requests on [Github](https://github.com/botman/driver-facebook). + +## Pull Requests + +- **[PSR-2 Coding Standard.](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** The easiest way to apply the conventions is to install [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). +- **Add tests!** Your patch won't be accepted if it doesn't have tests. +- **Document any change in behaviour.** Make sure the `README.md` and any other relevant documentation are kept up-to-date. +- **Consider our release cycle.** We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. +- **Create feature branches.** Don't ask us to pull from your master branch. +- **One pull request per feature.** If you want to do more than one thing, send multiple pull requests. +- **Send coherent history.** Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + +## Running Tests + +```bash +$ phpunit +``` + + +*Happy coding!* \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cae2fc5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Marcel Pociot + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2a5b94c --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# BotMan HipChat Driver + +BotMan driver to connect HipChat with [BotMan](https://github.com/botman/botman) + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +If you discover a security vulnerability within BotMan, please send an e-mail to Marcel Pociot at m.pociot@gmail.com. All security vulnerabilities will be promptly addressed. + +## License + +BotMan is free software distributed under the terms of the MIT license. + \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7541b04 --- /dev/null +++ b/composer.json @@ -0,0 +1,42 @@ +{ + "name": "botman/driver-hipchat", + "license": "MIT", + "description": "HipChat driver for BotMan", + "keywords": [ + "Bot", + "BotMan", + "HipChat" + ], + "homepage": "http://github.com/botman/driver-hipchat", + "authors": [ + { + "name": "Marcel Pociot", + "email": "m.pociot@gmail.com" + } + ], + "require": { + "php": ">=7.0", + "mpociot/botman": "~2.0" + }, + "require-dev": { + "phpunit/phpunit": "~5.0", + "mockery/mockery": "dev-master", + "ext-curl": "*" + }, + "autoload": { + "psr-4": { + "BotMan\\Drivers\\HipChat\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "test": "vendor/bin/phpunit", + "cs": "php-cs-fixer fix" + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/discovery.json b/discovery.json new file mode 100644 index 0000000..8b7753a --- /dev/null +++ b/discovery.json @@ -0,0 +1,8 @@ +{ + "botman/driver-config": [ + "stubs/hipchat.php" + ], + "botman/driver": [ + "BotMan\\Drivers\\HipChat\\HipChatDriver" + ] +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..2ba9eaf --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,23 @@ + + + + + tests/ + + + + + src/ + + + \ No newline at end of file diff --git a/src/HipChatDriver.php b/src/HipChatDriver.php new file mode 100644 index 0000000..936fd7c --- /dev/null +++ b/src/HipChatDriver.php @@ -0,0 +1,158 @@ +payload = new ParameterBag((array) json_decode($request->getContent(), true)); + $this->event = Collection::make($this->payload->get('item')); + $this->config = Collection::make($this->config->get('hipchat', [])); + } + + /** + * Determine if the request is for this driver. + * + * @return bool + */ + public function matchesRequest() + { + return ! is_null($this->payload->get('webhook_id')) && $this->payload->get('event') === 'room_message'; + } + + /** + * @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message + * @return Answer + */ + public function getConversationAnswer(IncomingMessage $message) + { + return Answer::create($message->getText())->setMessage($message); + } + + /** + * Retrieve the chat message. + * + * @return array + */ + public function getMessages() + { + return [ + new IncomingMessage($this->event->get('message')['message'], $this->event->get('message')['from']['id'], + $this->event->get('room')['id'], $this->event), + ]; + } + + /** + * @return bool + */ + public function isBot() + { + return false; + } + + /** + * @param string|Question|IncomingMessage $message + * @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage + * @param array $additionalParameters + * @return Response|null + */ + public function buildServicePayload($message, $matchingMessage, $additionalParameters = []) + { + $parameters = array_merge_recursive([ + 'message_format' => 'text', + ], $additionalParameters); + /* + * If we send a Question with buttons, ignore + * the text and append the question. + */ + if ($message instanceof Question) { + $parameters['message'] = $message->getText(); + } elseif ($message instanceof OutgoingMessage) { + $parameters['message'] = $message->getText(); + } else { + $parameters['message'] = $message; + } + + $this->apiURL = Collection::make($this->config->get('urls', []))->filter(function ($url) use ( + $matchingMessage + ) { + return strstr($url, 'room/'.$matchingMessage->getRecipient().'/notification'); + })->first(); + + return $parameters; + } + + /** + * @param mixed $payload + * @return Response + */ + public function sendPayload($payload) + { + $headers = [ + 'Content-Type:application/json', + ]; + + if (! is_null($this->apiURL)) { + return $this->http->post($this->apiURL, [], $payload, $headers, true); + } + } + + /** + * @return bool + */ + public function isConfigured() + { + $urls = $this->config->get('urls'); + + if (is_array($urls)) { + $urls = array_filter($urls); + } + + return ! empty($urls); + } + + /** + * Retrieve User information. + * @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage + * @return User + */ + public function getUser(IncomingMessage $matchingMessage) + { + $payload = $matchingMessage->getPayload(); + + return new User($payload->get('message')['from']['id'], $payload->get('message')['from']['name'], null, + $payload->get('message')['from']['mention_name']); + } + + /** + * Low-level method to perform driver specific API requests. + * + * @param string $endpoint + * @param array $parameters + * @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage + * @return void + */ + public function sendRequest($endpoint, array $parameters, IncomingMessage $matchingMessage) + { + // + } +} diff --git a/stubs/hipchat.php b/stubs/hipchat.php new file mode 100644 index 0000000..fcf082c --- /dev/null +++ b/stubs/hipchat.php @@ -0,0 +1,15 @@ + env('HIPCHAT_URLS'), + +]; \ No newline at end of file diff --git a/tests/HipChatDriverTest.php b/tests/HipChatDriverTest.php new file mode 100644 index 0000000..918d6b9 --- /dev/null +++ b/tests/HipChatDriverTest.php @@ -0,0 +1,232 @@ +shouldReceive('getContent')->andReturn(json_encode($responseData)); + if ($htmlInterface === null) { + $htmlInterface = m::mock(Curl::class); + } + + return new HipChatDriver($request, [], $htmlInterface); + } + + /** @test */ + public function it_returns_the_driver_name() + { + $driver = $this->getDriver([]); + $this->assertSame('HipChat', $driver->getName()); + } + + /** @test */ + public function it_matches_the_request() + { + $driver = $this->getDriver([ + 'to' => '41766013098', + 'messageId' => '0C000000075069C7', + 'text' => 'Hi Julia', + 'type' => 'text', + 'keyword' => 'HEY', + 'message_timestamp' => '2016-11-30 19:27:46', + ]); + $this->assertFalse($driver->matchesRequest()); + + $driver = $this->getDriver([ + 'event' => 'room_message', + 'item' => [ + 'message' => [ + 'from' => [ + 'id' => '12345', + ], + 'message' => 'Hi Julia', + ], + 'room' => [ + 'id' => '98765', + ], + ], + 'webhook_id' => '11223344', + ]); + $this->assertTrue($driver->matchesRequest()); + } + + /** @test */ + public function it_returns_the_message_object() + { + $driver = $this->getDriver([ + 'event' => 'room_message', + 'item' => [ + 'message' => [ + 'from' => [ + 'id' => '12345', + ], + 'message' => 'Hi Julia', + ], + 'room' => [ + 'id' => '98765', + ], + ], + 'webhook_id' => '11223344', + ]); + $this->assertTrue(is_array($driver->getMessages())); + } + + /** @test */ + public function it_returns_the_user_object() + { + $driver = $this->getDriver([ + 'event' => 'room_message', + 'item' => [ + 'message' => [ + 'from' => [ + 'id' => '12345', + 'name' => 'Marcel', + 'mention_name' => 'Marcel_Pociot', + ], + 'message' => 'Hi Julia', + ], + 'room' => [ + 'id' => '98765', + ], + ], + 'webhook_id' => '11223344', + ]); + + $message = $driver->getMessages()[0]; + $user = $driver->getUser($message); + + $this->assertSame($user->getId(), '12345'); + $this->assertSame($user->getFirstName(), 'Marcel'); + $this->assertNull($user->getLastName()); + $this->assertSame($user->getUsername(), 'Marcel_Pociot'); + } + + /** @test */ + public function it_returns_the_message_text() + { + $driver = $this->getDriver([ + 'event' => 'room_message', + 'item' => [ + 'message' => [ + 'from' => [ + 'id' => '12345', + ], + 'message' => 'Hi Julia', + ], + 'room' => [ + 'id' => '98765', + ], + ], + 'webhook_id' => '11223344', + ]); + $this->assertSame('Hi Julia', $driver->getMessages()[0]->getText()); + } + + /** @test */ + public function it_detects_bots() + { + $driver = $this->getDriver([ + 'event' => 'room_message', + 'item' => [ + 'message' => [ + 'from' => [ + 'id' => '12345', + ], + 'message' => 'Hi Julia', + ], + 'room' => [ + 'id' => '98765', + ], + ], + 'webhook_id' => '11223344', + ]); + $this->assertFalse($driver->isBot()); + } + + /** @test */ + public function it_returns_the_user_id() + { + $driver = $this->getDriver([ + 'event' => 'room_message', + 'item' => [ + 'message' => [ + 'from' => [ + 'id' => '12345', + ], + 'message' => 'Hi Julia', + ], + 'room' => [ + 'id' => '98765', + ], + ], + 'webhook_id' => '11223344', + ]); + $this->assertSame('12345', $driver->getMessages()[0]->getSender()); + } + + /** @test */ + public function it_returns_the_channel_id() + { + $driver = $this->getDriver([ + 'event' => 'room_message', + 'item' => [ + 'message' => [ + 'from' => [ + 'id' => '12345', + ], + 'message' => 'Hi Julia', + ], + 'room' => [ + 'id' => '98765', + ], + ], + 'webhook_id' => '11223344', + ]); + $this->assertSame('98765', $driver->getMessages()[0]->getRecipient()); + } + + /** @test */ + public function it_is_configured() + { + $request = m::mock(Request::class.'[getContent]'); + $request->shouldReceive('getContent')->andReturn(''); + $htmlInterface = m::mock(Curl::class); + + $driver = new HipChatDriver($request, [ + 'hipchat' => [ + 'urls' => ['1', '2'], + ] + ], $htmlInterface); + + $this->assertTrue($driver->isConfigured()); + + $driver = new HipChatDriver($request, [ + 'hipchat' => [ + 'urls' => [], + ] + ], $htmlInterface); + + $this->assertFalse($driver->isConfigured()); + + $driver = new HipChatDriver($request, [], $htmlInterface); + + $this->assertFalse($driver->isConfigured()); + + $driver = new HipChatDriver($request, [ + 'hipchat' => [ + 'urls' => [''], + ] + ], $htmlInterface); + + $this->assertFalse($driver->isConfigured()); + } +}