如何在 Symfony 3(控制器和视图)中集成 Botman?
How to integrate Botman in Symfony 3 (controller and view )?
我想将聊天机器人集成到我的 symfony 网站中。所以我看到有 Botman,它是一个 PHP 框架,它满足了我的需求,但我没有找到关于它与 Symfony.So 集成的文档,因为它也在 PHP 和 symfony 中,所以我开始用作曲家安装它,然后也是驱动程序。
这是我遵循的步骤
- 作曲家要求botman/botman
- 作曲家要求botman/driver-web
- 在我的额头上做一个控制器
我的控制器
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');
}
我的观点
对于我来说,我没有起点,我不知道该怎么做,这就是为什么我只是把 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>
不过没办法,我的渲染中只有一段css的显示和js代码。
可以帮我
谢谢
我假设您使用 Botman web widget 来呈现聊天框。
您需要三个路由和三个控制器函数:
- 将发回包含聊天机器人小部件的页面(以下示例中的“主页”),
- 一个将处理 Botman 逻辑和 return 机器人的序列化答案(以下示例中的“消息”),
- 将发回聊天框(以下示例中的“chatframe”)的一个。
这是一个基本示例:
<?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');
}
}
您需要两个视图,第一个是聊天框 chat_frame.html.twig
(Botman web widget's documentation 中提供的视图的简单复制粘贴):
<!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>
以及将在右下角包含聊天小部件的页面 homepage.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>
我想将聊天机器人集成到我的 symfony 网站中。所以我看到有 Botman,它是一个 PHP 框架,它满足了我的需求,但我没有找到关于它与 Symfony.So 集成的文档,因为它也在 PHP 和 symfony 中,所以我开始用作曲家安装它,然后也是驱动程序。
这是我遵循的步骤
- 作曲家要求botman/botman
- 作曲家要求botman/driver-web
- 在我的额头上做一个控制器
我的控制器
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');
}
我的观点 对于我来说,我没有起点,我不知道该怎么做,这就是为什么我只是把 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>
不过没办法,我的渲染中只有一段css的显示和js代码。 可以帮我 谢谢
我假设您使用 Botman web widget 来呈现聊天框。
您需要三个路由和三个控制器函数:
- 将发回包含聊天机器人小部件的页面(以下示例中的“主页”),
- 一个将处理 Botman 逻辑和 return 机器人的序列化答案(以下示例中的“消息”),
- 将发回聊天框(以下示例中的“chatframe”)的一个。
这是一个基本示例:
<?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');
}
}
您需要两个视图,第一个是聊天框 chat_frame.html.twig
(Botman web widget's documentation 中提供的视图的简单复制粘贴):
<!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>
以及将在右下角包含聊天小部件的页面 homepage.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>