bots - 我的 Telegram Bot 不断地发送消息

标签 bots telegram telegram-bot php-telegram-bot

我开始编写 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/

相关文章:

python-3.x - 在python中添加与telethon的联系

python - 如何通过 python-telegram-bot 发送照片

telegram - 我如何以编程方式获取 Telegram Bot 信息

javascript - Discord.js V12 粗鲁言语过滤器不起作用

facebook - 如何使用单一平台创建多个 Facebook 机器人?

telegram - 我可以将 Telegram API 用于我的商业应用程序吗?

telegram - 如何将 Telegram Bot 添加到我不是管理员的 Telegram 组

Python IRC Bot 链接解析器?

javascript - 机器人不接收消息

java - 如何更改 Telegram Bot 消息中的图片?