skip to Main Content

i am working on facebook messenger bot. I am using Botman (botman.io) without Laravel or botman studio. Version of PHP is 7.4.

Simple hears and reply method works fine, but conversation replying method does not working.

If I try type hi|hello or some greetings, chatbot answer me “Hello! What is your firstname?”, then I write my name and chatbot does not returns any text :-/

Can you help me where is a bug?

There is a conversation class:

namespace LiborMatejkaConversations;

use BotManBotManMessagesConversationsConversation;
use BotManBotManMessagesIncomingAnswer;

    class OnboardingConversation extends Conversation {

        protected $firstname;
        protected $email;

        function askFirstname() {

            $this->ask('Hello! What is your firstname?', function (Answer $answer) {

                // Save result
                $this->firstname = $answer->getText();

                $this->say('Nice to meet you ' . $this->firstname);

                $this->askEmail();

            });

        }

        public function askEmail() {

            $this->ask('One more thing - what is your email?', function (Answer $answer) {
                // Save result
                $this->email = $answer->getText();

                $this->say('Great - that is all we need, ' . $this->firstname);
            });

            //$this->bot->typesAndWaits(2);
        }

        public function run() {

            // This will be called immediately

            $this->askFirstname();

        }

    }

and there is config:

require_once "vendor/autoload.php";
require_once "class/onboardingConversation.php";

use BotManBotManBotMan;
use BotManBotManBotManFactory;
use BotManBotManDriversDriverManager;
use BotManDriversFacebookFacebookDriver;
use LiborMatejkaConversationsOnboardingConversation;

$config = [
    // Your driver-specific configuration
    'facebook' => [
        'token' => 'my_token',
        'app_secret' => 'my_secret_app_code',
        'verification' => 'verification_code',
    ],
    'botman' => [
        'conversation_cache_time' => 0,
    ],
];

// Load the driver(s) you want to use
DriverManager::loadDriver(BotManDriversFacebookFacebookDriver::class);

// Create an instance
$botman = BotManFactory::create($config);

$botman->hears('ahoj|hi|hello|cau|cus|zdar|zdarec|cago|hey|ciao', function (BotMan $bot) {
    $bot->startConversation(new OnboardingConversation);
});

// Start listening
$botman->listen();

2

Answers


  1. Add symfony/cache to your project using composer

     composer require symfony/cache
    

    Put following at top of index.php (or other) file where you’re setting up BotMan

    use BotManBotManCacheSymfonyCache;
    use SymfonyComponentCacheAdapterFilesystemAdapter;
    

    Then create your BotMan using the following:

    $adapter = new FilesystemAdapter();
    $botman = BotManFactory::create($config, new SymfonyCache($adapter));
    

    Then use your $botman variable accordingly, like example below:

    $botman->hears('Hi', function (BotMan $bot) {
        $bot->typesAndWaits(2);
        $bot->reply('Hello and welcome');
        $bot->typesAndWaits(2);
        $bot->ask('Anything I can do for you today?',function($answer, $bot){
            $bot->say("Oh, really! You said '{$answer->getText()}'... is that right?");
        });
    });
    
    Login or Signup to reply.
  2. I would rather to use auto-wiring to inject the SymfonyCache anywhere you create the Botman instance, without creating the adapter and cache again and again.

    Step 1: config the cache in cache.yaml

    framework:
        cache:
            # botman: cache adapter
            app: cache.adapter.filesystem
    

    Step 2: autowiring in services.yaml

    services:
        BotManBotManCacheSymfonyCache:
            arguments:
                $adapter: '@cache.app'
    
    

    Step 3: Inject the SymfonyCache where you need, for example in ChatController::message()

    public function message(SymfonyCache $symfonyCache): Response
    {
        ....
        $botman = BotManFactory::create([], $symfonyCache);
        ....
        $botman->hears(
                'survey',
                function (BotMan $bot) {
                    $bot->startConversation(new OnBoardingConversation());
                }
            );
    }
    
    

    To create the OnBoardingConversation, just follow the documentation on create a conversation in botman

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search