首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel Botman问题-在botman类中嵌套后无法调用同一类中的函数

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

Stack Overflow用户
提问于 2020-03-04 14:55:55
回答 1查看 547关注 0票数 0

我正在尝试使用Botman inside laravel中的原生按钮和问题功能,但是我正在努力理解如何在不使用静态函数的情况下链接函数。我有它的工作,其中一切都是一个静态函数,但我想使用收集的所有信息发送电子邮件。

代码语言:javascript
复制
    // 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不存在的方法错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-12 13:08:02

因此,在找到一些github代码示例后,我设法解决了这个问题。这与botman框架的结构方式有关。要实现链接会话,您必须使用botman框架中一个名为startConversation()的函数来调用它,您需要引用来自扩展基类Conversation的bot。因此,您需要一个入口点,然后是您想要链接到的对话,如下所示:*注意,对于每个对话,您都需要默认的入口点run()。

代码语言:javascript
复制
//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();
        }
    }

然后

代码语言:javascript
复制
// 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;
                    }
                }
            });
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60520546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档