我正在尝试使用Botman inside laravel中的原生按钮和问题功能,但是我正在努力理解如何在不使用静态函数的情况下链接函数。我有它的工作,其中一切都是一个静态函数,但我想使用收集的所有信息发送电子邮件。
// initialization function
public function handle()
{
$botman->hears("{message}", function($botman, $message) {
$this->selectHelpQuery($botman);
});
}
// ask question function
public function selectHelpQuery($botman)
{
$question = Question::create("How can i help you, would you like to know about the following:")
->fallback("Unable to help at this time, please try again later")
->callbackId("choose_query")
->addButtons([
Button::create("button1")->value("val1"),
Button::create("button2")->value("val2"),
]);
$botman->ask($question, function (Answer $answer, $botman) {
// Detect if button was clicked:
if ($answer->isInteractiveMessageReply()) {
if($answer->getValue() == "val1")
{
$this->contactFollowUp($botman); //** not working
} else {
$this->contactNoFollowUp($botman); //** not working
}
}
});
}
// other functions.....但是,如果我没有将contactFollowUp()函数声明为静态并使用类名BotManController::contactFollowUp($botman)来访问它,那么在访问和设置其他函数中使用的数据时就会遇到问题。具体地说,我得到了一个contactFollowUp不存在的方法错误。
发布于 2020-03-12 13:08:02
因此,在找到一些github代码示例后,我设法解决了这个问题。这与botman框架的结构方式有关。要实现链接会话,您必须使用botman框架中一个名为startConversation()的函数来调用它,您需要引用来自扩展基类Conversation的bot。因此,您需要一个入口点,然后是您想要链接到的对话,如下所示:*注意,对于每个对话,您都需要默认的入口点run()。
//BotManController.php
<?php
namespace App\Http\Controllers\Chatbot;
use App\Http\Controllers\Controller;
use BotMan\BotMan\BotMan;
use Illuminate\Http\Request;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Outgoing\Question;
class BotManController extends Controller
{
/**
* start the conversation on intitlization
*/
public function handle()
{
$botman = app("botman");
$botman->hears("{message}", function($botman, $message) {
$botman->startConversation(new BotManStart);
});
$botman->listen();
}
}然后
// BotManStart.php
<?php
namespace App\Http\Controllers\Chatbot;
use BotMan\BotMan\BotMan;
use Illuminate\Http\Request;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Conversations\Conversation;
class BotManStart extends Conversation
{
public function run()
{
$this->selectHelpQuery();
}
public function selectHelpQuery()
{
$question = Question::create("How can i help you, would you like to know about the following: ")
->fallback("Unable to help at this time, please try again later")
->callbackId("choose_query")
->addButtons([
Button::create("Button 1")->value("button1"),
Button::create("Button 2")->value("button2"),
]);
$this->ask($question, function (Answer $answer) {
if ($answer->isInteractiveMessageReply()) {
switch ($answer->getValue()) {
case "button1":
$this->bot->startConversation(new BotManConversation1());
break;
case "button2":
$this->bot->startConversation(new BotManConversation2());
break;
}
}
});
}
}https://stackoverflow.com/questions/60520546
复制相似问题