Y
Y
Yerlan Ismanov2019-12-01 12:13:48
PHP
Yerlan Ismanov, 2019-12-01 12:13:48

How to track just TelegramBot text?

I use the TelegramBot\Api library . I can receive commands, but I cannot receive simple text. How can this be done?

$bot = new \TelegramBot\Api\Client($token);
$botApi = new \TelegramBot\Api\BotApi($token);

$bot->command('start', function($message) use ($bot) {
        $chatId = $message->getChat()->getId();
        $answer = "Здравствуйте!";
        $bot->sendMessage($chatId, $answer);
});

$bot->run();

And another question, how to register? I can write data to the database, I did a little logging. To, for example, after entering /register, ask for a login and when the user writes, track what he wrote exactly after the /register command.
So far I have done so, but I would like the command and the login itself to be in different messages.
$bot->command('register', function($message) use ($bot) {
        $chatId = $message->getChat()->getId();
        $text = $message->getText();
        $param = str_replace('/register', '', $text);
        $answer = 'Укажите пожалуйста логин';
        if(!empty($param)) {
            $answer = "Ваш логин: $param";
        }
        $bot->sendMessage($chatId, $answer);
});

Thanks in advance to all who respond.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zudwa, 2019-12-03
@winrunner

The text can be obtained in this way:

$body = json_decode($bot->getRawBody(), true);
$message = isset($body['message']['text']) ? mb_strtolower($body['message']['text']) : '';

Regarding registration, for example, after entering the /register command in the database, you can store some kind of flag, which will mean that the user entered this command and, accordingly, now he is logging in.
Roughly speaking, before entering the command, the entry in the database looks like this:
user_id|reg_process
123    | 0

At this point, it is considered that the user enters anything except the login.
The user enters the /register command and the entry in the table takes the form:
user_id|reg_process
123    | 1

At this moment, it is considered that the user enters only the login and each of his messages is processed by some function for checking the login.
Accordingly, with each message received, you check reg_process for a specific user_id.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question