Asking Alexa to deploy my @laravelphp Forge sites 😎
— Marcel Pociot (@marcelpociot) December 28, 2017
This is 12 lines of PHP with BotMan + Laravel 🙂 pic.twitter.com/d9knUQmvLC
$ composer require botman/botman
$ composer global require botman/installer
$ botman new my_awesome_chatbot
$ php artisan botman:driver-install facebook
$botman->hears('keyword', function($bot) {
$bot->reply('Hello my friend!');
});
$botman->hears('another keyword', 'My\Bot\Controllers@handle')
$botman->listen();
$botman->hears('tell me more', function($bot) {
$bot->reply('Hello my friend!');
$bot->reply('What do you want to know?');
});
$botman->group(['driver' => SlackDriver::class], function($botman) {
$botman->hears('I only listen on Slack', function($bot) { });
$botman->hears('Me too', function($bot) { });
});
$botman->group(['driver' => TelegramDriver::class], function($botman) {
$botman->hears('And I only listen on Telegram', function($bot) { });
});
$botman->hears('Call me {name}', function($bot, $name) {
$bot->reply('Hello '.$name);
});
$botman->hears('I am {name} the {adjective}', function($bot, $name, $adjective) {
$bot->reply('Hello '.$name.'. You truly are '.$adjective);
});
$botman->hears('I am ([0-9]+) years old', function($bot, $age) {
$bot->reply('Got it - your age is: '.$age);
});
$botman->receivesImages(function($bot, $images) {
//
});
$botman->receivesVideos(function($bot, $videos) {
//
});
$botman->receivesAudio(function($bot, $audio) {
//
});
$bot->receivesLocation(function($bot, Location $location) {
$lat = $location->getLatitude();
$lng = $location->getLongitude();
});
$botman->fallback(function($bot) {
$bot->reply('I have no idea what you are talking about!');
});
$botman->hears('I am ([0-9]+) years old', function($bot, $age) {
$bot->reply('Got it - your age is: '.$age);
$bot->userStorage()->save([
'age' => $age
]);
});
$botman->hears('How old am i', function($bot) {
$age = $bot->userStorage()->get('age');
$bot->reply('You are '.$age);
});
$bot->userStorage()->save([
'age' => $age
]);
$bot->channelStorage()->save([
'num_users' => $num
]);
$bot->driverStorage()->save([
'foo' => $bar
]);
// Slack
$botman->on('team_join', function($payload, $botman) {
$botman->reply('Hello!');
});
// Facebook
$botman->on('messaging_reads', function($payload, $botman) {
// Message was read.
});
// Telegram
$botman->on('left_chat_member', function($payload, $botman) {
$botman->reply('Goodbye '.$payload['username']);
});
$botman->hears('give me images', function($bot) {
$image = Image::url('http://php-conference-uk.test/img/botman.png');
$message = OutgoingMessage::create('Here is a nice image')
->withAttachment($image);
$bot->reply($message);
});
$botman->hears('give me videos', function($bot) {
$video = Video::url('http://php-conference-uk.test/video/botman.mp4');
$message = OutgoingMessage::create()->withAttachment($video);
$bot->reply($message);
});
$template = ListTemplate::create()
->addElement(
Element::create('BotMan Documentation')
->subtitle('Take a look at the BotMan documentation')
->image('http://botman.io/img/logo.png')
->addButton(ElementButton::create('Tell Me More')
->url('https://botman.io'))
)
->addElement(
Element::create('BotMan Studio')
->subtitle('Take a look at BotMan Studio')
->image('http://botman.io/img/logo.png')
->addButton(ElementButton::create('Visit')
->url('https://botman.io'))
);
$bot->reply($template);
$template = ButtonTemplate::create('Do you want to know more about BotMan?')
->addButton(
ElementButton::create('Tell me more')
->type('postback')
->payload('tellmemore')
)
->addButton(
ElementButton::create('Show me the docs')
->url('http://botman.io/')
);
$bot->reply($template);
$botman->hears('I want a ([^\s]+) ([^\s]+) (with [^\s]+)? delivered to ([^\s]+)', function(...))
$botman->hears('I want images', function($bot) {
$bot->ask('Which images?', function($answer, $bot) {
$text = $answer->getText();
$image = Image::url('http://lorempixel.com/400/200/'.$text.'/');
$message = OutgoingMessage::create()
->withAttachment($image);
$bot->say($message);
});
});
class PizzaConversation extends Conversation
{
public function run()
{
$this->ask('What Pizza size do you want?', function($answer) {
$this->size = $answer->getText();
});
}
}
$this->ask('What Pizza size do you want?', function($answer) {
$this->size = $answer->getText();
$this->askTopping();
});
public function askTopping()
{
$this->ask('What kind of topping do you want?', function($answer) {
$this->topping = $answer->getText();
$this->askAddress();
});
}
$this->ask('Where can we deliver your tasty pizza?', function($answer) {
$this->address = $answer->getText();
$this->say('Okay. That is all I need.');
$this->say('Size: '.$this->size);
$this->say('Topping: '.$this->topping);
$this->say('Delivery address: '.$this->address);
});
$botman->hears('I want pizza', function($bot) {
$bot->startConversation(new PizzaConversation);
});
// Better pizza
$question = Question::create('What Pizza size do you want?');
$question->addButtons([
Button::create('Supersize')->value('XXL'),
Button::create('Large')->value('L')
]);
$this->ask($question, function($answer) {
//
});
Hey Calendar, schedule dinner with Nicole at 8 pm tomorrow.
{
"intent": "create_meeting",
"entities": {
"name" : "Dinner with Nicole",
"invitees" : ["Nicole"],
"time": "2024-10-07 20:00:00"
}
}
$middleware = Dialogflow::create('my-dialogflow-token')->listenForAction();
$botman->middleware->received($middleware);
$botman->hears('order.pizza', function($bot) {
$extras = $bot->getMessage()->getExtras('apiParameters');
$bot->reply('Type: '.$extras['type']);
$bot->reply('Size: '.$extras['size']);
$bot->reply('Topping: '.implode($extras['topping'], ' '));
})->middleware($middleware);
$botman->say('This is your daily status update...', $userId);
$botman->startConversation(
new UserFeedbackConversation(),
$userId
);
class BotManTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->bot
->receives('test')
->assertReply('hello!');
}
}
$this->bot
->receives('Hi')
->assertReply('Hello')
->assertReplies(['Hello!', 'Nice to meet you'])
->assertReplyIsNot('Good bye')
->assertReplyNothing()
->assertQuestion()
->assertTemplate(ButtonTemplate::class);
$logger = new LoggerMiddleware();
$botman->middleware->sending($logger);
$botman->middleware->receiving($logger);