1

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.

Mariela
  • 134
  • 2
  • 13
  • How did you install BotMan? – noam Jul 14 '20 at 08:33
  • I create a new folder "my_botman_project" inside "my_symfony_project" then cd my_botman_project and followed the tutorials steps (composer global require "botman/installer", etc.) – Mariela Jul 14 '20 at 12:16
  • Do you have a botman directory inside your 'vendor' directory? – noam Jul 14 '20 at 12:20
  • What have you tried so far? Where are you stuck? Why not install BotMan like each other PHP dependency using Composer? – Nico Haase Jul 15 '20 at 13:57
  • I actually installed BotMan as a dependency using composer, I'm stuck with the controller, with the method that must render the chat. I don't know how to make it work. – Mariela Jul 15 '20 at 15:16
  • "Stuck" sounds broad. Can you explain more detailed why works, and what is not working? – Nico Haase Jul 15 '20 at 15:19

2 Answers2

3

You should use composer for managing dependencies.

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

composer require botman/botman
ffx14
  • 1,825
  • 2
  • 14
  • 19
0

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.

Gillu13
  • 898
  • 6
  • 11