Skip to content

Commit

Permalink
Merge pull request #4831 from askovpen/dev
Browse files Browse the repository at this point in the history
add telegram event handler
  • Loading branch information
solderzzc authored Aug 27, 2016
2 parents 566a3f7 + 3a2dfee commit 81384b3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
4 changes: 3 additions & 1 deletion configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
{
"type": "TelegramTask",
"config": {
"enabled": false
"enabled": false,
"master": null,
"alert_catch": ["all"]
}
},
{
Expand Down
13 changes: 10 additions & 3 deletions pokemongo_bot/cell_workers/telegram_task.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
import telegram
import os
import logging
import json
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.base_dir import _base_dir

from pokemongo_bot.event_handlers import TelegramHandler
class TelegramTask(BaseTask):
SUPPORTED_TASK_API_VERSION = 1
update_id = None
Expand All @@ -13,6 +14,7 @@ class TelegramTask(BaseTask):
def initialize(self):
if not self.enabled:
return
self.logger = logging.getLogger(type(self).__name__)
api_key = self.bot.config.telegram_token
if api_key == None:
self.emit_event(
Expand All @@ -21,6 +23,8 @@ def initialize(self):
)
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')))
try:
self.update_id = self.tbot.getUpdates()[0].update_id
except IndexError:
Expand All @@ -32,6 +36,9 @@ def work(self):
for update in self.tbot.getUpdates(offset=self.update_id, timeout=10):
self.update_id = update.update_id+1
if update.message:
self.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)<>update.message.from_user.id:
continue
if update.message.text == "/info":
stats = self._get_player_stats()
if stats:
Expand All @@ -45,8 +52,8 @@ def work(self):
"*"+self.bot.config.username+"*",
"_Level:_ "+str(stats["level"]),
"_XP:_ "+str(stats["experience"])+"/"+str(stats["next_level_xp"]),
"_Pokemons Captured:_ "+str(stats["pokemons_captured"])+" ("+str(catch_day)+" _today_)",
"_Poke Stop Visits:_ "+str(stats["poke_stop_visits"])+" ("+str(ps_day)+" _today_)",
"_Pokemons Captured:_ "+str(stats["pokemons_captured"])+" ("+str(catch_day)+" _last 24h_)",
"_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))
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/event_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from socketio_handler import SocketIoHandler
from colored_logging_handler import ColoredLoggingHandler
from social_handler import SocialHandler
from telegram_handler import TelegramHandler
23 changes: 23 additions & 0 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from pokemongo_bot.event_manager import EventHandler
import thread

DEBUG_ON = False

class TelegramHandler(EventHandler):
def __init__(self, tbot,master,pokemons):
self.tbot = tbot
self.master=master
self.pokemons=pokemons

def handle_event(self, event, sender, level, formatted_msg, data):
if self.master:
if event == 'level_up':
self.tbot.sendMessage(chat_id=self.master, parse_mode='Markdown', text="level up ({})".format(data["current_level"]))
elif event == 'pokemon_caught':
if data["pokemon"] in self.pokemons or self.pokemons[0]=="all":
self.tbot.sendMessage(chat_id=self.master, parse_mode='Markdown',
text="Caught {} CP: {}, IV: {}".format(data["pokemon"],data["cp"],data["iv"])
)


0 comments on commit 81384b3

Please sign in to comment.