如何区分“消息"更新和“回调查询"更新? (Telegram Bot API) [英] How can I differentiate between a 'Message' update and a 'Callback Query' update? (Telegram Bot API)

查看:177
本文介绍了如何区分“消息"更新和“回调查询"更新? (Telegram Bot 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 */
}

因此,此代码可以处理消息,但不能处理CallbackQueries.如果我想处理这些问题,可以使用类似这样的方法(基于另一个问题的答案):

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.

这篇关于如何区分“消息"更新和“回调查询"更新? (Telegram Bot API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆