我的电报机器人不停地发送消息

Ham*_*deh 5 bots telegram telegram-bot php-telegram-bot

我开始编程电报机器人,我遇到了问题.当我发送/启动命令时,它向我发送一条欢迎消息(正如我编程的那样),但它不发送一次!它像循环一样无休止地发送它!这是来源:

    <?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>');
}

?>
Run Code Online (Sandbox Code Playgroud)

Yoi*_*y L 7

你可能正在使用webhook.
如果您没有以http状态200响应,则电报机器人API会认为服务器出现问题,并且每隔几秒就会再次请求,如API文档中所述:

如果请求不成功,我们将在合理的尝试后放弃.

所以只需添加header("HTTP/1.1 200 OK");到您的脚本并vo!
(如果你的php版本大于5.4你可以使用http_response_code(200);)