$ 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://laracon-eu-2017.marcelpociot.de/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://laracon-eu-2017.marcelpociot.de/video/pickle.mp4');
$message = OutgoingMessage::create()->withAttachment($video);
$bot->reply($message);
});
$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);
});
// Keyword: "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": "2025-05-16 20:00:00"
}
}
$middleware = ApiAi::create('my-api-ai-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!');
}
}
class BotManTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testConversationBasicTest()
{
$this->bot
->receives('Start Conversation')
->assertQuestion('Huh - you woke me up. What do you need?')
->receivesInteractiveMessage('quote')
->assertReplyIn(['an apple a day keeps the doctor away']);
}
}
$logger = new LoggerMiddleware();
$botman->middleware->receiving($logger);
$botman->middleware->sending($logger);