irazasyed / telegram-bot-sdk

🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
https://telegram-bot-sdk.com
BSD 3-Clause "New" or "Revised" License
3.05k stars 673 forks source link

How to catch CallbackQuery object via Webhook? #350

Closed behnamazimi closed 7 years ago

behnamazimi commented 7 years ago

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!

jsina commented 7 years ago

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

manzoorwanijk commented 7 years ago
$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'
    ]);
}
shuni95 commented 7 years ago

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

irazasyed commented 7 years ago

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 :)