簡體   English   中英

Laravel Botman 問題 - 在 botman 類中嵌套后無法調用來自同一類的函數

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

我正在嘗試在 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()的函數來調用它,您需要引用來自擴展基類 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;
                    }
                }
            });
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM