如何区分“消息"更新和“回调查询"更新?(电报机器人 API)

问题描述:

对不起,如果我的问题太乱了,我是新来的,所以,欢迎任何建议.

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?

if (($update['message']) != null) {

} else if ($update['callback_query'] != Null) {

根据电报文档:

至多一个可选参数可以出现在任何给定的更新.

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

所以你只需要检查其中哪一个不是 Null.

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