我有一台电报机,它能正常工作几个月。大约3-4周前,电报机器人有时会发送(!)同样的信息有几次--不仅仅是一次!
我已经尝试过很多方法来解决这个问题:
慢慢地,我的想法到了尽头。也许有人能给出一个很好的答案。
我总是有$update和聊天ID:
define('api', 'https://api.telegram.org/bot'.token.'/');
$data = file_get_contents("php://input");
$update = json_decode($data, true);
$cbid = $update["callback_query"]["from"]["id"];我的功能如下:
function callback($up){
return $up["callback_query"];
}
function tg_send_message($id, $text) {
$params=[
'chat_id' => $id,
'text' => $text,
'parse_mode' => 'Markdown',
];
$ch = curl_init('https://api.telegram.org/bot'.token.'/sendMessage?limit=1');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3500);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3500);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}如果有人点击“内联”按钮“前10名”,我想发送一条消息:
if(callback($update) and $cbdata == "top_10"){
tg_send_message($cbid, "This is my message!");
}一切都很好,而且现在也在工作--机器人只是在某个时候发送一条消息多次。
额外的消息总是有一个新的update_id!如果另一个API有一个可变值(比如比特币价格),它也在发生变化。就像你点击了22次按钮(但你只点击了一次)
我非常感谢每一个有用的答案!非常感谢。
诚挚的问候。
编辑:我也尝试过以下几种方法:
function tg_btn_click_send_message($id, $text) {
$response = $update["callback_query"];
$botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "/answerCallbackQuery";
$postFields = array('callback_query_id' => $callback_query_id, 'text' => $response);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $botUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$output = curl_exec($ch);
//send Text
header("Content-Type: application/json");
$parameters = array('chat_id' => $id, "text" => $text);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);
}就像这里建议的:php telegram answercallbackquery sendmessage
但也没用。
发布于 2018-11-28 21:38:02
如果有人有同样的问题,这里的解决方案是:
function tg_answer_callback_query($cbq_id) {
$params=[
'callback_query_id' => $cbq_id
];
$ch = curl_init('https://api.telegram.org/bot'.token.'/answerCallbackQuery');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3500);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3500);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}在if(回调($update))的末尾运行此代码(而不是在它之后!)而且起作用了!
https://stackoverflow.com/questions/53522294
复制相似问题