Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 等待botman对话结束并继续_Php_Laravel_Facebook_Bots_Botman - Fatal编程技术网

Php 等待botman对话结束并继续

Php 等待botman对话结束并继续,php,laravel,facebook,bots,botman,Php,Laravel,Facebook,Bots,Botman,嗨,我正试图写一个Facebook机器人,但挑战是即使对话还没有结束,控制器中的代码也会被执行。如何使此同步 这是我的控制器 class GetStartedController extends Controller { public function index($bot) { $user = $bot->getUser(); $ps_id = $user->getId(); $bot_user = FacebookU

嗨,我正试图写一个Facebook机器人,但挑战是即使对话还没有结束,控制器中的代码也会被执行。如何使此同步

这是我的控制器

class GetStartedController extends Controller
{
    public function index($bot)
    {
        $user = $bot->getUser();
        $ps_id = $user->getId();
        $bot_user = FacebookUser::where('ps_id',$ps_id)->first();
        if($bot_user == null){
            $bot->startConversation(new AskDemographics($bot));
            // $this->saveNewUser($user);
            $bot->reply("User saved");
        }else {
            $bot->reply("Display menu");
        }
    }

    public function saveNewUser($user){
        $_user = new User;
        $fb_user = new FacebookUser;

        $_user->name = $user->getFirstName() ." ". $user->getLastName();
        $_user->save();

        $fb_user->user_id = $_user->id;
        $fb_user->ps_id = $user->getId();
        $fb_user->save();
    }
}
在我的AskDemoGraphics对话中

<?php

namespace App\Conversations;

use BotMan\BotMan\BotMan;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Conversations\Conversation;


class AskDemographics extends Conversation
{
    public $bot;
    protected $year_of_birth;
    protected $firstname;

    public function __construct(BotMan $bot){
        $this->bot = $bot;
    }

    public function askYearOfBirth()
    {
        $msg = 'Please put your year of birth e.g 1997';
        $error_msg_age = 'Enter a valid age';
        $this->bot->typesAndWaits(1);

        $this->ask($msg , function ($answer, $conversation ) use ($error_msg_age) {
            $reply = $answer->getText();
            if (preg_match('/^\d{4}$/',$reply)){
                $this->say('Ask another question');
            }else{
                $conversation->say($error_msg_age);
                $this->askYearOfBirth();
            }

        });

    }

    /**
     * Start the conversation.
     *
     * @return mixed
     */

    public function run()
    {
        $this->askYearOfBirth();
    }
}