Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in RequestInputHandler while executing $this->acceptInput() #97

Closed
punyflash opened this issue Nov 14, 2023 Discussed in #96 · 0 comments
Closed

Error in RequestInputHandler while executing $this->acceptInput() #97

punyflash opened this issue Nov 14, 2023 Discussed in #96 · 0 comments

Comments

@punyflash
Copy link
Member

Discussed in #96

Originally posted by mgksmv November 14, 2023
cmd_UiROKoomGN

It seems like JsonStorage is not creating a file to keep track of input states. I'm using default configuration for storage (JsonStorage). Maybe there's something that I don't understand? Thanks in advance.

Here's my code.

Bot config:

'bots' => [
        'taxi' => [
            'token' => env('TELEGRAM_TAXI_BOT_TOKEN'),
            'name' => env('TELEGRAM_TAXI_BOT_NAME'),
            'api_url' => env('TELEGRAM_API_URL', 'https://api.telegram.org/bot{TOKEN}/{METHOD}'),
            'exceptions' => true,
            'async' => false,
            'storage' => \WeStacks\TeleBot\Storage\JsonStorage::class,
            'http' => [
                'http_errors' => false,
            ],

            'handlers' => [
                \App\Libs\Telegram\Handlers\Taxi\StartHandler::class,
                \App\Libs\Telegram\Handlers\Taxi\AskContactHandler::class,
            ],
        ],
    ],

Start command handler:

<?php

namespace App\Libs\Telegram\Handlers\Taxi;

use App\Libs\Telegram\Keyboards\Reply\TaxiReplyKeyboard;
use App\Libs\Telegram\Services\BotService;
use WeStacks\TeleBot\Handlers\CommandHandler;

class StartHandler extends CommandHandler
{
    protected static $aliases = ['/start'];

    public function handle(): void
    {
        $userId = $this->update->user()->id;
        $chatId = $this->update->chat()->id;

        $isUserAlreadyRegistered = BotService::checkIfUserAlreadyRegistered($userId);

        if ($isUserAlreadyRegistered) {
            $this->sendMessage([
                'chat_id' => $chatId,
                'text' => 'Вы уже регистрировались в системе!',
            ]);
        } else {
            AskContactHandler::requestInput($this->bot, $userId);

            $this->sendMessage([
                'chat_id' => $chatId,
                'text' => 'Пожалуйста, отправьте свой номер телефона в качестве контакта для регистрации. После этого мы пригласим Вас в группу таксистов.',
                TaxiReplyKeyboard::requestPhoneNumber()
            ]);
        }
    }
}

AskContactHandler for registering a user:

<?php

namespace App\Libs\Telegram\Handlers\Taxi;

use App\Libs\Telegram\Models\TelegramUser;
use Illuminate\Support\Facades\Validator;
use WeStacks\TeleBot\Handlers\RequestInputHandler;

class AskContactHandler extends RequestInputHandler
{
    public function trigger(): bool
    {
        return isset($this->update->message);
    }

    public function handle()
    {
        $message = $this->update->message;
        $data = $message->toArray();

        $validator = Validator::make($data, [
            'contact' => ['required'],
        ]);

        if ($validator->fails()) {
            return $this->sendMessage([
                'text' => 'Отправьте контакт для продолжения регистрации!'
            ]);
        }

        $this->acceptInput();

        $phone = $message->contact->phone_number;

        TelegramUser::query()->create([
            'user_id' => $message->from->id,
            'phone' => $phone,
        ]);

        return $this->sendMessage([
            'text' => 'Вы успешно зарегистрировались.',
        ]);
    }
}
```</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant