最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Pyhon Telegram Bot Multiple Bots - Stack Overflow

programmeradmin1浏览0评论

I have multiple bots that utilize Python Telegram Bot module. Each bot has code like:

from telegram.ext import Updater, CommandHandler

def start(update, context):
    update.message.reply_text("Command List:\n/start - Display this message")

def main():
    updater = Updater(token='XXXXXXX:XXXXXXX-XXXXXXXXXXX', use_context=True)
    updater.dispatcher.add_handler(CommandHandler('start', start)) # One example of a bot command
    # ... Several CommandHandler and ConversationHandler functions ...
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

I found this documentation for running multiple asyncio frameworks but I could not find a specific example that creates multiple instances of python telegram bot. The bots I have run on completely separate channels and have different set of commands/functions/tokens. How would I have one script for multiple bots?

I have multiple bots that utilize Python Telegram Bot module. Each bot has code like:

from telegram.ext import Updater, CommandHandler

def start(update, context):
    update.message.reply_text("Command List:\n/start - Display this message")

def main():
    updater = Updater(token='XXXXXXX:XXXXXXX-XXXXXXXXXXX', use_context=True)
    updater.dispatcher.add_handler(CommandHandler('start', start)) # One example of a bot command
    # ... Several CommandHandler and ConversationHandler functions ...
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

I found this documentation for running multiple asyncio frameworks but I could not find a specific example that creates multiple instances of python telegram bot. The bots I have run on completely separate channels and have different set of commands/functions/tokens. How would I have one script for multiple bots?

Share Improve this question asked Mar 18 at 16:49 BijanBijan 8,72221 gold badges102 silver badges162 bronze badges 7
  • There are multiple ways of doing that. Questions is more what do you need? Do Bot1 and Bot2 need to be able to reply at the same time? Or can you process Bot2 after Bot1? Asking 'for the best way' makes this more of an opinion based question. – 0stone0 Commented Mar 18 at 20:20
  • That said, "How would I have one script for multiple bots", eventually you'll need that split that single script into multiple files if the bots get to big. So why not start with a 'BotManager' that uses 2 Asyncio threads to start 2 bots from different classes/files – 0stone0 Commented Mar 18 at 20:22
  • github/python-telegram-bot/python-telegram-bot/wiki/… – 0stone0 Commented Mar 18 at 20:38
  • @0stone0 Bot1 and Bot2 never reply at the same time. It is okay to have Bot2 process after Bot1. I reference that FAQ page in my original post but I do not understand how to use that code to have 2 PTB bots. The main reason is that I have a lot of simple bots that only have 1 or 2 commands associated with it and I am trying to simplify my environment to have 1 script that initializes all the bots. – Bijan Commented Mar 18 at 20:47
  • (Can't you combine them into a single bot?) - Typing this from my phone, might have some time coming days to make you a working example – 0stone0 Commented Mar 18 at 21:18
 |  Show 2 more comments

1 Answer 1

Reset to default 0

The most simple solution is probably to create a script (.bat/.sh/...) which will run a proccess for each bot by passing different tokens to main(). This way you can put them into different tmux session, cmd windows and etc.

发布评论

评论列表(0)

  1. 暂无评论