简体   繁体   English

BotMan 和 Symfony 5.1 - 在子目录中安装 botman

[英]BotMan and Symfony 5.1 - Install botman in subdirectory

I'm newbie with Symfony.我是 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.我创建了一个 Symfony 应用程序,我的 Symfony 根目录中有一个 BotMan 文件夹,所以我想将该机器人集成到我的应用程序中。 My directory structure goes like this:我的目录结构是这样的:

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

So how can I "include" BotMan classes inside my Symfony project to be able to use them as any other Symfony class?那么如何在我的 Symfony 项目中“包含”BotMan 类,以便能够将它们用作任何其他 Symfony class? Do I need to change my services.yaml file?我需要更改我的 services.yaml 文件吗? 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...编辑:做了一些改变......

I installed BotMan again but as a standalone composer dependency, and now I have my "botman" folder inside vendor.我再次安装了 BotMan,但作为独立的作曲家依赖项,现在我在 vendor 中有我的“botman”文件夹。 After that, I created a twig template with the web widget and a method with the route "/botman/chat".之后,我创建了一个 twig 模板,其中包含 web 小部件和一个带有路径“/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:更详细的代码:

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

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

HomeController.php (only botman method) HomeController.php(仅限botman方法)

/**
 * @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', [
    ]);
}

Edit #2 : Finally I've found the answer...编辑#2终于我找到了答案......

In order to be able to use the bot, you need to return an empty response because headers have been already sent by Botman.为了能够使用该机器人,您需要返回一个空响应,因为 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>

You'll notice that the widget is empty and only contains response in JSON.您会注意到小部件是空的,并且仅包含 JSON 中的响应。 So I decided to write my template using Pete Lawrence's chatbot as a guidance and I don't use prebuilt widget.所以我决定使用Pete Lawrence 的聊天机器人作为指导来编写我的模板,并且我不使用预构建的小部件。 Link to the GitHub closed issue that helped me to solve this .链接到帮助我解决此问题的 GitHub 已关闭问题

You should use composer for managing dependencies.您应该使用 composer 来管理依赖项。

Check Alternatives part and "Basic Usage without BotMan Studio".检查替代部分和“没有 BotMan Studio 的基本用法”。

composer require botman/botman

You can find here a kind of mini tutorial on how to integrate Botman in a Symfony3 project.你可以在这里找到关于如何将Botman集成到Symfony3项目中的迷你教程。 I know that Symfony5 's philosophy is relatively different from Symfony3 's but with no loss of generality the up-cited tutorial might be useful.我知道Symfony5的理念与Symfony3的理念相对不同,但不失一般性,被引用的教程可能有用。

Also, you can find this bundle which is a kind of wrapper that includes Botman directly in Symfony 's services container... However it is only valid up to Symfony4 .此外,您还可以在Symfony的服务容器中找到这个包,它是一种直接包含Botman的包装器......但是它仅在Symfony4有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM