[英]How to integrate Botman in Symfony 3 (controller and view )?
I would like to integrate a chatbot into my symfony website.我想将聊天机器人集成到我的 symfony 网站中。 So I saw that there was Botman which is a PHP framework and it meets my needs, but I find no documentation concerning its integration with Symfony.So as it is in PHP and symfony too, so I started to install it with composer and then the drivers too. So I saw that there was Botman which is a PHP framework and it meets my needs, but I find no documentation concerning its integration with Symfony.So as it is in PHP and symfony too, so I started to install it with composer and then the司机也是。
Here are the steps I followed这是我遵循的步骤
My Controller我的 Controller
public function chatAction()
{
$config = [
// Your driver-specific configuration
// "telegram" => [
// "token" => "TOKEN"
// ]
];
DriverManager::loadDriver(\BotMan\Drivers\Web\WebDriver::class);
$botman = BotManFactory::create($config);
// Give the bot something to listen for.
$botman->hears('Hello', function (BotMan $bot) {
$bot->reply('Hello too');
});
// $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();
return $this->render('DoctixFrontBundle:Chat:index.html.twig');
}
My view For my view I have no starting point and I don't know what to do clearly, that's why i just put the css and the js of botman in it我的观点对于我的观点,我没有起点,我不知道该怎么做,这就是为什么我只是把css和botman的js放在里面
<!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>
<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>
But nothing to do, I only have a display of a piece of css and js code in my rendering.不过没事,我的渲染中只有一段css和js代码的展示。 Can help me Thanks.可以帮助我谢谢。
I assume that you use the Botman web widget to render the chat box.我假设您使用Botman web 小部件来呈现聊天框。
You need three routes and three controller functions:您需要三个路由和三个 controller 功能:
here is a basic example:这是一个基本示例:
<?php
namespace AppBundle\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
class BobotController extends Controller{
/**
* @Route("/message", name="message")
*/
function messageAction(Request $request)
{
DriverManager::loadDriver(\BotMan\Drivers\Web\WebDriver::class);
// Configuration for the BotMan WebDriver
$config = [];
// Create BotMan instance
$botman = BotManFactory::create($config);
// Give the bot some things to listen for.
$botman->hears('(hello|hi|hey)', function (BotMan $bot) {
$bot->reply('Hello!');
});
// Set a fallback
$botman->fallback(function (BotMan $bot) {
$bot->reply('Sorry, I did not understand.');
});
// Start listening
$botman->listen();
return new Response();
}
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
return $this->render('DoctixFrontBundle:Chat:homepage.html.twig');
}
/**
* @Route("/chatframe", name="chatframe")
*/
public function chatframeAction(Request $request)
{
return $this->render('DoctixFrontBundle:Chat:chat_frame.html.twig');
}
}
And you need two views, first the chat frame chat_frame.html.twig
(a simple copy-paste of the one provided in Botman web widget's documentation ):您需要两个视图,第一个是聊天框架chat_frame.html.twig
( Botman web中提供的一个简单的复制粘贴):
<!DOCTYPE html>
<html lang="en">
<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>
And the page that will contain the chat widget in its bottom right corner homepage.html.twig
:以及将在其右下角homepage.html.twig
中包含聊天小部件的页面。html.twig:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Hello World!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Click on the chat button.</p>
<script>
var botmanWidget = {
frameEndpoint: '{{ path("chatframe") }}',
chatServer: '{{ path("message") }}',
introMessage: 'Hello, I am a Chatbot',
title: 'My Chatbot',
mainColor: '#456765',
bubbleBackground: '#ff76f4',
aboutText: ''
};
</script>
<script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.