运行以下文件时,尽管我尝试了一切以确保只有一个本地进程进行 infinity_polling() 调用,但我还是收到以下错误代码 409。我已经浏览了与此相同错误代码 409 相关的所有其他堆栈溢出帖子,但我找不到任何符合我的情况或适合我的解决方案。
备注:
我尝试过的:
from TelegramBot import *
语句导入 TelegramBot.py,但我已经注释掉了最后一条语句,以查看循环导入是否存在任何问题,尽管if __name__ == '__main__':
守卫,但没有任何变化,我仍然收到错误代码 409。bot.remove_webhook()
行上方添加对 bot.infinity_polling()
的调用,但这似乎也没有改变任何内容。我不想尝试什么:
什么可以帮助我更好地理解这一点:
文件:
import telebot
import emoji
import sys
import Helpers.helper as helper
TOKEN = '*************************************'
bot = telebot.TeleBot(TOKEN, parse_mode=None)
@bot.message_handler(commands=['help'])
def send_welcome(message):
bot.reply_to(message, "// List of commands for the bot")
if __name__ == '__main__':
try:
print("before call to IP")
bot.infinity_polling()
print("after call to IP")
except (KeyboardInterrupt, SystemExit):
pass
终端输出(请注意,我在出现一条错误消息后使用 ctrl-C 来终止它):
before call to IP
2023-08-26 14:40:01,906 (__init__.py:1083 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 409. Description: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running"
2023-08-26 14:40:01,906 (__init__.py:1085 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/__init__.py", line 1073, in __threaded_polling
polling_thread.raise_exceptions()
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/util.py", line 108, in raise_exceptions
raise self.exception_info
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/util.py", line 90, in run
task(*args, **kwargs)
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/__init__.py", line 649, in __retrieve_updates
updates = self.get_updates(offset=(self.last_update_id + 1),
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/__init__.py", line 623, in get_updates
json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates, long_polling_timeout)
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/apihelper.py", line 321, in get_updates
return _make_request(token, method_url, params=payload)
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/apihelper.py", line 162, in _make_request
json_result = _check_result(method_name, result)
File "/Users/anonymous/.pyenv/versions/3.9.12/lib/python3.9/site-packages/telebot/apihelper.py", line 189, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 409. Description: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
"
^C2023-08-26 14:40:03,895 (__init__.py:966 MainThread) ERROR - TeleBot: "Infinity polling: polling exited"
2023-08-26 14:40:03,895 (__init__.py:968 MainThread) ERROR - TeleBot: "Break infinity polling"
after call to IP
这是不言自明的。您对 API 的请求存在冲突,因此状态代码为
409
。
多个机器人实例运行的问题只是本地问题吗?也就是说,可以让两台单独的机器运行此文件,向电报服务器发出单独的 getUpdates 请求,但同一台机器上不能有两个进程?
似乎只要使用相同的令牌,从哪台机器发送此类请求并不重要,如果有多个使用相同令牌的打开连接,您将不断收到 409。请使用您的令牌关闭与 pyTelegramBotAPI 的所有打开的连接。我还建议关闭可能使用此令牌的任何其他进程。
如果问题仍然存在,我建议在 github 上的 pyTelegramBotAPI 上打开一个新问题,因为他们对其源代码有深入的了解。