简体   繁体   English

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

[英]Laravel Botman issue - Can't call functions from the same class after nesting inside botman class

I am trying to use the native buttons and questions feature in Botman inside laravel, however i am struggling to understand how to chain functions without using static functions.我正在尝试在 Laravel 中使用 Botman 中的本机按钮和问题功能,但是我很难理解如何在不使用静态函数的情况下链接函数。 I have it working where everything is a static function, however i want to use all the information gathered to send an email.我让它在一切都是静态函数的地方工作,但是我想使用收集到的所有信息来发送电子邮件。

    // 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.....

However without declaring the contactFollowUp() function as static and accessing it by using the classname BotManController::contactFollowUp($botman) However if i do this i have issues with accessing and setting data for use in other functions.但是,如果没有将contactFollowUp()函数声明为静态函数并使用类名BotManController::contactFollowUp($botman)访问它,但是如果我这样做,我在访问和设置用于其他函数的数据时会遇到问题。 Specifically i get a Method contactFollowUp does not exist error.具体来说,我得到一个 Method contactFollowUp 不存在错误。

So, after finding some github code examples i have managed to work out the issue.因此,在找到一些 github 代码示例后,我设法解决了这个问题。 It is to do with the way the botman framework is structured.这与botman框架的结构方式有关。 To achive linked conversations you have to use a function from the botman framework called startConversation() to envoke this you need to reference bot which is from the extended base class Conversation.要实现链接对话,您必须使用 botman 框架中名为startConversation()的函数来调用它,您需要引用来自扩展基类 Conversation 的bot So you will need an entry point and then the conversation you want to link to like so: *note you will need the default entry point of run() for each 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();
        }
    }

Then然后

// 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;
                    }
                }
            });
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM