Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BotMan - From Zero to Chatbot [Laravel Live]

BotMan - From Zero to Chatbot [Laravel Live]

Marcel Pociot

March 19, 2017
Tweet

More Decks by Marcel Pociot

Other Decks in Programming

Transcript

  1. Open Source 15 open source PHP packages / ~200k downloads

    Laravel TestTools Chrome extension / ~6k weekly users Codeception TestTools Chrome extension Rank 5 of top German PHP GitHub developers
  2. Hi!

  3. Configuration botman.io $botman = BotManFactory::create([ 'slack_token' => 'YOUR-SLACK-TOKEN-HERE', 'telegram_token' =>

    'YOUR-TELEGRAM-TOKEN-HERE', 'facebook_token' => 'YOUR-FACEBOOK-TOKEN-HERE', ]);
  4. Hearing things $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) { }); });
  5. Hearing things $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); });
  6. Remembering things $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) { $user = $bot->userStorage()->get(); $bot->reply('You are '.$user->get('age')); });
  7. Advanced Responses $botman->hears('give me images', function($bot) { $message = Message::create('Here

    is a nice image') ->image('http://laravel-live-india.dev/img/botman.png'); $bot->reply($message); });
  8. Advanced Responses $botman->hears('give me videos', function($bot) { $message = Message::create('Here

    is a nice image') ->video('http://laravel-live-india.dev/img/botman.mp4'); $bot->reply($message); });
  9. Let's order some Pizza! - I want a large capricciosa

    with extra cheese delivered to Example street 34 $botman->hears('I want a ([^\s]+) ([^\s]+) (with [^\s]+)? delivered to ([^\s]+)'
  10. Conversations $botman->hears('I want images', function($bot) { $bot->ask('Which images?', function($answer, $bot)

    { $text = $answer->getText(); $message = Message::create() ->image('http://lorempixel.com/400/200/'.$text.'/'); $bot->say($message); }); });
  11. Conversations class PizzaConversation extends Conversation { public function run() {

    $this->ask('What Pizza size do you want?', function($answer) { $this->size = $answer->getText(); }); } }
  12. Conversations $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(); }); }
  13. Conversations $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); });
  14. Questions // 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) { // });
  15. Natural Language Processing Hey Calendar, schedule dinner with Nicole at

    8 pm tomorrow. { "intent": "create_meeting", "entities": { "name" : "Dinner with Nicole", "invitees" : ["Nicole"], "time": "2017-04-11 20:00:00" } }
  16. Natural Language Processing $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(ApiAi::create('my-api-ai-token')->listenForAction());
  17. Extending BotMan Middleware class Authorization implements MiddlewareInterface { /** @var

    array */ protected $allowedUsers = [1,2,3]; public function isMessageMatching(Message $message, $test) { return in_array($message->getUser(), $this->allowedUsers); } }
  18. Extending BotMan Middleware class RegisterUsers implements MiddlewareInterface { public function

    handle(Message $message) { $user = User::firstOrCreate(['user_id' => $message->getUser()]); $message->addExtras('user', $user); } }
  19. Extending BotMan Drivers class FlockDriver implements DriverInterface { public function

    matchesRequest() { return true; } public function getMessages() { return [new Message('', '', '')]; } public function reply($message, $matchingMessage, $additionalParameters = [] { // }