简体   繁体   中英

Chatbot - Botman functions not working

I am using Botman 2.0 to build a Facebook Messenger chatbot.

Everything is fine with webhook verification and the hear() and reply() methods work well.

But, it looks like other methods don't work.

ie

Whenever I use the say() method, it never works. My server receives the request from Messenger, but it does not respond with the message response.

I have test with Botman's main example:

$botman->hears('Hello BotMan!', function($bot) {
    $bot->reply('Hello!');
    $bot->ask('Whats your name?', function($answer, $bot) {
        $bot->say('Welcome '.$answer->getText()); //this never works
    });
});

Additionally, when I try to use ButtonTemplate it raises an exception:

PHP Fatal error: Uncaught Error: Class 'ButtonTemplate' not found

Even though Botman's Facebook Driver is loaded:

DriverManager::loadDriver(\\BotMan\\Drivers\\Facebook\\FacebookDriver::class);

And my composer.json file looks right:

"require": {
        "botman/driver-facebook": "^1.7"
    }

What am I missing here?

You need add in your header the following classes:

use BotMan\Drivers\Facebook\Commands\AddStartButtonPayload;
use BotMan\Drivers\Facebook\Commands\AddGreetingText;

use BotMan\Drivers\Facebook\Extensions\ButtonTemplate;
use BotMan\Drivers\Facebook\Extensions\ElementButton;

This works for me. I added the below Facebook extensions immediately after the controller class

use BotMan\Drivers\Facebook\Extensions\Element as Element; 
use BotMan\Drivers\Facebook\Extensions\ElementButton as ElementButton; 
use BotMan\Drivers\Facebook\Extensions\ButtonTemplate as ButtonTemplate; 
use BotMan\Drivers\Facebook\Extensions\GenericTemplate as GenericTemplate; 
use BotMan\Drivers\Facebook\Extensions\ListTemplate as ListTemplate;

$botman = resolve('botman');

$botman->hears('Hello BotMan!', function($bot) {
    $bot->reply('Hello!');
    $bot->ask('Whats your name?', function($answer, $bot) {
        $bot->say('Welcome '.$answer->getText()); //this works
    });
});

You can check from this link as well https://www.gitmemory.com/issue/botman/botman/1055/522233149

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