尽管有单个实例正在运行,但 Telegram Bot 错误代码 409

问题描述 投票:0回答:1

运行以下文件时,尽管我尝试了一切以确保只有一个本地进程进行 infinity_polling() 调用,但我还是收到以下错误代码 409。我已经浏览了与此相同错误代码 409 相关的所有其他堆栈溢出帖子,但我找不到任何符合我的情况或适合我的解决方案。

备注:

  • MacOS 文图拉 13.5
  • VSCode 1.81.1(通用)
  • 我的项目仓库中的 Python 3.9.12 使用 pyenv 2.3.24
  • pip3 列表包含 pyTelegramBotAPI 4.13.0 和 telebot 0.0.5,但我的同事运行相同的代码库,仅安装 pyTelegramBot 4.9.0 而没有安装 telebot,没有问题。
  • pip 23.2.1 来自 /Users/********/.pyenv/versions/3.9.12/lib/python3.9/site-packages/pip (python 3.9)
  • 我团队的另一名成员运行相同的代码来从不同的机器执行测试,并且效果很好

我尝试过的:

  • 重新启动我的电脑
  • 追踪活动监视器中的每个 python 和 vscode 进程并杀死它们
  • 删除整个本地存储库并重新克隆它
  • 卸载 pyTelegramBotAPI 和 telebot 并通过 pip3 重新安装它们
  • 检查导入语句:TelegramBot.py 导入 helper.py,它导入 fileNo2.py,后者使用
    from TelegramBot import *
    语句导入 TelegramBot.py,但我已经注释掉了最后一条语句,以查看循环导入是否存在任何问题,尽管
    if __name__ == '__main__':
    守卫,但没有任何变化,我仍然收到错误代码 409。
  • 像我的同事一样仅使用 pyTelegramBotAPI 版本 4.9.0 并卸载 telebot,但这似乎不会影响任何内容。
  • bot.remove_webhook()
    行上方添加对
    bot.infinity_polling()
    的调用,但这似乎也没有改变任何内容。

我不想尝试什么:

  • 如果我最终不必创建新的代币或新的电报机器人,我会非常高兴:还有其他与我一起工作的人会让这件事变得复杂

什么可以帮助我更好地理解这一点:

  • 多个机器人实例运行的问题只是本地问题吗?也就是说,可以让两台单独的机器运行此文件,向电报服务器发出单独的 getUpdates 请求,但同一台机器上不能有两个进程?
  • 如果我重新启动计算机,另一个调用 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
python telegram telegram-bot python-telegram-bot py-telegram-bot-api
1个回答
0
投票

这是不言自明的。您对 API 的请求存在冲突,因此状态代码为

409

多个机器人实例运行的问题只是本地问题吗?也就是说,可以让两台单独的机器运行此文件,向电报服务器发出单独的 getUpdates 请求,但同一台机器上不能有两个进程?

似乎只要使用相同的令牌,从哪台机器发送此类请求并不重要,如果有多个使用相同令牌的打开连接,您将不断收到 409。请使用您的令牌关闭与 pyTelegramBotAPI 的所有打开的连接。我还建议关闭可能使用此令牌的任何其他进程。

如果问题仍然存在,我建议在 github 上的 pyTelegramBotAPI 上打开一个新问题,因为他们对其源代码有深入的了解。

© www.soinside.com 2019 - 2024. All rights reserved.