4. Do czego możemy je wykorzystać?
- Automatyczne/interaktywne deploymenty
- Działania DevOps
- Wsparcie sprzedaży (E-commerce)
- Wsparcie działu obsługi klienta
- Brand marketing
- Promocja bloga/treści
- Usługi typu self-service
- Fun
- … ?
13. Usłysz i odpowiedz...
plik botman.php w katalogu routes
$botman->hears('Kto dziś wygra mecz?', function ($bot) {
$bot->reply('LECH!');
});
// Process incoming message
$botman->listen();
14. Przesłanie do kontrolera
$botman->hears('Kto dziś wygra mecz?', 'SomeNamespaceLechController@respondAboutWinningToday');
class LechController {
public function respondAboutWinningToday($bot) {
$bot->reply('Oczywiście, że KKS!');
}
}
15. Parametry i wyrażenia regularne
$botman->hears('lubię {team}', function ($bot, $team) {
$bot->reply('Twoja ulubiona drużyna to: '. $team);
});
$botman->hears('.*(KKS|Lech|Poznań|Kolejorz).*', function ($bot) {
$bot->reply('Zdobyte mistrzostwa Polski: 1983, 1984, 1990, 1992, 1993, 2010, 2015');
});
$botman->hears('Chcę kupić ([0-9]+) bilet(y|ów)', function ($bot, $number) {
$bot->reply($number .' - tyle biletów zamawiam dla Ciebie. Proszę czekać...');
});
16. Można też przyjmować załączniki
$bot->receivesImages(function($bot, $images) {
foreach ($images as $image) {
$url = $image->getUrl(); // The direct url
$title = $image->getTitle(); // The title, if available
$payload = $image->getPayload(); // The original payload
}
});
17. Odpowiadanie
$botman->hears('Kto dziś wygra ważny mecz?', function (BotMan $bot) {
$bot->reply("LECH!");
$bot->reply("Kto?");
$bot->typesAndWaits(2); // 2 sekundy pokazuje, że pisze...
$bot->reply("LECH!");
});
18. Odpowiadanie obrazkiem
use BotManBotManMessagesAttachmentsImage;
use BotManBotManMessagesOutgoingOutgoingMessage;
$botman->hears('Pokaż herb', function (BotMan $bot) {
// Create attachment
$attachment = new Image('https://www.lechpoznan.pl/view/basic/images/favicon/facebook_200x200.png');
// Build message object
$message = OutgoingMessage::create('Herb Lecha Poznań')
->withAttachment($attachment);
// Reply message object
$bot->reply($message);
});
20. Konwersacje
class AccountRegisterConversation extends Conversation
{
protected $name;
protected $email;
public function run()
{
$this->askName();
}
public function askName()
{
$this->ask('OK, jak się nazywasz?', function(Answer $answer) {
$this->name = $answer->getText();
$this->say('Dziękuję za odpowiedź: '. $this->name);
$this->askEmail();
});
}
public function askEmail()
{
$this->ask('Podaj proszę adres e-mail by dokończyć rejestrację', function(Answer $answer) {
$this->email = $answer->getText();
// ... sending e-mail
$this->say('Dziękuję. Mail z potwierdzeniem został wysłany na: '. $this->email);
});
}
}
22. Pytania z opcjami wyboru
public function askForNextMatchWinner()
{
$question = Question::create('Kto wygra mecz w niedzielę?')
->fallback('Nie udało się odpowiedzieć...')
->callbackId('who_will_win_next_match')
->addButtons([
Button::create('Lech')->value('lech'),
Button::create('Jagiellonia')->value('jaga'),
]);
$this->ask($question, function (Answer $answer) {
// Detect if button was clicked:
if ($answer->isInteractiveMessageReply()) {
$selectedValue = $answer->getValue(); // will be either 'lech' or 'jaga'
$selectedText = $answer->getText(); // will be either 'Lech' or 'Jagiellonia'
}
});
}
23. Pytania ze wzorcami
public function askWhoWillWinNextMatch()
{
$this->ask('Kto w niedzielę wygra mecz? Lech czy Jagiellonia?', [
[
'pattern' => 'kks|lech|poznań|kolejorz',
'callback' => function () {
$this->say('Świetnie, że nadal w nas wierzysz!');
}
],
[
'pattern' => 'jagiellonia|białystok|jaga',
'callback' => function () {
$this->say(':(. Nadzieja umiera ostatnia!');
}
]
]);
}
24. Dane użytkownika
/**
* Retrieve User information.
* @param IncomingMessage $matchingMessage
* @return UserInterface
*/
public function getUser(IncomingMessage $matchingMessage)
{
return new User($matchingMessage->getSender());
}
// Access user
$user = $bot->getUser();
28. Testowanie
/** @test */
public function test_simple_reply()
{
$this->bot
->receives('Kto dziś wygra mecz?')
->assertReply('LECH!');
}
/** @test */
public function test_image_reply()
{
$this->bot
->receivesImage(['https://www.lechpoznan.pl/view/basic/images/favicon/facebook_200x200.png'])
->assertReply('To jest logo Lecha Poznań');
}
29. Testowanie
/** @test */
public function test_conversation()
{
$this->bot
->receives('Kto dziś wygra ważny mecz?')
->assertReplies([
'LECH!',
'Kto?',
])->receives('LECH')
->assertReply('Alleluja, alleluja...');
}
35. Messenger / Generic Template
$bot->reply(GenericTemplate::create()
->addElements([
Element::create('BotMan Documentation')
->subtitle('All about BotMan')
->image('http://botman.io/img/botman-body.png')
->addButton(ElementButton::create('visit')
->url('http://botman.io')
)
->addButton(ElementButton::create('tell me more')
->payload('tellmemore')
->type('postback')
),
Element::create('BotMan Laravel Starter')
->subtitle('This is the best way to start...')
->image('http://botman.io/img/botman-body.png')
->addButton(ElementButton::create('visit')
->url('https://github.com/botman/botman')
),
])
);
36. Messenger / List Template
$bot->reply(ListTemplate::create()
->useCompactView()
->addGlobalButton(ElementButton::create('view more')->url('http://buildachatbot.io'))
->addElement(
Element::create('BotMan Documentation')
->subtitle('All about BotMan')
->image('http://botman.io/img/botman-body.png')
->addButton(ElementButton::create('tell me more')
->payload('tellmemore')->type('postback'))
)
->addElement(
Element::create('BotMan Laravel Starter')
->subtitle('This is the best way to start...')
->image('http://botman.io/img/botman-body.png')
->addButton(ElementButton::create('visit')
->url('http://botman.io')
)
)
);
37. Messenger / Media Template
$bot->reply(MediaTemplate::create()
->element(MediaAttachmentElement::create('image')
->addButton(ElementButton::create('Tell me more')
->type('postback')
->payload('Tell me more'))
->addButton(ElementButton::create('Documentation')
->url('https://botman.io/'))));