# About me **Marcel Pociot** Managing Partner at Beyond Code @marcelpociot
# Open Source ### 😍
# Open Source 30+ open source PHP packages / 920k+ downloads Laravel TestTools Chrome extension / ~15k weekly users Codeception TestTools Chrome extension

Chatbots

Definition

Chatbot

A conversational user interface to interact with your application

Bots are your friends

Automate deployments

Bots are your friends

DevOps

Bots are your friends

Brand marketing

Bots are your friends

Self-Service

Bots are your friends

Fun

The time is right

## 🎉 ## Let's build our own chatbot!

How does all of this work?

Hi!

Installation

Standalone


$ composer require botman/botman

Installation

BotMan Studio


$ composer global require botman/installer
$ botman new my_awesome_chatbot

Adding Drivers


$ php artisan botman:driver-install facebook

Configuration

Bot fundamentals

  1. Hear
  2. Process
  3. Respond

Hearing things


$botman->hears('keyword', function($bot) {
    $bot->reply('Hello my friend!');
});
$botman->hears('another keyword', 'My\Bot\Controllers@handle')

$botman->listen();

Hearing things

Hearing things

Hearing things


$botman->hears('tell me more', function($bot) {
    $bot->reply('Hello my friend!');
    $bot->reply('What do you want to know?');
});

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) { });
});

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);
});

Hearing things


$botman->hears('I am ([0-9]+) years old', function($bot, $age) {
    $bot->reply('Got it - your age is: '.$age);
});

Attachments

Listen for image uploads


$botman->receivesImages(function($bot, $images) {
    //
});

Attachments


$botman->receivesVideos(function($bot, $videos) {
    //
});

$botman->receivesAudio(function($bot, $audio) {
    //
});

$bot->receivesLocation(function($bot, Location $location) {
    $lat = $location->getLatitude();
    $lng = $location->getLongitude();
});

Hearing things

If nothing else matters matches


$botman->fallback(function($bot) {
    $bot->reply('I have no idea what you are talking about!');
});

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) {
    $age = $bot->userStorage()->get('age');
    $bot->reply('You are '.$age);
});

Remembering things


$bot->userStorage()->save([
    'age' => $age
]);

$bot->channelStorage()->save([
    'num_users' => $num
]);

$bot->driverStorage()->save([
    'foo' => $bar
]);

Driver events


// 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']);
});

Advanced Responses


$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);
});

Advanced Responses


$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);
});

Driver Specific Responses


$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);

Driver Specific Responses

Driver Specific Responses


$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);

Driver Specific Responses

Let's order some Pizza!

👨 - I want a large capricciosa with extra cheese delivered to The Brewery

$botman->hears('I want a ([^\s]+) ([^\s]+) (with [^\s]+)? delivered to ([^\s]+)', function(...))

Conversations


$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);
    });
});

Conversations


class PizzaConversation extends Conversation
{
    public function run()
    {
        $this->ask('What Pizza size do you want?', function($answer) {
            $this->size = $answer->getText();
        });
    }
}

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();
    });
}

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);
});

Conversations


$botman->hears('I want pizza', function($bot) {
    $bot->startConversation(new PizzaConversation);
});

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) {
    //
});

Questions

Questions

Natural Language Processing



Hey Calendar, schedule dinner with Nicole at 8 pm tomorrow.


{
  "intent": "create_meeting",
  "entities": {
    "name" : "Dinner with Nicole",
    "invitees" : ["Nicole"],
    "time": "2018-12-12 20:00:00"
  }
}

Natural Language Processing

Natural Language Processing

Natural Language Processing


$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);

Natural Language Processing

Originating Messages


$botman->say('This is your daily status update...', $userId);

$botman->startConversation(
    new UserFeedbackConversation(), 
    $userId
);

Testing

Testing


class BotManTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return  void
     */
    public function testBasicTest()
    {
        $this->bot
             ->receives('test')
             ->assertReply('hello!');
    }
}

Testing


$this->bot
    ->receives('Hi')

    ->assertReply('Hello')

    ->assertReplies(['Hello!', 'Nice to meet you'])

    ->assertReplyIsNot('Good bye')

    ->assertReplyNothing()

    ->assertQuestion()

    ->assertTemplate(ButtonTemplate::class);
    

Extending BotMan

Middleware

Middleware


$logger = new LoggerMiddleware();

$botman->middleware->sending($logger);
$botman->middleware->receiving($logger);

Extending BotMan

Drivers

  • Connect to your service
  • Use different protocols

Demo

botman.io

t.me/BotManTalkFeedbackBot

buildachatbot.io