-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bba20f1
Showing
7 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea/ | ||
.DS_Store | ||
composer.lock | ||
.php_cs.cache | ||
/vendor/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
preset: laravel | ||
|
||
enabled: | ||
- unalign_double_arrow | ||
|
||
linting: true |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# BotMan Tinker | ||
|
||
Gives your Laravel chatbot the ability to try your chatbot in your local terminal. | ||
|
||
## Installation | ||
|
||
Run `composer require mpociot/botman-tinker` to install the composer dependencies. | ||
|
||
Then in your `config/app.php` add | ||
|
||
```php | ||
Mpociot\BotManTinker\BotManTinkerServiceProvider::class, | ||
``` | ||
|
||
to the `providers` array. | ||
|
||
## Usage | ||
|
||
You now have a new Artisan command that helps you to test and develop your chatbot locally: | ||
|
||
```php | ||
php artisan botman:tinker | ||
``` | ||
|
||
## About BotMan | ||
|
||
BotMan is a framework agnostic PHP library that is designed to simplify the task of developing innovative bots for multiple messaging platforms, including [Slack](http://slack.com), [Telegram](http://telegram.me), [Microsoft Bot Framework](https://dev.botframework.com/), [Nexmo](https://nexmo.com), [HipChat](http://hipchat.com), [Facebook Messenger](http://messenger.com) and [WeChat](http://web.wechat.com). | ||
|
||
```php | ||
$botman->hears('I want cross-platform bots with PHP!', function (BotMan $bot) { | ||
$bot->reply('Look no further!'); | ||
}); | ||
``` | ||
|
||
## Documentation | ||
|
||
You can find the BotMan documentation at [http://botman.io](http://botman.io). | ||
|
||
## 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 Tinker is free software distributed under the terms of the MIT license. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "mpociot/botman-tinker", | ||
"license": "MIT", | ||
"description": "BotMan tinker command for your Laravel BotMan project", | ||
"keywords": [ | ||
"Bot", | ||
"BotMan", | ||
"Laravel", | ||
"Tinker" | ||
], | ||
"homepage": "http://github.com/mpociot/botman-tinker", | ||
"authors": [ | ||
{ | ||
"name": "Marcel Pociot", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6.0", | ||
"mpociot/botman": "dev-master", | ||
"clue/stdio-react": "^1.0", | ||
"mpociot/slack-client": "^0.2.6", | ||
"illuminate/support": "~5.0" | ||
}, | ||
"require-dev": { | ||
"orchestra/testbench": "~3.0", | ||
"phpunit/phpunit": "~5.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Mpociot\\BotManTinker\\": "src/" | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Mpociot\BotManTinker; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use Mpociot\BotManTinker\Commands\BotManTinker; | ||
|
||
class BotManTinkerServiceProvider extends ServiceProvider | ||
{ | ||
|
||
public function register() | ||
{ | ||
$this->commands([ | ||
BotManTinker::class | ||
]); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Mpociot\BotManTinker\Commands; | ||
|
||
use Clue\React\Stdio\Stdio; | ||
use React\EventLoop\Factory; | ||
use Illuminate\Console\Command; | ||
use Mpociot\BotMan\BotManFactory; | ||
use Mpociot\BotMan\Cache\ArrayCache; | ||
use Mpociot\BotManTinker\Drivers\ConsoleDriver; | ||
|
||
class BotManTinker extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'botman:tinker'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Tinker around with BotMan.'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
/** @var \Illuminate\Foundation\Application $app */ | ||
$app = app('app'); | ||
$loop = Factory::create(); | ||
|
||
$app->singleton('botman', function ($app) use ($loop) { | ||
$config = config('services.botman', []); | ||
$botman = BotManFactory::create($config, new ArrayCache()); | ||
|
||
$stdio = new Stdio($loop); | ||
$stdio->getReadline()->setPrompt('You: '); | ||
|
||
$botman->setDriver(new ConsoleDriver($config, $stdio)); | ||
|
||
$stdio->on('line', function ($line) use ($botman) { | ||
$botman->listen(); | ||
}); | ||
|
||
return $botman; | ||
}); | ||
|
||
if (file_exists('routes/botman.php')) { | ||
require base_path('routes/botman.php'); | ||
} | ||
|
||
$loop->run(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
namespace Mpociot\BotManTinker\Drivers; | ||
|
||
use Mpociot\BotMan\User; | ||
use Mpociot\BotMan\Answer; | ||
use Mpociot\BotMan\Message; | ||
use Clue\React\Stdio\Stdio; | ||
use Mpociot\BotMan\Question; | ||
use Illuminate\Support\Collection; | ||
use Mpociot\BotMan\Interfaces\DriverInterface; | ||
use Mpociot\BotMan\Messages\Message as IncomingMessage; | ||
|
||
class ConsoleDriver implements DriverInterface | ||
{ | ||
/** @var string */ | ||
protected $message; | ||
|
||
/** @var Stdio */ | ||
protected $client; | ||
|
||
/** @var string */ | ||
protected $bot_id; | ||
|
||
/** @var boolean */ | ||
protected $hasQuestion = false; | ||
|
||
/** @var array */ | ||
protected $lastQuestions; | ||
|
||
const DRIVER_NAME = 'SlackRTM'; | ||
|
||
const BOT_NAME = 'BotMan'; | ||
|
||
/** | ||
* Driver constructor. | ||
* @param array $config | ||
* @param Stdio $client | ||
*/ | ||
public function __construct(array $config, Stdio $client) | ||
{ | ||
$this->event = Collection::make(); | ||
$this->config = Collection::make($config); | ||
$this->client = $client; | ||
|
||
$this->client->on('line', function ($line) { | ||
$this->message = $line; | ||
}); | ||
} | ||
|
||
/** | ||
* Return the driver name. | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return self::DRIVER_NAME; | ||
} | ||
|
||
/** | ||
* Determine if the request is for this driver. | ||
* | ||
* @return bool | ||
*/ | ||
public function matchesRequest() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* @param Message $message | ||
* @return Answer | ||
*/ | ||
public function getConversationAnswer(Message $message) | ||
{ | ||
$index = (int)$message->getMessage() - 1; | ||
|
||
if ($this->hasQuestion && isset($this->lastQuestions[$index])) { | ||
$question = $this->lastQuestions[$index]; | ||
return Answer::create($question['name']) | ||
->setInteractiveReply(true) | ||
->setValue($question['value']) | ||
->setMessage($message); | ||
} | ||
return Answer::create($this->message)->setMessage($message); | ||
} | ||
|
||
/** | ||
* Retrieve the chat message. | ||
* | ||
* @return array | ||
*/ | ||
public function getMessages() | ||
{ | ||
return [new Message($this->message, 999, '#channel', $this->message)]; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isBot() | ||
{ | ||
return strpos($this->message, 'BotMan: ') === 0; | ||
} | ||
|
||
/** | ||
* @param string|Question|IncomingMessage $message | ||
* @param Message $matchingMessage | ||
* @param array $additionalParameters | ||
* @return $this | ||
*/ | ||
public function reply($message, $matchingMessage, $additionalParameters = []) | ||
{ | ||
$questionData = null; | ||
if ($message instanceof IncomingMessage) { | ||
$text = $message->getMessage(); | ||
} elseif ($message instanceof Question) { | ||
$text = $message->getText(); | ||
$questionData = $message->toArray(); | ||
} else { | ||
$text = $message; | ||
} | ||
|
||
$this->client->writeln(self::BOT_NAME.': '.$text); | ||
|
||
if (!is_null($questionData)) { | ||
foreach ($questionData['actions'] as $key => $action) { | ||
$this->client->writeln(($key+1).') '.$action['text']); | ||
} | ||
$this->hasQuestion = true; | ||
$this->lastQuestions = $questionData['actions']; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Send a typing indicator. | ||
* @param Message $matchingMessage | ||
* @return mixed | ||
*/ | ||
public function types(Message $matchingMessage) | ||
{ | ||
$this->client->writeln(self::BOT_NAME.': ...'); | ||
} | ||
|
||
/** | ||
* Retrieve User information. | ||
* @param Message $matchingMessage | ||
* @return User | ||
*/ | ||
public function getUser(Message $matchingMessage) | ||
{ | ||
return new User($matchingMessage->getUser()); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isConfigured() | ||
{ | ||
return false; | ||
} | ||
} |