I’m working on a project where I need to run several Telegram bots using the Python Telegram Bot library. Each bot has its own unique commands, functions, and tokens, and they operate on different channels. I have the basic structure for a single bot, but I am unsure how to integrate multiple bots into one script.
Here’s a modified version of the code I’m using for each bot:
import telegram
from telegram.ext import Updater, CommandHandler
def welcome(update, context):
update.message.reply_text('Hello! Use /help to see available commands.')
def initiate_bot():
updater = Updater('YOUR_BOT_TOKEN', use_context=True)
updater.dispatcher.add_handler(CommandHandler('welcome', welcome))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
initiate_bot()
I’ve researched async frameworks, but I’m not clear on how to apply them here. Can someone explain an efficient way to run multiple bots within a single script? Thanks!
hey, i’ve dealt with multiple bots before. you could try using multiprocessing instead of threading or asyncio. it’s pretty straightforward:
from multiprocessing import Process
from telegram.ext import Updater
def run_bot(token):
updater = Updater(token, use_context=True)
# Add handlers here
updater.start_polling()
if __name__ == '__main__':
Process(target=run_bot, args=('BOT1_TOKEN',)).start()
Process(target=run_bot, args=('BOT2_TOKEN',)).start()
this way each bot runs in its own process, which can be more efficient for CPU-bound tasks.
I’ve tackled this issue before in a project where I needed to manage multiple Telegram bots simultaneously. The key is to leverage Python’s asyncio library along with the python-telegram-bot’s application builder.
Here’s a basic structure that worked well for me:
import asyncio
from telegram.ext import ApplicationBuilder, CommandHandler
async def bot1_welcome(update, context):
await update.message.reply_text('Welcome to Bot 1!')
async def bot2_welcome(update, context):
await update.message.reply_text('Welcome to Bot 2!')
async def main():
bot1 = ApplicationBuilder().token('BOT1_TOKEN').build()
bot2 = ApplicationBuilder().token('BOT2_TOKEN').build()
bot1.add_handler(CommandHandler('welcome', bot1_welcome))
bot2.add_handler(CommandHandler('welcome', bot2_welcome))
await bot1.initialize()
await bot2.initialize()
await bot1.start()
await bot2.start()
await bot1.run_polling()
await bot2.run_polling()
if __name__ == '__main__':
asyncio.run(main())
This approach allows you to run multiple bots concurrently without blocking. Each bot can have its own set of handlers and functions. Just remember to handle potential exceptions and implement proper logging for each bot.
Having managed multiple bots in a single script before, I can offer some insights. While the asyncio approach is viable, I’ve found that using threading can be more straightforward and equally effective.
Here’s a simplified example of how you could structure your code:
import threading
from telegram.ext import Updater, CommandHandler
def run_bot(token, handlers):
updater = Updater(token, use_context=True)
for command, handler in handlers.items():
updater.dispatcher.add_handler(CommandHandler(command, handler))
updater.start_polling()
if __name__ == '__main__':
bot1_handlers = {'welcome': bot1_welcome}
bot2_handlers = {'welcome': bot2_welcome}
threading.Thread(target=run_bot, args=('BOT1_TOKEN', bot1_handlers)).start()
threading.Thread(target=run_bot, args=('BOT2_TOKEN', bot2_handlers)).start()
This method allows each bot to run independently in its own thread, simplifying management and scaling.