[英]BotMan - Conversations method is not replying
i am working on facebook messenger bot.我正在开发 facebook Messenger bot。 I am using Botman (botman.io) without Laravel or botman studio.我正在使用没有 Laravel 或 botman studio 的 Botman (botman.io)。 Version of PHP is 7.4. PHP 的版本是 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 :-/如果我尝试输入 hi|hello 或一些问候,聊天机器人回答我“你好!你的名字是什么?”,然后我写下我的名字,聊天机器人不会返回任何文本:-/
Can you help me where is a bug?你能帮我看看哪里有bug吗?
There is a conversation class :有一个会话类:
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();
}
}
and there is config :并且有配置:
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();
问题已解决 - 我添加了 symfony 缓存
Add symfony/cache to your project using composer使用 composer 将 symfony/cache 添加到你的项目中
composer require symfony/cache
Put following at top of index.php (or other) file where you're setting up BotMan将以下内容放在您设置 BotMan 的 index.php(或其他)文件的顶部
use BotMan\BotMan\Cache\SymfonyCache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
Then create your BotMan using the following:然后使用以下命令创建您的 BotMan:
$adapter = new FilesystemAdapter();
$botman = BotManFactory::create($config, new SymfonyCache($adapter));
Then use your $botman
variable accordingly, like example below:然后相应地使用您的$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?");
});
});
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.我宁愿使用自动连接将 SymfonyCache 注入您创建 Botman 实例的任何地方,而无需一次又一次地创建适配器和缓存。
Step 1: config the cache in cache.yaml
第 1 步:在cache.yaml
配置缓存
framework:
cache:
# botman: cache adapter
app: cache.adapter.filesystem
Step 2: autowiring in services.yaml
第 2 步:在services.yaml
自动装配
services:
BotMan\BotMan\Cache\SymfonyCache:
arguments:
$adapter: '@cache.app'
Step 3: Inject the SymfonyCache
where you need, for example in ChatController::message()
第 3 步:在需要的地方注入SymfonyCache
,例如在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要创建 OnBoardingConversation,只需按照有关在 botman 中创建对话的文档进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.