Hey everyone! I’m stuck with my Python Telegram Bot project. I’m trying to set up a feature where my account gets update messages every now and then. But when I run the code, I keep getting this annoying error:
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
I’ve set up a subscribe command that should start sending these periodic messages, but it’s not working as expected. I’m using a ProcessManager class to handle the background tasks.
Here’s a simplified version of what I’m trying to do:
import telebot
from custom_process_manager import ProcessManager
bot = telebot.TeleBot('my_bot_token', threaded=True)
process_manager = ProcessManager()
@bot.message_handler(commands=['subscribe'])
def subscribe(message):
user_id = message.from_user.id
if not process_manager.is_subscribed(user_id):
process_manager.start_update_process(user_id, send_update, user_id, 'Update message')
bot.reply_to(message, 'You are now subscribed to updates!')
else:
bot.reply_to(message, 'You are already subscribed.')
def send_update(user_id, text):
bot.send_message(user_id, text)
bot.infinity_polling()
Can anyone spot what I’m doing wrong here? Thanks in advance for any help!
I have encountered this problem before and found that the error usually appears when more than one instance of the bot is running concurrently. I solved the issue by checking all active processes and making sure only a single instance remains active. In my case, switching from infinity_polling() to bot.polling(none_stop=True, interval=0) improved the long-running performance. I also explored using asynchronous libraries such as aiogram or python-telegram-bot, which handle concurrency much better. It is important as well to review your ProcessManager implementation to confirm it does not inadvertently start multiple bot instances.
The issue you’re facing might be related to how your ProcessManager is handling the background tasks. One approach that worked for me was using the schedule library instead of a custom process manager. It allows for easy scheduling of periodic tasks without conflicting with the main bot instance.
Here’s a simplified example of how you could modify your code:
import telebot
import schedule
import time
import threading
bot = telebot.TeleBot('your_token')
subscribed_users = set()
def send_update():
for user_id in subscribed_users:
bot.send_message(user_id, 'Update message')
@bot.message_handler(commands=['subscribe'])
def subscribe(message):
user_id = message.from_user.id
if user_id not in subscribed_users:
subscribed_users.add(user_id)
bot.reply_to(message, 'You are now subscribed to updates!')
else:
bot.reply_to(message, 'You are already subscribed.')
schedule.every(1).hours.do(send_update)
def run_scheduler():
while True:
schedule.run_pending()
time.sleep(1)
scheduler_thread = threading.Thread(target=run_scheduler)
scheduler_thread.start()
bot.polling(none_stop=True)
This approach avoids conflicts between multiple bot instances and simplifies the periodic message sending process.
hey mike, i’ve seen this before. the error happens when multiple bot instances run at once. try using bot.polling(none_stop=True) instead of infinity_polling(). also, double-check ur ProcessManager to make sure it’s not starting extra bot instances. if that doesn’t work, you might wanna look into async libraries like aiogram for better concurrency handling.