Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handling non-numeric "master" in telegram config #5074

Merged
merged 3 commits into from
Sep 2, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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":
Expand All @@ -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:
Expand Down