0

我正在研究 facebook messenger bot。我正在使用没有 Laravel 或 botman studio 的 Botman (botman.io)。PHP的版本是7.4。

简单的听到和回复方法可以正常工作,但对话回复方法不起作用。

如果我尝试输入 hi|hello 或打招呼,聊天机器人会回答我“你好!你的名字是什么?”,然后我写下我的名字,聊天机器人不会返回任何文本:-/

你能帮我看看哪里有bug吗?

有一个会话类

namespace LiborMatejka\Conversations;

use BotMan\BotMan\Messages\Conversations\Conversation;
use BotMan\BotMan\Messages\Incoming\Answer;

    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();

        }

    }

并且有配置

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

use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
use BotMan\Drivers\Facebook\FacebookDriver;
use LiborMatejka\Conversations\OnboardingConversation;

$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(\BotMan\Drivers\Facebook\FacebookDriver::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();
4

2 回答 2

2

使用 composer 将 symfony/cache 添加到您的项目中

 composer require symfony/cache

将以下内容放在您要设置 BotMan 的 index.php (或其他)文件的顶部

use BotMan\BotMan\Cache\SymfonyCache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

然后使用以下命令创建您的 BotMan:

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

然后$botman相应地使用您的变量,如下例所示:

$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?");
    });
});
于 2021-05-06T16:37:46.683 回答
0

我宁愿使用自动连接在任何你创建 Botman 实例的地方注入 SymfonyCache,而不是一次又一次地创建适配器和缓存。

第一步:配置缓存cache.yaml

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

第2步:自动装配services.yaml

services:
    BotMan\BotMan\Cache\SymfonyCache:
        arguments:
            $adapter: '@cache.app'

第 3 步:在您需要的地方注入SymfonyCache,例如在ChatController::message()

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

要创建 OnBoardingConversation,只需按照在 botman 中创建对话的文档即可

于 2021-05-21T14:33:03.363 回答