When running the below file I get the following error code 409 despite trying everything to ensure that I only have one local process making the infinity_polling() call. I have looked through all of the other stack overflow posts relating to this same error code 409 but I can’t find any that match my situation or whose solutions work for me.
Notes:
- MacOS Ventura 13.5
- VSCode 1.81.1 (Universal)
- Python 3.9.12 in my project’s repo which is using pyenv 2.3.24
- pip3 list contains pyTelegramBotAPI 4.13.0 and telebot 0.0.5, but my associate running the same code base has no issue with just pyTelegramBot 4.9.0 installed and no telebot.
- pip 23.2.1 from /Users/*******/.pyenv/versions/3.9.12/lib/python3.9/site-packages/pip (python 3.9)
- another member of my team runs the same code to perform tests from a different machine and that works just fine
What I’ve tried:
- restarting my computer
- tracking down every python and vscode process in activity monitor and killing them
- deleting the whole local repo and re-cloning it
- uninstalling pyTelegramBotAPI and telebot and re installing them via pip3
- checking import statement: TelegramBot.py imports helper.py which imports fileNo2.py which imports TelegramBot.py with a
from TelegramBot import *
statement, but I’ve commented out that last statement to see if there is any issue with circular imports despite theif __name__ == '__main__':
guard but nothing changes and I still get the error code 409. - using just pyTelegramBotAPI version 4.9.0 like my colleague and uninstalling telebot but this does not appear to affect anything.
- adding a call to
bot.remove_webhook()
just above thebot.infinity_polling()
line, but this also doesn’t appear to change anything.
What I would prefer not trying:
- I would be really happy if I don’t end up having to create a new token or a new telegram bot: there are other people with whom I’m working that would make this complicated
What would help me understand this better:
- Is the issue with the multiple bot instances running just a local issue? i.e. it’s ok to have two separate machines running this file making separate getUpdates requests to the telegram servers but not two processes on the same machine?
- How could another process that calls bot.infinity_polling() be running if I’ve restarted my computer? Doesn’t restarting a computer kill all processes by definition?
The file:
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
The terminal output (note that I use ctrl-C to kill it after one error message):
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
2
Answers
This is quite self-explanatory. There is something that your request to the API conflicts with, thus the status code
409
.It seems like that it doesn’t matter from which machine such requests are sent as long as they are using the same token, you will keep receiving a 409 if there are multiple open connections using the same token. Please close all open connections to the pyTelegramBotAPI using your token. I also suggest closing any other processes that might be using this token.
If the issue persists I would propose opening a new issue at pyTelegramBotAPI on GitHub, as they have the in depth knowledge of their source code.
Just try
bot.delete_webhook()
inside your script.