首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BotMan -会话方法不应答

BotMan -会话方法不应答
EN

Stack Overflow用户
提问于 2019-12-26 16:56:11
回答 2查看 2.3K关注 0票数 0

我在做facebook信使机器人。我使用的是Botman (botman.io),没有Laravel或botman工作室。PHP版本为7.4。

简单的收听和回复方法工作良好,但会话应答方法不起作用。

如果我尝试输入hi\Hello或问候,聊天机器人回答我“你好!您的名字是什么?”,然后我写我的名字,chatbot不返回任何文本:-/

你能帮我一下窃听器在哪里吗?

有一个会话类

代码语言:javascript
复制
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();

        }

    }

还有config

代码语言:javascript
复制
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();
EN

回答 2

Stack Overflow用户

发布于 2021-05-06 16:37:46

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

代码语言:javascript
复制
 composer require symfony/cache

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

代码语言:javascript
复制
use BotMan\BotMan\Cache\SymfonyCache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

然后使用以下内容创建BotMan:

代码语言:javascript
复制
$adapter = new FilesystemAdapter();
$botman = BotManFactory::create($config, new SymfonyCache($adapter));

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

代码语言:javascript
复制
$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?");
    });
});
票数 2
EN

Stack Overflow用户

发布于 2021-05-21 14:33:03

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

步骤1:配置cache.yaml中的缓存

代码语言:javascript
复制
framework:
    cache:
        # botman: cache adapter
        app: cache.adapter.filesystem

步骤2:自动装配在services.yaml

代码语言:javascript
复制
services:
    BotMan\BotMan\Cache\SymfonyCache:
        arguments:
            $adapter: '@cache.app'

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

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

要创建OnBoardingConversation,只需遵循在botman中创建一个对话上的文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59491248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档