简体   繁体   中英

BotMan and Symfony 5.1 - Install botman in subdirectory

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/

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

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:

My template (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)

/**
 * @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...

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>

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 .

You should use composer for managing dependencies.

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

composer require botman/botman

You can find here a kind of mini tutorial on how to integrate Botman in a Symfony3 project. 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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