簡體   English   中英

Symfony 3(控制器和視圖)如何集成Botman?

[英]How to integrate Botman in Symfony 3 (controller and view )?

我想將聊天機器人集成到我的 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司機也是。

這是我遵循的步驟

  1. 作曲家需要 botman/botman
  2. 作曲家需要 botman/driver-web
  3. 在我的額頭上做一個 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');
 }

我的觀點對於我的觀點,我沒有起點,我不知道該怎么做,這就是為什么我只是把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 小部件來呈現聊天框。

您需要三個路由和三個 controller 功能:

  • 一個將發回包含聊天機器人小部件的頁面(以下示例中的“主頁”)的頁面,
  • 一個將處理 Botman 邏輯並返回 bot 的序列化答案(以下示例中的“消息”),
  • 將發送回聊天框架(以下示例中的“聊天框架”)的一種。

這是一個基本示例:

<?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.twigBotman 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>

以及將在其右下角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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM