【问题标题】:How to prevent my bot from entering into a loop?如何防止我的机器人进入循环?
【发布时间】:2021-06-10 00:28:08
【问题描述】:

我在我的机器人中创建了一个功能,允许 ping 其他计算机,当用户 ping 超过 60 次时,机器人需要很长时间才能完成,而且我认为它不会使用 http 200 代码响应 Telegram,所以 Telegram 再次向它发送 ping 请求,并且循环永远不会停止。我使用这个库来构建我的机器人:https://github.com/Eleirbag89/TelegramBotPHP

代码很简单:

<?php
$telegram = new Telegram('token');
date_default_timezone_set('America/Bogota');
$chat_id = $telegram->ChatID();
$text = $telegram->Text();
$palabras = explode(" ", $text); 
foreach ($palabras as $key) {
  if (filter_var($key, FILTER_VALIDATE_IP)) {
      $ip = $key;
      $key = "";
  }
  if (is_numeric($key)){
    $cant_paq=$key;
  };
};

if (stristr($text, 'ping') && !empty($ip)) {

    $numero = !empty($cant_paq) ? $cant_paq : 4;
    exec("ping -c $numero $ip", $output, $status);
    foreach ($output as $key) {
      $resultPing .= $key . "\r\n";
    }
    $content = array('chat_id' => $chat_id, 'text' => $resultPing);
    $telegram->sendMessage($content);
}

【问题讨论】:

    标签: telegram telegram-bot php-telegram-bot telegram-webhook


    【解决方案1】:

    最后我自己解决了,我在网上搜索了一个有同样问题的人的线索,添加http_response_code (200)是不够的。现在每次有东西进入(stristr ($ text, 'ping') &amp;&amp;! empty ($ ip)) 时,我都会通过setWebhook 方法https://core.telegram.org/bots/api#setwebhook 中的drop_pending_updates 参数清除待处理的消息@

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 1970-01-01
      • 2021-05-24
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      相关资源
      最近更新 更多