Skip to content

Commit

Permalink
multibot process example
Browse files Browse the repository at this point in the history
  • Loading branch information
fopina committed Jan 14, 2016
1 parent e6e2175 commit 69501be
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
81 changes: 81 additions & 0 deletions multibot.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 69501be

Please sign in to comment.