Telegram-bot-sdk: How to catch CallbackQuery object via Webhook?

Created on 11 Mar 2017  路  4Comments  路  Source: irazasyed/telegram-bot-sdk

I wanna send a message when a inline button attached, but I don't know How to catch CallbackQueryobject via Webhook.
Maybe a simple example be enough!

Most helpful comment

Here's a solution if using V3.x dev with laravel-starter.

Thanks to all the others for posting their solutions BTW. Highly appreciate helping each other here :)

All 4 comments

dear @behnamazimi
If you find some point about this problem please notice me here.

$update = $telegram->getWebhookUpdates();
// check if update contains callback_query
if ( isset($update['callback_query']) ){
        // extract what is required
    $chat_id = $callback_query['message']['chat']['id'];// to be used for sendMessage
    $user_id = $callback_query['from']['id'];
    $callbackdata = $callback_query['data'];
    $message_id = $callback_query['message']['message_id'];
    $text = $callback_query['message']['text'];

    // then send the message you want
    $res = $telegram->sendMessage([
      'chat_id' => $chat_id, 
      'text' => 'Your message'
    ]);
}

I guess you are using commandsHandler. Well, I manage with the next, i put commands on callback_data of inline keyboard :joy:

$commands = [ /**/ ];
$update = Telegram::commandsHandler(true);

if ($update->isType('callback_query')) {
    $query = $update->getCallbackQuery();
    $data  = $query->getData();
    $start = strpos($data, ' ');

    $command = ($start !== false) ? substr($data, 1, $start - 1) : substr($data, 1);

    if (in_array($command, $commands)) {
        $update->put('message', collect([
            'text' => substr($data, $start + 1),
            'from' => $query->getMessage()->getFrom(),
            'chat' => $query->getMessage()->getChat()
        ]));
       Telegram::triggerCommand($command, $update);
    }
}

I am using V3 :stuck_out_tongue_closed_eyes: Maybe helps you

Here's a solution if using V3.x dev with laravel-starter.

Thanks to all the others for posting their solutions BTW. Highly appreciate helping each other here :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Enelar picture Enelar  路  3Comments

BehroozBvk picture BehroozBvk  路  5Comments

YaroslavStrigun picture YaroslavStrigun  路  4Comments

Visionary89 picture Visionary89  路  5Comments

nanangkoesharwanto picture nanangkoesharwanto  路  3Comments