skip to Main Content

Sorry if my question gets too messy, I’m new here, so, any advice is welcome.

How can I differentiate between a ‘Message’ update and a ‘Callback Query’ update?
I’ve managed to make an inline keyboard, but when I use it, the bot just hangs, he doesn’t reply anything. I did a little bit of research and found this question, which helped me understand the problem, but not much else.

My bot uses something like this right now:

// read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];

switch($update["message"]["text"]){
    /* insert magic here */
}

So, this code can handle Messages, but not CallbackQueries. If I wantew to handle them, I could use something like this (based on the other question’s answer):

$callback_query = $update["callback_query"]
/* same as above */

But how can I check whether it is a message or a callback query?

2

Answers


  1. You can simply check if CallbackQuery is null or not.
    See the Telegram docs:

    CallbackQuery

    This object represents an incoming callback query from a callback
    button in an inline keyboard. If the button that originated the query
    was attached to a message sent by the bot, the field message will be
    present. If the button was attached to a message sent via the bot (in
    inline mode), the field inline_message_id will be present. Exactly one
    of the fields data or game_short_name will be present.

    Login or Signup to reply.
  2. if (($update['message']) != null) {
    
    } else if ($update['callback_query'] != Null) {
    

    According to telegram Docs:

    At most one of the optional parameters can be present in any given
    update.

    so you just need to check which one of them is not Null.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search