Core: Redirect Callback to Message handler

Created on 12 May 2016  路  2Comments  路  Source: php-telegram-bot/core

I can handle a callback query with an overwrited CallbackqueryCommand.PHP and then I do the following:

return $this->telegram->executeCommand('command', $this->update);

There I have the following lines for a message (not a callback):

        $message = $this->getMessage();
        $chat_id = $message->getChat()->getId();
        $user_id = $message->getFrom()->getId();
        $text = $message->getText(true);

Is it possible to transform it to callback entity? I've tried several times without any success.

Thank you

Most helpful comment

This is how I handle commands that can be used with either command/message or callback query:

$message = $this->getMessage();
$callback_query = $this->getUpdate()->getCallbackQuery();

if ($message) {
    $chat_id = $message->getChat()->getId();
    $user_id = $message->getFrom()->getId();
    $text = $message->getText(true);
} elseif ($callback_query) {
    $chat_id = $callback_query->getMessage()->getChat()->getId();
    $user_id = $callback_query->getFrom()->getId();
    $text = $callback_query->getData();

    $data_callback = [];
    $data_callback ['callback_query_id'] = $callback_query->getId();
}

// your code

$data = [];
$data['chat_id'] = $chat_id;
$data['parse_mode'] = 'HTML';
$data['disable_web_page_preview'] = true;
$data['text'] = $text;

if ($inline_keyboard) {
    $data['reply_markup'] = new InlineKeyboardMarkup(['inline_keyboard' => $inline_keyboard]);
}

if ($message) {
    return Request::sendMessage($data);
} elseif ($callback_query) {
    Request::answerCallbackQuery($data_callback);
    $data['message_id'] = $callback_query->getMessage()->getMessageId();
    return Request::editMessageText($data);
} else {
    return Request::emptyResponse();
}

All 2 comments

This is how I handle commands that can be used with either command/message or callback query:

$message = $this->getMessage();
$callback_query = $this->getUpdate()->getCallbackQuery();

if ($message) {
    $chat_id = $message->getChat()->getId();
    $user_id = $message->getFrom()->getId();
    $text = $message->getText(true);
} elseif ($callback_query) {
    $chat_id = $callback_query->getMessage()->getChat()->getId();
    $user_id = $callback_query->getFrom()->getId();
    $text = $callback_query->getData();

    $data_callback = [];
    $data_callback ['callback_query_id'] = $callback_query->getId();
}

// your code

$data = [];
$data['chat_id'] = $chat_id;
$data['parse_mode'] = 'HTML';
$data['disable_web_page_preview'] = true;
$data['text'] = $text;

if ($inline_keyboard) {
    $data['reply_markup'] = new InlineKeyboardMarkup(['inline_keyboard' => $inline_keyboard]);
}

if ($message) {
    return Request::sendMessage($data);
} elseif ($callback_query) {
    Request::answerCallbackQuery($data_callback);
    $data['message_id'] = $callback_query->getMessage()->getMessageId();
    return Request::editMessageText($data);
} else {
    return Request::emptyResponse();
}

Thank you! It works for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Recouse picture Recouse  路  3Comments

dorcu picture dorcu  路  3Comments

sayjeyhi picture sayjeyhi  路  3Comments

esomkin picture esomkin  路  3Comments

TheSleepySlee picture TheSleepySlee  路  3Comments