Laravel Botman问题-嵌套在botman类中后无法调用同一类中的函数

czq61nw1  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(119)

我正在尝试使用本机按钮和问题的特点,在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不存在的错误。

30byixjq

30byixjq1#

所以,在找到一些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;
                    }
                }
            });
        }
    }

相关问题