Created
March 12, 2018 17:17
-
-
Save WyriHaximus/64c297d920829979530c4b60287dfa15 to your computer and use it in GitHub Desktop.
botman with react/http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$time = time(); | |
use BotMan\BotMan\BotManFactory; | |
use BotMan\BotMan\Drivers\DriverManager; | |
use BotMan\Drivers\Web\WebDriver; | |
use Psr\Http\Message\ServerRequestInterface; | |
use React\EventLoop\Factory; | |
use React\Http\Response; | |
use React\Http\Server; | |
use React\Socket\Server as SocketServer; | |
use Symfony\Component\HttpFoundation\Request; | |
require 'vendor/autoload.php'; | |
$loop = Factory::create(); | |
$socket = new SocketServer('[::1]:8910', $loop); | |
$http = new Server([ | |
function (ServerRequestInterface $request, callable $next) use ($time) { | |
if ($request->getUri()->getPath() === '/botman') { | |
$sr = new Request( | |
$request->getQueryParams(), | |
$request->getParsedBody(), | |
$request->getAttributes(), | |
$request->getCookieParams(), | |
$request->getUploadedFiles(), | |
$request->getServerParams(), | |
$request->getBody()->getContents() | |
); | |
ob_start(); | |
$config = []; | |
// Load the driver(s) you want to use | |
DriverManager::loadDriver(WebDriver::class); | |
// Create an instance | |
$botman = BotManFactory::create($config, null, $sr); | |
// Give the bot something to listen for. | |
$botman->hears('hello', function ($bot) { | |
$bot->reply('Hello yourself.'); | |
}); | |
// Give the bot something to listen for. | |
$botman->hears('uptime', function ($bot) use ($time) { | |
$bot->reply('I have been running for ' . (time() - $time) . ' seconds.'); | |
}); | |
// Give the bot something to listen for. | |
$botman->hears('pid', function ($bot) use ($time) { | |
$bot->reply('My PID is: ' . getmypid()); | |
}); | |
try { | |
$botman->listen(); | |
return new Response(200, [], ob_get_clean()); | |
} catch (Throwable $et) { | |
return new Response(200, [], (string)$et); | |
} | |
} | |
return $next($request); | |
}, | |
function (ServerRequestInterface $request, callable $next) { | |
if ($request->getUri()->getPath() === '/botmanFrame') { | |
return new Response(200, [], '<!doctype html> | |
<!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>'); | |
} | |
return $next($request); | |
}, | |
function (ServerRequestInterface $request, callable $next) { | |
return new Response(200, [], '<!doctype html> | |
<html> | |
<head> | |
<title>BotMan Widget</title> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<script> | |
var botmanWidget = { | |
chatServer: \'/botman\', | |
frameEndpoint: \'/botmanFrame\', | |
}; | |
</script> | |
<script src=\'https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js\'></script> | |
</body> | |
</html>'); | |
}, | |
]); | |
$http->listen($socket); | |
$http->on('error', function ($e) { | |
echo (string)$e; | |
}); | |
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"botman/botman": "^2.3", | |
"botman/driver-web": "^1.5", | |
"react/http": "^0.8.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment