From 334df96730d40ef62a1cac86fd9fe06e4770a879 Mon Sep 17 00:00:00 2001 From: DBa2016 Date: Fri, 2 Sep 2016 02:04:49 +0200 Subject: [PATCH] implemented handling of symbolic "master" in telegram config (again) --- pokemongo_bot/event_handlers/telegram_handler.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pokemongo_bot/event_handlers/telegram_handler.py b/pokemongo_bot/event_handlers/telegram_handler.py index 165521c062..3776b4a079 100755 --- a/pokemongo_bot/event_handlers/telegram_handler.py +++ b/pokemongo_bot/event_handlers/telegram_handler.py @@ -17,11 +17,12 @@ class TelegramClass: update_id = None - def __init__(self, bot, master, pokemons): + def __init__(self, bot, master, pokemons, config): self.bot = bot self.master = master self.pokemons = pokemons self._tbot = None + self.config = config def sendMessage(self, chat_id=None, parse_mode='Markdown', text=None): self._tbot.sendMessage(chat_id=chat_id, parse_mode=parse_mode, text=text) @@ -78,6 +79,16 @@ def run(self): self.bot.logger.info("message from {} ({}): {}".format(update.message.from_user.username, update.message.from_user.id, update.message.text)) if self.master and self.master not in [update.message.from_user.id, "@{}".format(update.message.from_user.username)]: continue + if not re.match(r'^[0-9]+$', self.master): + # the "master" is not numeric, set self.master to update.message.chat_id and re-instantiate the handler + newconfig = self.config + newconfig['master'] = update.message.chat_id + # remove old handler + self.bot.event_manager._handlers = filter(lambda x: not isinstance(x, TelegramHandler), self.bot.event_manager._handlers) + # add new handler (passing newconfig as parameter) + self.bot.event_manager.add_handler(TelegramHandler(self.bot, newconfig)) + + if update.message.text == "/info": self.send_player_stats_to_chat(update.message.chat_id) elif update.message.text == "/start" or update.message.text == "/help": @@ -98,11 +109,12 @@ def __init__(self, bot, config): self.master = config.get('master', None) self.pokemons = config.get('alert_catch', {}) self.whoami = "TelegramHandler" + self.config = config def handle_event(self, event, sender, level, formatted_msg, data): if self.tbot is None: try: - self.tbot = TelegramClass(self.bot, self.master, self.pokemons) + self.tbot = TelegramClass(self.bot, self.master, self.pokemons, self.config) self.tbot.connect() thread.start_new_thread(self.tbot.run) except Exception as inst: