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

Catch exception telegram.error.NetworkError. #4940

Merged
merged 2 commits into from
Aug 30, 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
72 changes: 38 additions & 34 deletions pokemongo_bot/cell_workers/telegram_task.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,64 @@
# -*- coding: utf-8 -*-

from datetime import datetime
from datetime import timedelta
import telegram
import os
import logging
import json
import telegram
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.base_dir import _base_dir
from pokemongo_bot.event_handlers import TelegramHandler

from pprint import pprint
import re

class FileIOException(Exception):
pass

class TelegramTask(BaseTask):
SUPPORTED_TASK_API_VERSION = 1
update_id = None
tbot = None
min_interval=None
next_job=None
min_interval = None
next_job = None

def initialize(self):
if not self.enabled:
return
api_key = self.bot.config.telegram_token
if api_key == None:
if api_key is None:
self.emit_event(
'config_error',
formatted='api_key not defined.'
)
return
self.tbot = telegram.Bot(api_key)
if self.config.get('master',None):
self.bot.event_manager.add_handler(TelegramHandler(self.tbot,self.config.get('master',None),self.config.get('alert_catch')))
self.master = self.config.get('master', None)
if self.master:
self.bot.event_manager.add_handler(TelegramHandler(self.tbot, self.master, self.config.get('alert_catch')))
try:
self.update_id = self.tbot.getUpdates()[0].update_id
except IndexError:
self.update_id = None
self.min_interval=self.config.get('min_interval',120)
self.next_job=datetime.now() + timedelta(seconds=self.min_interval)
self.min_interval = self.config.get('min_interval', 120)
self.next_job = datetime.now() + timedelta(seconds=self.min_interval)
def work(self):
if not self.enabled:
return
if datetime.now()<self.next_job:
if datetime.now() < self.next_job:
return
self.next_job=datetime.now() + timedelta(seconds=self.min_interval)
self.next_job = datetime.now() + timedelta(seconds=self.min_interval)
for update in self.tbot.getUpdates(offset=self.update_id, timeout=10):
self.update_id = update.update_id+1
if update.message:
self.bot.logger.info("message from {} ({}): {}".format(update.message.from_user.username, update.message.from_user.id, update.message.text))
if self.config.get('master',None) and self.config.get('master',None) not in [update.message.from_user.id, "@{}".format(update.message.from_user.username)]:
self.emit_event(
'debug',
formatted="Master wrong: expecting {}, got {}({})".format(self.config.get('master',None), update.message.from_user.username, update.message.from_user.id))
if self.config.get('master', None) and self.config.get('master', None) not in [update.message.from_user.id, "@{}".format(update.message.from_user.username)]:
self.emit_event(
'debug',
formatted="Master wrong: expecting {}, got {}({})".format(self.master, update.message.from_user.username, update.message.from_user.id))
continue
else:
if not re.match(r'^[0-9]+$', "{}".format(self.config['master'])): # master was not numeric...
self.config['master'] = update.message.chat_id
idx = (i for i,v in enumerate(self.bot.event_manager._handlers) if type(v) is TelegramHandler).next()
self.bot.event_manager._handlers[idx] = TelegramHandler(self.tbot,self.config['master'], self.config.get('alert_catch'))



self.bot.event_manager._handlers[idx] = TelegramHandler(self.tbot, self.master, self.config.get('alert_catch'))
if update.message.text == "/info":
stats = self._get_player_stats()
if stats:
Expand All @@ -81,36 +76,45 @@ def work(self):
"_Poke Stop Visits:_ "+str(stats["poke_stop_visits"])+" ("+str(ps_day)+" _last 24h_)",
"_KM Walked:_ "+str(stats["km_walked"])
)
self.tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))
self.tbot.send_location(chat_id=update.message.chat_id, latitude=self.bot.api._position_lat, longitude=self.bot.api._position_lng)
self.send_message(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))
self.send_location(chat_id=update.message.chat_id, latitude=self.bot.api._position_lat, longitude=self.bot.api._position_lng)
else:
self.tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="Stats not loaded yet\n")
self.send_message(chat_id=update.message.chat_id, parse_mode='Markdown', text="Stats not loaded yet\n")
elif update.message.text == "/start" or update.message.text == "/help":
res = (
"Commands: ",
"/info - info about bot"
)
self.tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))

self.send_message(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))
def send_message(self, chat_id, parse_mode, text):
try:
self.tbot.sendMessage(chat_id, parse_mode, text)
except telegram.error.NetworkError:
pass
def send_location(self, chat_id, latitude, longitude):
try:
self.tbot.send_location(chat_id, latitude, longitude)
except telegram.error.NetworkError:
pass
def _get_player_stats(self):
"""
Helper method parsing the bot inventory object and returning the player stats object.
:return: The player stats object.
:rtype: dict
"""
web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)

try:
with open(web_inventory, "r") as infile:
json_inventory = json.load(infile)
except ValueError as e:
except ValueError as exception:
# Unable to read json from web inventory
# File may be corrupt. Create a new one.
self.bot.logger.info('[x] Error while opening inventory file for read: %s' % e)
# File may be corrupt. Create a new one.
self.bot.logger.info('[x] Error while opening inventory file for read: %s' % exception)
json_inventory = []
except:
raise FileIOException("Unexpected error reading from {}".web_inventory)
raise FileIOException("Unexpected error reading from {}".format(web_inventory))

return next((x["inventory_item_data"]["player_stats"]
for x in json_inventory
if x.get("inventory_item_data", {}).get("player_stats", {})),
Expand Down