def test_save_load(tmpdir): """Verify MultiBot can start with no config, can have a bot added, and can restart.""" conffile = tmpdir.join('bots.yaml') mybot = multibot.MultiBot((), confdir=tmpdir.strpath) mockbot = ntelebot.bot.Bot('1234:goodbot') mockbot.getme.respond(json={ 'ok': True, 'result': { 'id': 1234, 'username': '******' } }) mockbot.getupdates.respond(json={'ok': True, 'result': []}) mybot.add_bot('1234:goodbot') mybot.run_bot('goodbot') assert yaml.safe_load(conffile.read()) == { 'goodbot': { 'issue37': { 'telegram': { 'running': True, 'token': '1234:goodbot', }, }, }, } tmpdir.join('calendars.json').write( json.dumps([ { 'calid': 'static:test_multibot', 'name': 'MultiBot Test Calendar', }, ])) newbot = multibot.MultiBot((), confdir=tmpdir.strpath) assert newbot.conf == mybot.conf assert newbot.calendars == { 'c9328778': { 'calid': 'static:test_multibot', 'name': 'MultiBot Test Calendar', }, } mybot.stop_bot('goodbot') newbot.stop_bot('goodbot')
def test_add_bot(): """Verify the basic behavior of add_bot.""" mybot = multibot.MultiBot(()) mockbot = ntelebot.bot.Bot('1234:badbot') mockbot.getme.respond(json={ 'description': 'Unauthorized', 'error_code': 401, 'ok': False }) with pytest.raises(ntelebot.errors.Unauthorized): mybot.add_bot('1234:badbot')
def main(): # pylint: disable=missing-docstring logging.basicConfig( format='%(asctime)s %(levelname)s %(filename)s:%(lineno)s] %(message)s', level=logging.INFO) mybot = multibot.MultiBot(modutil.load_modules('metabot.modules'), confdir='config') if not mybot.conf['bots']: print() print( "Hi! Before I can start, I need at least one bot's Telegram token. If you don't have " 'one already, follow the instructions at:') print() print(' https://core.telegram.org/bots#creating-a-new-bot') print() print( 'At the end, BotFather will send you a message starting with "Use this token" ' 'followed by a line starting with a number. Copy the full line starting with the ' 'number and paste it here:') print() while not mybot.conf['bots']: initial_token = input('Telegram token: ') try: username = mybot.add_bot(initial_token) except AssertionError: print() print( "That doesn't look quite right. Look for a message from BotFather starting " 'with "Use this token" followed by a line starting with a number. Copy the ' 'full line starting with the number and paste it here:') print() except Exception as exc: # pylint: disable=broad-except print() print('Woops, that generated: %r', exc) else: mybot.run_bot(username) unconfigured = sorted(username for username, botconf in mybot.conf['bots'].items() if not botconf['issue37']['admin'].get('admins')) if unconfigured: print() print('To configure %s, open a chat with:' % humanize.list(unconfigured)) print() for username in unconfigured: print(' https://t.me/%s' % username) print() print('and type:') print() print(' /_bootstrap %s' % admin.BOOTSTRAP_TOKEN) print() mybot.run()
def __init__(self, *modules): def dummymod(ctx): # pylint: disable=missing-docstring,unused-argument return ctx.command == 'dummymod' and ctx.reply_text('DUMMYMOD') self.multibot = multibot.MultiBot( set(modules) | {admin, dummymod, help, settings}) ntelebot.bot.Bot('1234:test').getme.respond(json={ 'ok': True, 'result': { 'username': '******' }, }) username = self.multibot.add_bot('1234:test') self.bot = self.multibot._build_bot(username) # pylint: disable=protected-access self.bot.config['issue37']['admin']['admins'] = [1000]
def test_module(): """Verify the configurable module dispatcher.""" results = [] event = threading.Event() class _DummyMod: # pylint: disable=too-few-public-methods @staticmethod def moddispatch(ctx, unused_msg, unused_modconf): # pylint: disable=missing-docstring results.append(ctx.text) mybot.stop() event.set() mybot = multibot.MultiBot({_DummyMod}) mockbot = ntelebot.bot.Bot('1234:modbot') mockbot.getme.respond(json={ 'ok': True, 'result': { 'id': 1234, 'username': '******' } }) user = {'id': 1000} chat = {'id': 1000, 'type': 'private'} message = { 'message_id': 2000, 'chat': chat, 'from': user, 'text': '/dummymod test' } update = {'message': message, 'update_id': 3000} mockbot.getupdates.respond(json={'ok': True, 'result': [update]}) mybot.add_bot('1234:modbot') mybot.run_bot('modbot') mybot.run() event.wait() assert results == ['test']