Skip to content

Instantly share code, notes, and snippets.

@revooms
Created August 19, 2020 14:59
Show Gist options
  • Save revooms/3071697f8af453fdedf9691493107173 to your computer and use it in GitHub Desktop.
Save revooms/3071697f8af453fdedf9691493107173 to your computer and use it in GitHub Desktop.
Botman Conversation Setup
<?php
use BotMan\BotMan\Messages\Conversations\Conversation;
use BotMan\BotMan\Messages\Incoming\Answer;
class AskName extends Conversation
{
protected $firstname;
public function askFirstname()
{
$this->ask('Hello! What is your firstname?', function (Answer $answer) {
// Save result
$this->firstname = $answer->getText();
$this->say('Nice to meet you ' . $this->firstname);
});
}
public function run()
{
// This will be called immediately
$this->askFirstname();
}
}
<!doctype html>
<html>
<head>
<title>BotMan Widget</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/assets/css/chat.min.css">
</head>
<body>
<script id="botmanWidget" src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/chat.js'></script>
</body>
</html>
<?php
require_once 'vendor/autoload.php';
require_once 'AskName.php';
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
use BotMan\Drivers\Web\WebDriver;
use BotMan\BotMan\Cache\SymfonyCache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$config = [];
DriverManager::loadDriver(WebDriver::class);
$adapter = new FilesystemAdapter;
$botman = BotManFactory::create($config, new SymfonyCache($adapter));
// Give the bot something to listen for.
$botman->hears('Hello', function (BotMan $bot) {
$bot->startConversation(new AskName);
});
$botman->hears('Hi', function ($bot) {
$bot->reply('Hello!');
});
$botman->fallback(function ($bot) {
$bot->reply('Sorry, I did not understand these commands. Here is a list of commands I understand: ...');
});
// Start listening
$botman->listen();
<script>
var botmanWidget = {
frameEndpoint: '/chat.html',
introMessage: 'Hello, I am a Chatbot',
chatServer : 'chat.php',
title: 'My Chatbot',
mainColor: '#456765',
bubbleBackground: '#ff76f4',
aboutText: '',
bubbleAvatarUrl: '',
};
</script>
<script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment