php - Laravel Botman 问题 - 嵌套在 botman 类中后无法调用同一类的函数
问题描述
我正在尝试在 laravel 中使用 Botman 中的本机按钮和问题功能,但是我很难理解如何在不使用静态函数的情况下链接函数。我让它在一切都是静态功能的地方工作,但是我想使用收集到的所有信息来发送电子邮件。
// 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)
但是如果我这样做,我在访问和设置用于其他函数的数据时会遇到问题。具体来说,我得到一个 Method contactFollowUp 不存在错误。
解决方案
因此,在找到一些 github 代码示例后,我设法解决了这个问题。这与 botman 框架的结构方式有关。要实现链接对话,您必须使用 botman 框架中的一个函数来调用startConversation()
这个函数,您需要引用bot
该函数,该函数来自扩展基类 Conversation。所以你需要一个入口点,然后是你想要链接的对话,如下所示: *注意,每个对话都需要 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;
}
}
});
}
}
推荐阅读
- 2sxc - WebAPI - Error Accessing ADAM Assets in 2sxc v9.42
- group-by - PySpark:基于观察序列的组类型的编号组
- bazel - 即使存在,也无法找到“datagen”文件夹和 BUILD 文件
- html - 如何在css中选择彼此相邻的特定元素
- sql-server - 有没有办法在带有条件的 INSERT INTO 子句中选择一个表?
- python - 如何将 OpenCv 图像从网络摄像头流式传输到 http 服务器
- java - JNI Linux 分段错误
- c++ - 二维容器的通用打印
- c - 获取结构的用户输入的问题
- r - 如何在列表中粘贴对象的完整位置 (r)