C
C
Cyril2020-05-19 12:05:48
PHP
Cyril, 2020-05-19 12:05:48

Telegram Bot: How to get not only a callback from inlinekeyboard, but also a manually entered message?

Bot using telegram-bot/api. There is a code (abbreviated):

Bot Code
<?php
include('vendor/autoload.php'); 
include('admin/include/database.php'); 

function startMess($bot, $message, $db)
{
    ...
    foreach ($arDBStreets as $arDBStreet) {
        $arStreets[] = [
            ['callback_data' => 'street__' . $arDBStreet['id'], 'text' => ' ' . $arDBStreet['name']]
        ];
    }
    $arStreets[] = [
        ['callback_data' => 'message__1', 'text' => 'Перейти к разделам']
    ];

    $keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup($arStreets);
    $sendedStartMessage = $bot->sendMessage($message->getChat()->getId(), $mess, false, null, null, $keyboard);
    
...

}

...

try {
    $bot = new \TelegramBot\Api\Client($settings['telegram']['token']);

    $bot->command('start', function ($message) use ($bot) {
        global $db;

       ...

        startMess($bot, $message, $db);
    });

    $bot->on(function ($update) use ($bot) {
        global $db;

        $callback = $update->getCallbackQuery();
        $message = $callback->getMessage();
        $data = $callback->getData();

        $bot->answerCallbackQuery($callback->getId());

        $arData = explode('__', $data);

        if ($arData[0] == 'street') {
            houseMess($bot, $message, $db, $arData[1]);
        }
        ...
    }, function ($update) {
        $callback = $update->getCallbackQuery();
        if (is_null($callback) || !strlen($callback->getData()))
            return false;
        return true;
    });

    $bot->run();

} catch (\TelegramBot\Api\Exception $e) {
    $e->getMessage();
}



Please tell me how to catch messages from the user not from inlinekeyboard, but simply sent to the bot as a message. For example, I ask a question - enter the age and you need to receive a message from the user with his age.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cyril, 2020-05-19
@ldeimosl

Yes, I found the answer. I rummaged through the Client class, there is a getRawBody () method. So it contains everything that comes from the client - both messages and callback

D
dx4321, 2020-05-19
@dx4321

I think it's possible to take a user's message and see if it contains a command to process. Then take the command from the message and use the rest of the text as needed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question