Skip to content

Commit bba20f1

Browse files
committed
initial commit
0 parents  commit bba20f1

File tree

7 files changed

+347
-0
lines changed

7 files changed

+347
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea/
2+
.DS_Store
3+
composer.lock
4+
.php_cs.cache
5+
/vendor/

.styleci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
preset: laravel
2+
3+
enabled:
4+
- unalign_double_arrow
5+
6+
linting: true

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# BotMan Tinker
2+
3+
Gives your Laravel chatbot the ability to try your chatbot in your local terminal.
4+
5+
## Installation
6+
7+
Run `composer require mpociot/botman-tinker` to install the composer dependencies.
8+
9+
Then in your `config/app.php` add
10+
11+
```php
12+
Mpociot\BotManTinker\BotManTinkerServiceProvider::class,
13+
```
14+
15+
to the `providers` array.
16+
17+
## Usage
18+
19+
You now have a new Artisan command that helps you to test and develop your chatbot locally:
20+
21+
```php
22+
php artisan botman:tinker
23+
```
24+
25+
## About BotMan
26+
27+
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).
28+
29+
```php
30+
$botman->hears('I want cross-platform bots with PHP!', function (BotMan $bot) {
31+
$bot->reply('Look no further!');
32+
});
33+
```
34+
35+
## Documentation
36+
37+
You can find the BotMan documentation at [http://botman.io](http://botman.io).
38+
39+
## Contributing
40+
41+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
42+
43+
## Security Vulnerabilities
44+
45+
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.
46+
47+
## License
48+
49+
BotMan Tinker is free software distributed under the terms of the MIT license.

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "mpociot/botman-tinker",
3+
"license": "MIT",
4+
"description": "BotMan tinker command for your Laravel BotMan project",
5+
"keywords": [
6+
"Bot",
7+
"BotMan",
8+
"Laravel",
9+
"Tinker"
10+
],
11+
"homepage": "http://github.com/mpociot/botman-tinker",
12+
"authors": [
13+
{
14+
"name": "Marcel Pociot",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.6.0",
20+
"mpociot/botman": "dev-master",
21+
"clue/stdio-react": "^1.0",
22+
"mpociot/slack-client": "^0.2.6",
23+
"illuminate/support": "~5.0"
24+
},
25+
"require-dev": {
26+
"orchestra/testbench": "~3.0",
27+
"phpunit/phpunit": "~5.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Mpociot\\BotManTinker\\": "src/"
32+
}
33+
}
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Mpociot\BotManTinker;
3+
4+
use Illuminate\Support\ServiceProvider;
5+
use Mpociot\BotManTinker\Commands\BotManTinker;
6+
7+
class BotManTinkerServiceProvider extends ServiceProvider
8+
{
9+
10+
public function register()
11+
{
12+
$this->commands([
13+
BotManTinker::class
14+
]);
15+
}
16+
17+
}

src/Commands/BotManTinker.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Mpociot\BotManTinker\Commands;
4+
5+
use Clue\React\Stdio\Stdio;
6+
use React\EventLoop\Factory;
7+
use Illuminate\Console\Command;
8+
use Mpociot\BotMan\BotManFactory;
9+
use Mpociot\BotMan\Cache\ArrayCache;
10+
use Mpociot\BotManTinker\Drivers\ConsoleDriver;
11+
12+
class BotManTinker extends Command
13+
{
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'botman:tinker';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Tinker around with BotMan.';
27+
28+
/**
29+
* Create a new command instance.
30+
*
31+
* @return void
32+
*/
33+
public function __construct()
34+
{
35+
parent::__construct();
36+
}
37+
38+
/**
39+
* Execute the console command.
40+
*
41+
* @return mixed
42+
*/
43+
public function handle()
44+
{
45+
/** @var \Illuminate\Foundation\Application $app */
46+
$app = app('app');
47+
$loop = Factory::create();
48+
49+
$app->singleton('botman', function ($app) use ($loop) {
50+
$config = config('services.botman', []);
51+
$botman = BotManFactory::create($config, new ArrayCache());
52+
53+
$stdio = new Stdio($loop);
54+
$stdio->getReadline()->setPrompt('You: ');
55+
56+
$botman->setDriver(new ConsoleDriver($config, $stdio));
57+
58+
$stdio->on('line', function ($line) use ($botman) {
59+
$botman->listen();
60+
});
61+
62+
return $botman;
63+
});
64+
65+
if (file_exists('routes/botman.php')) {
66+
require base_path('routes/botman.php');
67+
}
68+
69+
$loop->run();
70+
}
71+
}

src/Drivers/ConsoleDriver.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
namespace Mpociot\BotManTinker\Drivers;
4+
5+
use Mpociot\BotMan\User;
6+
use Mpociot\BotMan\Answer;
7+
use Mpociot\BotMan\Message;
8+
use Clue\React\Stdio\Stdio;
9+
use Mpociot\BotMan\Question;
10+
use Illuminate\Support\Collection;
11+
use Mpociot\BotMan\Interfaces\DriverInterface;
12+
use Mpociot\BotMan\Messages\Message as IncomingMessage;
13+
14+
class ConsoleDriver implements DriverInterface
15+
{
16+
/** @var string */
17+
protected $message;
18+
19+
/** @var Stdio */
20+
protected $client;
21+
22+
/** @var string */
23+
protected $bot_id;
24+
25+
/** @var boolean */
26+
protected $hasQuestion = false;
27+
28+
/** @var array */
29+
protected $lastQuestions;
30+
31+
const DRIVER_NAME = 'SlackRTM';
32+
33+
const BOT_NAME = 'BotMan';
34+
35+
/**
36+
* Driver constructor.
37+
* @param array $config
38+
* @param Stdio $client
39+
*/
40+
public function __construct(array $config, Stdio $client)
41+
{
42+
$this->event = Collection::make();
43+
$this->config = Collection::make($config);
44+
$this->client = $client;
45+
46+
$this->client->on('line', function ($line) {
47+
$this->message = $line;
48+
});
49+
}
50+
51+
/**
52+
* Return the driver name.
53+
*
54+
* @return string
55+
*/
56+
public function getName()
57+
{
58+
return self::DRIVER_NAME;
59+
}
60+
61+
/**
62+
* Determine if the request is for this driver.
63+
*
64+
* @return bool
65+
*/
66+
public function matchesRequest()
67+
{
68+
return false;
69+
}
70+
71+
/**
72+
* @param Message $message
73+
* @return Answer
74+
*/
75+
public function getConversationAnswer(Message $message)
76+
{
77+
$index = (int)$message->getMessage() - 1;
78+
79+
if ($this->hasQuestion && isset($this->lastQuestions[$index])) {
80+
$question = $this->lastQuestions[$index];
81+
return Answer::create($question['name'])
82+
->setInteractiveReply(true)
83+
->setValue($question['value'])
84+
->setMessage($message);
85+
}
86+
return Answer::create($this->message)->setMessage($message);
87+
}
88+
89+
/**
90+
* Retrieve the chat message.
91+
*
92+
* @return array
93+
*/
94+
public function getMessages()
95+
{
96+
return [new Message($this->message, 999, '#channel', $this->message)];
97+
}
98+
99+
/**
100+
* @return bool
101+
*/
102+
public function isBot()
103+
{
104+
return strpos($this->message, 'BotMan: ') === 0;
105+
}
106+
107+
/**
108+
* @param string|Question|IncomingMessage $message
109+
* @param Message $matchingMessage
110+
* @param array $additionalParameters
111+
* @return $this
112+
*/
113+
public function reply($message, $matchingMessage, $additionalParameters = [])
114+
{
115+
$questionData = null;
116+
if ($message instanceof IncomingMessage) {
117+
$text = $message->getMessage();
118+
} elseif ($message instanceof Question) {
119+
$text = $message->getText();
120+
$questionData = $message->toArray();
121+
} else {
122+
$text = $message;
123+
}
124+
125+
$this->client->writeln(self::BOT_NAME.': '.$text);
126+
127+
if (!is_null($questionData)) {
128+
foreach ($questionData['actions'] as $key => $action) {
129+
$this->client->writeln(($key+1).') '.$action['text']);
130+
}
131+
$this->hasQuestion = true;
132+
$this->lastQuestions = $questionData['actions'];
133+
}
134+
135+
return $this;
136+
}
137+
138+
/**
139+
* Send a typing indicator.
140+
* @param Message $matchingMessage
141+
* @return mixed
142+
*/
143+
public function types(Message $matchingMessage)
144+
{
145+
$this->client->writeln(self::BOT_NAME.': ...');
146+
}
147+
148+
/**
149+
* Retrieve User information.
150+
* @param Message $matchingMessage
151+
* @return User
152+
*/
153+
public function getUser(Message $matchingMessage)
154+
{
155+
return new User($matchingMessage->getUser());
156+
}
157+
158+
/**
159+
* @return bool
160+
*/
161+
public function isConfigured()
162+
{
163+
return false;
164+
}
165+
}

0 commit comments

Comments
 (0)