【问题标题】:botman io + laravel not continue conversationsbotman io + laravel 不继续对话
【发布时间】:2020-09-05 15:13:30
【问题描述】:

我正在尝试使用电报在 laravel 7 和 botman 2 中开始对话。一切正常,但无法继续对话。当我试图回答对话的任何先前问题时,它假设开始新对话而不是询问对话线程的第二个问题。

我设置的电报 webhook url 是:

/api/telegram-bot

我的路线/api.php

Route::post('/telegram-bot', 'TelegramController@bot');

TelegramController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Template
use App\Channel;
use Config;

use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
use App\Conversations\BankingConversation;

class TelegramController extends Controller{
  public function bot(){
        $config = [
            "telegram" => [
               "token" => "{MY_BOT_TOKEN}",
               'conversation_cache_time' => 30
            ]
        ];
        DriverManager::loadDriver(\BotMan\Drivers\Telegram\TelegramDriver::class);
        $botman = BotManFactory::create($config);
        
         
        $botman->hears('(hi|hello|start)', function (BotMan $bot) {
            $bot->startConversation(new BankingConversation , \BotMan\Drivers\Telegram\TelegramDriver::class );
        });

        $botman->fallback(function($bot) {
            $bot->reply('Sorry, I did not understand you. Just type start to continue.');
        });

        $botman->listen();
    }
}

最后是 BankingConversation

<?php

namespace App\Conversations;

use Illuminate\Foundation\Inspiring;
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;

class BankingConversation extends Conversation
{

    protected $fullname;

    protected $email;

    public function askFullname()
    {
        $welcome = 'Hey '; 

        $this->ask($welcome.' What is your name ?', function(Answer $answer) {
            // Save result
            $this->fullname = $answer->getText();

            $this->say('Nice to meet you '.$this->fullname);
            $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);
        });
    }

    public function run()
    {
        // This will be called immediately
        $this->askFullname();
    }
}

每当键入 hi/hello/start 时,它都会问我对话的第一个问题“嘿,你叫什么名字?”

但在回答问题后,它会回退并返回“对不起,我不明白你的意思。只需键入开始即可继续。”

我在这里做错了什么?

【问题讨论】:

    标签: php laravel chatbot botman


    【解决方案1】:

    您尚未指定缓存驱动程序。

    更新代码部分。

    DriverManager::loadDriver(\BotMan\Drivers\Telegram\TelegramDriver::class);

    $botman = BotManFactory::create($config, new \BotMan\BotMan\Cache\LaravelCache());

    Botman 使用缓存来维护对话。

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 2021-01-22
      • 2020-04-16
      • 2023-03-12
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多