telegram - 如何防止我的机器人进入循环?
问题描述
我在我的机器人中创建了一个允许 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);
}
解决方案
最后,我自己设法解决了,我搜索了整个互联网,从遇到同样问题的人那里找到了线索,添加http_response_code (200)
还不够。现在每次有东西进入我都会通过方法https://core.telegram.org/bots/api#setwebhook中的参数(stristr ($ text, 'ping') &&! empty ($ ip))
清除待处理的消息drop_pending_updates
setWebhook
推荐阅读
- asp.net-mvc - 其中 + FirstOrDefault()。ArgumentNullException 值不能为空
- c++ - 如何默认初始化向量元素?
- javascript - 为什么我的 Bootstrap-Modal 不能动态加载?
- swagger - OpenAPI V2(Swagger)如何在 Base64 编码传递到授权标头之前强制 client_id 字段进行 URL 编码?
- python - 连接后保留 NaN 值
- wordpress - 在特定页面上启用 wordpress 自动加载选项
- java - Android Studio 中的简单计算器应用程序不断关闭
- triggers - 为什么 Google 跟踪代码管理器自动为我的自定义变量提供 gtm.load 的触发事件?
- python - 如何将numpy代码转换为张量流?
- python - python pandas read_excel engine=openpyxl不关闭文件