BotMan 和 Symfony 5.1 - 在子目录中安装 botman [英] BotMan and Symfony 5.1 - Install botman in subdirectory

查看:26
本文介绍了BotMan 和 Symfony 5.1 - 在子目录中安装 botman的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Symfony 的新手.我创建了一个 Symfony 应用程序,并且在我的 Symfony 根目录中有一个 BotMan 文件夹,所以我想将该机器人集成到我的应用程序中.我的目录结构是这样的:

I'm newbie with Symfony. I created a Symfony App and I have a BotMan folder inside my Symfony root directory, so I want to integrate that bot in my App. My directory structure goes like this:

my_symfony_project/
  bin/
  config/
  migrations/
  ...
  my_botman_project/

那么我如何包含"?我的 Symfony 项目中的 BotMan 类能够像任何其他 Symfony 类一样使用它们吗?我需要更改我的 services.yaml 文件吗?如何?我知道这可能是一个愚蠢的问题,但我还不明白它是如何工作的.提前致谢.

So how can I "include" BotMan classes inside my Symfony project to be able to use them as any other Symfony class? Do I need to change my services.yaml file? How? I know maybe it's a silly question but I don't understand yet how it works. Thanks in advance.

编辑:进行了一些更改...

Edit: Made some changes...

我再次安装了 BotMan,但作为独立的作曲家依赖项,现在我有了我的botman"供应商内的文件夹.之后,我创建了一个带有 web 小部件的 twig 模板和一个带有/botman/chat"路由的方法.小部件出现了,但我不知道如何在其中进行聊天.代码的更多细节:

I installed BotMan again but as a standalone composer dependency, and now I have my "botman" folder inside vendor. After that, I created a twig template with the web widget and a method with the route "/botman/chat". The widget shows up but I don't know how to get the chat inside of it. A little more detail with code:

我的模板(mybot/index.html.twig):

My template (mybot/index.html.twig):

<script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>

HomeController.php(仅botman方法)

HomeController.php (only botman method)

/**
 * @Route("/botman/chat", name="mybot")
 */
public function botman(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("hi");
    
    return $this->render('mybot/index.html.twig', [
    ]);
}

编辑 #2:我终于找到了答案......

为了能够使用机器人,您需要返回一个空响应,因为 Botman 已经发送了标头.之后,如果您只在模板中包含小部件...

In order to be able to use the bot, you need to return an empty response because headers have been already sent by Botman. After that, if you only include the widget in your template...

<script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>

您会注意到小部件是空的,并且只包含 JSON 格式的响应.所以我决定使用 Pete Lawrence 的聊天机器人 作为指导来编写我的模板,我不使用预构建的小部件.链接到帮助我解决此问题的 GitHub 已关闭问题.

You'll notice that the widget is empty and only contains response in JSON. So I decided to write my template using Pete Lawrence's chatbot as a guidance and I don't use prebuilt widget. Link to the GitHub closed issue that helped me to solve this.

推荐答案

你应该使用 composer 来管理依赖项.

You should use composer for managing dependencies.

检查替代部分和没有BotMan Studio的基本用法".

Check Alternatives part and "Basic Usage without BotMan Studio".

composer require botman/botman

这篇关于BotMan 和 Symfony 5.1 - 在子目录中安装 botman的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆