我开始编写 Telegram Bot 程序,但遇到了问题。当我发送/start 命令时,它会向我发送一条欢迎消息(如我编程的那样),但它不会发送一次!它像循环一样不断发送! 这是来源:
<?php
define('API_KEY','<token>');
function makereq($method,$datas=[])
{
$url = "https://api.telegram.org/bot".API_KEY."/".$method;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($datas));
$res = curl_exec($ch);
if(curl_error($ch)){
var_dump(curl_error($ch));
}else{
return json_decode($res);
}
}
$website = "https://api.telegram.org/bot".API_KEY;
$update = json_decode(file_get_contents('php://input'));
$chat_id = $update->message->chat->id;
$message_id = $update->message->message_id;
$from_id = $update->message->from->id;
$name = $update->message->from->first_name;
$username = $update->message->from->username;
$textmessage = isset($update->message->text)?$update->message->text:'';
$reply = $update->message->reply_to_message->forward_from->id;
$stickerid = $update->message->reply_to_message->sticker->file_id;
$messageEntity = $update->messageentity->type;
function SendMessage($ChatId, $TextMsg)
{
makereq('sendMessage',[
'chat_id'=>$ChatId,
'text'=>$TextMsg,
'parse_mode'=>"MarkDown"]
);
}
if($textmessage == '/start')
{
SendMessage($chat_id,'<welcome message>');
}
?>
最佳答案
您可能正在使用 webhook。
如果您不使用 http status 200 进行响应,telegram bot API 会认为您的服务器出现问题,并每隔几秒再次请求 mentioned in the API documentation :
In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
因此,只需将 header("HTTP/1.1 200 OK");
添加到您的脚本中即可!
(如果您的 php 版本大于 5.4,您可以使用 http_response_code(200);
)
关于bots - 我的 Telegram Bot 不断地发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593993/