Telegram Bot API setMessageReaction调用成功但消息无反应,求助排查
问题描述
Telegram 近期新增了用于设置消息反应的API函数:
setMessageReaction
此方法用于更改消息上已选的反应
我创建了一个机器人并将其添加为频道管理员,希望以机器人身份给频道发布的内容添加反应。
我尝试了以下PHP代码:
function sendToTelegram($token, $method, $response) { $ch = curl_init('https://api.telegram.org/bot' . $token . '/' . $method); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $response); curl_setopt($ch, CURLOPT_HEADER, false); $res = curl_exec($ch); curl_close($ch); return $res; } $result = sendToTelegram('BOT_KEY', 'setMessageReaction', ['chat_id' => -1001493310237, 'message_id' => 27, 'reaction[]' => "👍"]);
得到的响应是:
{"ok":true,"result":true}
但频道内容上并没有出现反应,请问哪里操作错了?
问题解决
你的代码存在两个关键错误:
1. Reaction参数格式不符合API要求
Telegram的setMessageReaction接口要求reaction参数是对象数组,每个对象必须包含type(固定值为"emoji")和emoji(表情符号)两个字段,而不是你用的reaction[]这种表单数组写法。
2. POST数据编码方式错误
你直接传递PHP数组给CURLOPT_POSTFIELDS,curl会自动将其编码为application/x-www-form-urlencoded格式,但这种格式不适合传递复杂的嵌套结构参数。Telegram API推荐使用application/json格式发送请求,才能正确解析嵌套的参数结构。
修改后的正确代码如下:
function sendToTelegram($token, $method, $data) { $ch = curl_init('https://api.telegram.org/bot' . $token . '/' . $method); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 将数据编码为JSON格式 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // 设置请求头为JSON类型 curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]); curl_setopt($ch, CURLOPT_HEADER, false); $res = curl_exec($ch); curl_close($ch); return $res; } // 正确构造reaction参数 $result = sendToTelegram('BOT_KEY', 'setMessageReaction', [ 'chat_id' => -1001493310237, 'message_id' => 27, 'reaction' => [ ['type' => 'emoji', 'emoji' => "👍"] ] ]);
另外还需要确认几个关键点:
- 机器人的频道管理员权限是否足够(至少确保没有被限制相关操作)
message_id是否准确对应频道中的目标消息,频道消息ID是独立的,不要和其他聊天的ID混淆chat_id格式正确(你用的-100开头的超级频道ID格式是对的)
内容的提问来源于stack exchange,提问作者Webkilla
相关产品推荐
相关产品推荐

