我正在使用PHP Botman库为Facebook页面构建聊天机器人。
它总体上运行良好,唯一的问题是它的对话冲突,我认为这是由于缓存造成的。
当我在其他内部开始对话时,先前对话的缓存将被新的对话所覆盖。我正在使用带有有效负载的通用模板(回答问题时Facebook触发的事件),并将其传递给ask()方法,该函数将问题关闭函数存储到缓存中。
当Facebook向我的服务器应用程序触发有效负载事件时,Botman读取有效负载并触发缓存的关闭函数。
我面临的问题是,第一次调用通用模板的有效负载时,它工作正常,但是在第二次调用时,未调用其缓存关闭功能,而是触发了后备功能。谁能帮助我,这是我的代码。
主控制器代码:
<?php
namespace App\Http\Controllers;
use App\Templates\Templates;
use App\Conversation\Season as SeasonCoversation;
use App\Controllers\BaseController;
use BotMan\BotMan\BotMan;
class ChatbotController extends Controller
{
public function start()
{
//initialize botman
$botman = app('botman');
//greeting message
$botman->hears('(hi|hello|test|start|hey|ہیلو|salam|get started|restart|let\'s talk)', function (BotMan $bot) {
$user = $bot->getUser();
$firstname = $user->getFirstName();
$bot->typesAndWaits(1);
$bot->reply("Hi {$firstname}");
$bot->typesAndWaits(1);
$bot->reply("Welcome! ?");
Templates::MainMenu($bot);
});
//Seasons
$botman->hears('Songs', function (BotMan $bot) {
$bot->typesAndWaits(2);
$bot->reply("What season are you up for today? ??");
$bot->typesAndWaits(1);
$bot->startConversation(new SeasonCoversation);
});
//Unexpected data
$botman->fallback(function ($bot) {
$bot->typesAndWaits(1);
$bot->reply("Sorry, I couldn't get it...");
});
$botman->listen();
}
}
对话代码:
<?php
namespace App\Conversation;
use App\Season as SeasonModel;
use App\Conversation\Episode;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Conversations\Conversation;
use BotMan\Drivers\Facebook\Extensions\Element;
use BotMan\Drivers\Facebook\Extensions\ElementButton;
use BotMan\Drivers\Facebook\Extensions\GenericTemplate;
class Season extends Conversation
{
public function run()
{
$this->getSeasons();
}
public function getSeasons() {
$seasons = SeasonModel::all();
$seasonsCount = $allSeasons->count();
if ($seasonsCount) {
$seasonElements = [];
$i = 1;
foreach ($seasons as $season) {
$seasonElements[] = Element::create('Coke Studio')
->image(secure_asset($season->image))
->addButton(ElementButton::create($season->name)->type('postback')->payload('seasonPayload'.uniqid().'_'.$season->id));
}
$question = GenericTemplate::create()
->addImageAspectRatio(GenericTemplate::RATIO_HORIZONTAL)
->addElements($seasonElements);
$this->ask($question, function (Answer $answer) {
if($answer->isInteractiveMessageReply()) {
$this->bot->startConversation(new Episode(false, null, extractPayloadValue($answer->getValue())));
}
});
} else {
$this->bot->reply("Sorry, I couldn't get it...");
}
}
也没有错误。