pythontelegramtelegram-botpy-telegram-bot-api

Telegram Bot Error Code 409 Despite There Being a Single Instance Running


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:

What I've tried:

What I would prefer not trying:

What would help me understand this better:

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

Solution

  • This is quite self-explanatory. There is something that your request to the API conflicts with, thus the status code 409.

    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?

    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.