zarincheg / telegram-bot-dialogs

The extension for Telegram Bot API PHP SDK that allows to implement dialogs in bots
MIT License
40 stars 29 forks source link

Validating answer #15

Open sigaev-pro opened 6 years ago

sigaev-pro commented 6 years ago

Hello, I need to validate answer for a step and, if it's wrong, I need to send message with error and stay on the step and give user a next try.

class AddBotDialog extends BaseDialog
{

    protected $steps = ['init', 'key'];

    public function init()
    {
        $this->replyWithMessageTpl('master/add-bot-info');
    }

    public function key()
    {
        $message_text = $this->update->getMessage()->getText();
        $match_count = preg_match('/(\d+:[a-zA-Z\d-_]+)/ixs', $message_text, $matches);
        if($match_count == 1) {
            $token = $matches[1];

            $telegram = new TeleApi($token);
            try {
                $about = $telegram->getMe();
            }catch(TelegramResponseException $e) {
                $this->replyWithMessageTpl('master/add-bot-error-unauthorized', ['token' => $token]);
                $this->jump('key');
                return;
            }

            if(!TelegramBot::where(['id' => $about->getId()])->first()) {
                // IT IS OK
            } else {
               // ERROR STAY ON THE STEP
                $this->replyWithMessageTpl('master/add-bot-error-exists');
                $this->jump('key');
                return;
            }
        } else {
            // ERROR STAY ON THE STEP
            $this->replyWithMessageTpl('master/add-bot-error-key');
            $this->jump('key');
            return;
        }

    }

}

This is my code, it's reply correctly first time if it's error, but not after second. What i'm doing wrong?