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

Improve telegram startup, fix master/sub issues #5537

Merged
merged 5 commits into from
Sep 19, 2016
Merged
Changes from 1 commit
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
24 changes: 15 additions & 9 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ def authenticate(self, update):
self.chat_handler.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="Authentication successful, you can now use all commands")
return

def run(self):
def run(self):
time.sleep(1)
while True:
if self._tbot is None:
self.connect()

for update in self._tbot.getUpdates(offset=self.update_id, timeout=10):
self.update_id = update.update_id+1
if update.message:
Expand Down Expand Up @@ -288,6 +291,7 @@ def __init__(self, bot, config):
self.whoami = "TelegramHandler"
self.config = config
self.chat_handler = ChatHandler(self.bot, self.pokemons)

if master == None:
self.master = None
return
Expand All @@ -309,6 +313,13 @@ def __init__(self, bot, config):
else: # uid not known yet
self.bot.logger.info("Telegram master UID not in datastore yet")
self.master = master

self._connect()

def _connect(self):
self.bot.logger.info("Telegram bot not running. Starting")
self.tbot = TelegramClass(self.bot, self.master, self.pokemons, self.config)
thread.start_new_thread(self.tbot.run)

def catch_notify(self, pokemon, cp, iv, params):
if params == " ":
Expand All @@ -324,15 +335,10 @@ def catch_notify(self, pokemon, cp, iv, params):

def handle_event(self, event, sender, level, formatted_msg, data):
if self.tbot is None:
if DEBUG_ON:
self.bot.logger.info("handle_event Telegram bot not running.")
try:
if hasattr(self, "master"):
selfmaster = self.master
else:
selfmaster = None
self.bot.logger.info("Telegram bot not running. Starting")
self.tbot = TelegramClass(self.bot, selfmaster, self.pokemons, self.config)
self.tbot.connect()
thread.start_new_thread(self.tbot.run)
self._connect()
except Exception as inst:
self.tbot = None
self.bot.logger.error("Unable to start Telegram bot; master: {}, exception: {}".format(selfmaster, pprint.pformat(inst)))
Expand Down