From 69501beb65e0fd33dc932ac161962d86ef107744 Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Thu, 14 Jan 2016 12:36:37 +0000 Subject: [PATCH] multibot process example --- README.md | 11 +++++-- multibot.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +- 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100755 multibot.py diff --git a/README.md b/README.md index caf1010..abcba7d 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,20 @@ Plugin Examples for [tgbotplug](https://github.com/fopina/tgbotplug) git clone https://github.com/fopina/tgbotplug-plugins/ pip install -r requirements.txt -And then either run _runit.py_ (after adding your bot token) or you can run the library directly from command line passing these plugins (and your own) as parameters: +And then either run _bot.py_ (specifying your bot token) or you can run the library directly from command line passing these plugins (and your own) as parameters: python -m tgbot -t YOUR_BOT_TOKEN -n plugins.simsimi.SimsimiPlugin \ plugins.echo.EchoPlugin plugins.random.RandomPlugin plugins.google.GooglePlugin \ plugins.guess.GuessPlugin plugins.admin.AdminPlugin - + _echo_ and _random_ are the simplest plugins, use _TelegramBot.send_message_ and _TGBot.need_reply_ _google_ is an example on how to use _TelegramBot.send_photo_ _guess_ shows off plugin data persistence using _TGPluginBase.save_data_ and _TGPluginBase.read_data_ Check _simsimi_ for a non-command plugin example. + +You can also check _multibot.py_ for an example on how to run multiple bots in the same process. +You can run try it with: + + ./multibot.py --token1 YOUR_BOT1_TOKEN --token2 YOUR_BOT2_TOKEN + +`BOT1` will answer to `/echo` command and `BOT2` to `/random`. diff --git a/multibot.py b/multibot.py new file mode 100755 index 0000000..d571cad --- /dev/null +++ b/multibot.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# coding=utf-8 + +import tgbot +from plugins.echo import EchoPlugin +from plugins.random_choice import RandomPlugin +import argparse + +from requests.packages import urllib3 +urllib3.disable_warnings() + + +def main(): + args = build_parser().parse_args() + + bot_dbs = ['test1', 'test2'] + + bots = [ + tgbot.TGBot( + args.token1, + plugins=[ + EchoPlugin(), + ], + db_url=args.db_url % bot_dbs[0] + ), + tgbot.TGBot( + args.token2, + plugins=[ + RandomPlugin(), + ], + db_url=args.db_url % bot_dbs[1] + ) + ] + + if args.list: + for i, bot in enumerate(bots): + print 'Bot %d: %s' % (i + 1, bot_dbs[i]) + bot.print_commands() + print + return + + if args.create_db: + for bot in bots: + bot.setup_db() + print 'DB created' + return + + if args.webhook is None: + from tgbot.tgbot import run_bots + run_bots(bots, polling_time=args.polling) + else: + for bot in bots: + bot.set_webhook(args.webhook[0] + '/update/' + bot.token) + + from tgbot.webserver import run_server + run_server(bots, host='0.0.0.0', port=int(args.webhook[1])) + + +def build_parser(): + parser = argparse.ArgumentParser(description='Run TestBot') + + parser.add_argument('--polling', '-p', dest='polling', type=float, default=0.1, + help='interval (in seconds) to check for message updates') + parser.add_argument('--db_url', '-d', dest='db_url', default='sqlite:///%s.sqlite3', + help='URL for database (default is sqlite:///%%s.sqlite3)') + parser.add_argument('--list', '-l', dest='list', action='store_const', const=True, default=False, + help='list commands') + parser.add_argument('--webhook', '-w', dest='webhook', nargs=2, metavar=('hook_url', 'port'), + help='use webhooks (instead of polling) - requires bottle') + parser.add_argument('--create_db', dest='create_db', action='store_const', + const=True, default=False, + help='setup database') + parser.add_argument('--token1', dest='token1', + help='token for first bot (provided by @BotFather)') + parser.add_argument('--token2', dest='token2', + help='token for second bot (provided by @BotFather)') + + return parser + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt index 99af50d..bbd6dc4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ --e git+https://github.com/fopina/tgbotplug@v1.2.5#egg=tgbotplug +#-e git+https://github.com/fopina/tgbotplug@v1.2.5#egg=tgbotplug +tgbotplug==1.2.5