App\Http\Controllers\BotController::handleMessage PHP Method

handleMessage() public method

public handleMessage ( $platform )
    public function handleMessage($platform)
    {
        $input = Input::all();
        $botUserId = $input['from']['id'];
        if (!($token = $this->authenticate($input))) {
            return SkypeResponse::message(trans('texts.not_authorized'));
        }
        try {
            if ($input['type'] === 'contactRelationUpdate') {
                // brand new user, ask for their email
                if ($input['action'] === 'add') {
                    $response = SkypeResponse::message(trans('texts.bot_get_email'));
                    $state = BOT_STATE_GET_EMAIL;
                } elseif ($input['action'] === 'remove') {
                    $this->removeBot($botUserId);
                    $this->saveState($token, false);
                    return RESULT_SUCCESS;
                }
            } else {
                $state = $this->loadState($token);
                $text = strip_tags($input['text']);
                // user gaves us their email
                if ($state === BOT_STATE_GET_EMAIL) {
                    if ($this->validateEmail($text, $botUserId)) {
                        $response = SkypeResponse::message(trans('texts.bot_get_code'));
                        $state = BOT_STATE_GET_CODE;
                    } else {
                        $response = SkypeResponse::message(trans('texts.email_not_found', ['email' => $text]));
                    }
                    // user sent the scurity code
                } elseif ($state === BOT_STATE_GET_CODE) {
                    if ($this->validateCode($text, $botUserId)) {
                        $response = SkypeResponse::message(trans('texts.bot_welcome') . trans('texts.bot_help_message'));
                        $state = BOT_STATE_READY;
                    } else {
                        $response = SkypeResponse::message(trans('texts.invalid_code'));
                    }
                    // regular chat message
                } else {
                    if ($text === 'help') {
                        $response = SkypeResponse::message(trans('texts.bot_help_message'));
                    } elseif ($text == 'status') {
                        $response = SkypeResponse::message(trans('texts.intent_not_supported'));
                    } else {
                        if (!($user = User::whereBotUserId($botUserId)->with('account')->first())) {
                            return SkypeResponse::message(trans('texts.not_authorized'));
                        }
                        Auth::onceUsingId($user->id);
                        $user->account->loadLocalizationSettings();
                        $data = $this->parseMessage($text);
                        $intent = BaseIntent::createIntent($state, $data);
                        $response = $intent->process();
                        $state = $intent->getState();
                    }
                }
            }
            $this->saveState($token, $state);
        } catch (Exception $exception) {
            $response = SkypeResponse::message($exception->getMessage());
        }
        $this->sendResponse($token, $botUserId, $response);
        return RESULT_SUCCESS;
    }