How can I differentiate between a 'Message'

2019-05-11 13:17发布

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条回答
Explosion°爆炸
2楼-- · 2019-05-11 13:44
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.

查看更多
姐就是有狂的资本
3楼-- · 2019-05-11 14:03

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.

查看更多
登录 后发表回答