Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Mar 7, 2018
0 parents commit 7fc051a
Show file tree
Hide file tree
Showing 14 changed files with 681 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea/
.discovery/
.DS_Store
composer.lock
.php_cs.cache
/vendor/
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: php

php:
- 7.0
- 7.1
- 7.2

before_script:
- travis_retry composer self-update --preview
- travis_retry composer install --prefer-dist --no-interaction

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml

before_install:
- pip install --user codecov
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

after_success:
- codecov
22 changes: 22 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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-kik).

## 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!*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# BotMan Hangouts Driver

BotMan driver to connect Hangouts Chat with [BotMan](https://github.com/botman/botman)

[![Latest Version on Packagist](https://img.shields.io/packagist/v/botman/driver-hangouts.svg?style=flat-square)](https://packagist.org/packages/botman/driver-hangouts)
[![Build Status](https://travis-ci.org/botman/driver-hangouts.svg?branch=master)](https://travis-ci.org/botman/driver-hangouts)
[![codecov](https://codecov.io/gh/botman/driver-hangouts/branch/master/graph/badge.svg)](https://codecov.io/gh/botman/driver-hangouts)

## 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 [email protected]. All security vulnerabilities will be promptly addressed.

## License

BotMan is free software distributed under the terms of the MIT license.

49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "botman/driver-hangouts",
"license": "MIT",
"description": "Google Hangouts driver for BotMan",
"keywords": [
"Bot",
"BotMan",
"Hangouts"
],
"homepage": "http://github.com/botman/driver-hangouts",
"authors": [
{
"name": "Marcel Pociot",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.0",
"botman/botman": "~2.0"
},
"require-dev": {
"botman/studio-addons": "~1.0",
"illuminate/contracts": "~5.5.0",
"phpunit/phpunit": "~5.0",
"mockery/mockery": "dev-master",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
"BotMan\\Drivers\\Hangouts\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"cs": "php-cs-fixer fix"
},
"extra": {
"laravel": {
"providers": [
"BotMan\\Drivers\\Hangouts\\Providers\\HangoutsServiceProvider"
]
}
}
}
8 changes: 8 additions & 0 deletions discovery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"botman/driver-config": [
"stubs/hangouts.php"
],
"botman/driver": [
"BotMan\\Drivers\\Hangouts\\HangoutsDriver"
]
}
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
186 changes: 186 additions & 0 deletions src/HangoutsDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

namespace BotMan\Drivers\Hangouts;

use BotMan\BotMan\Users\User;
use Illuminate\Support\Collection;
use BotMan\BotMan\Drivers\HttpDriver;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Question;
use Symfony\Component\HttpFoundation\Request;
use BotMan\BotMan\Messages\Attachments\Image;
use Symfony\Component\HttpFoundation\Response;
use BotMan\BotMan\Drivers\Events\GenericEvent;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\JsonResponse;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;

class HangoutsDriver extends HttpDriver
{
protected $messages = [];

const DRIVER_NAME = 'Hangouts';

/**
* @param Request $request
* @return void
*/
public function buildPayload(Request $request)
{
$this->payload = new ParameterBag(json_decode($request->getContent(), true) ?? []);
$this->event = Collection::make($this->payload->all());
$this->config = Collection::make($this->config->get('hangouts', []));
}

/**
* Determine if the request is for this driver.
*
* @return bool
*/
public function matchesRequest()
{
return $this->event->get('token') === $this->config->get('token') && $this->event->get('type') === 'MESSAGE';
}

/**
* @return bool|GenericEvent
*/
public function hasMatchingEvent()
{
$event = false;
$type = $this->event->get('type');

if ($type === 'ADDED_TO_SPACE' || $type === 'REMOVED_FROM_SPACE') {
$event = new GenericEvent($this->event->toArray());
$event->setName($this->event->get('type'));
}

return $event;
}

/**
* Retrieve the chat message(s).
*
* @return array
*/
public function getMessages()
{
if (empty($this->messages)) {
$this->loadMessages();
}

return $this->messages;
}

/**
* Load the messages from the incoming payload.
*/
protected function loadMessages()
{
$message = $this->event->get('message');

$text = $message['text'];

if ($this->config->get('strip_annotations') === true && isset($message['annotations'])) {
$start = $message['annotations'][0]['startIndex'];
$length = $message['annotations'][0]['length'];
$text = substr($text, $start + $length + 1);
}

$this->messages = [new IncomingMessage($text, $message['sender']['name'], $message['name'], $this->event->toArray())];
}

/**
* @return bool
*/
public function isConfigured()
{
return ! empty($this->config->get('token'));
}

/**
* Retrieve User information.
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage
* @return User
*/
public function getUser(IncomingMessage $matchingMessage)
{
$payload = $matchingMessage->getPayload();

$user = $payload['message']['sender'];

return new User($user['name'], null, null, $user['displayName'], $user);
}

/**
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message
* @return Answer
*/
public function getConversationAnswer(IncomingMessage $message)
{
return Answer::create($message->getText())->setMessage($message);
}

/**
* @param OutgoingMessage|\BotMan\BotMan\Messages\Outgoing\Question $message
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage
* @param array $additionalParameters
* @return array
*/
public function buildServicePayload($message, $matchingMessage, $additionalParameters = [])
{
$payload = [
'text' => '',
'cards' => [
[
'sections' => [
[
'widgets' => []
]
]
]
]
];
if ($message instanceof OutgoingMessage) {
$text = $message->getText();

$payload['text'] = $text;

$attachment = $message->getAttachment();
if (! is_null($attachment) && $attachment instanceof Image) {
$payload['cards'][0]['sections'][0]['widgets'][] = [
'image' => [
'imageUrl' => $attachment->getUrl()
]
];
}
} elseif ($message instanceof Question) {
$payload['text'] = $message->getText();
}

return $payload;
}

/**
* @param mixed $payload
* @return Response
*/
public function sendPayload($payload)
{
return JsonResponse::create($payload)->send();
}

/**
* 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)
{
//
}
}
Loading

0 comments on commit 7fc051a

Please sign in to comment.