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

Telegram fix to allow "@username" as master #4860

Merged
merged 1 commit into from
Aug 28, 2016
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def _register_events(self):
self.event_manager.register_event('set_start_location')
self.event_manager.register_event('load_cached_location')
self.event_manager.register_event('location_cache_ignored')

self.event_manager.register_event('debug')

# ignore candy above threshold
self.event_manager.register_event(
Expand Down Expand Up @@ -592,6 +594,12 @@ def _register_events(self):
'moving_to_pokemon_throught_fort',
parameters=('fort_name', 'distance','poke_name','poke_dist')
)
self.event_manager.register_event(
'move_to_map_pokemon',
parameters=('message')
)



# cached recent_forts
self.event_manager.register_event('loaded_cached_forts')
Expand Down
25 changes: 19 additions & 6 deletions pokemongo_bot/cell_workers/move_to_map_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Moves a trainer to a Pokemon.

Events:
move_to_map_pokemon
When a generic message is logged
Returns:
message: Log message.

move_to_map_pokemon_fail
When the worker fails.
Returns:
Expand Down Expand Up @@ -83,7 +88,7 @@
SNIPE_MAX_IN_CHAIN = 2

# Don't call sniper every time in workers
SNIPE_SKIP_IN_ROUND = 30
SNIPE_SKIP_IN_ROUND = 5

DEBUG_ON = False

Expand Down Expand Up @@ -130,7 +135,13 @@ def get_pokemon_from_social(self):
pokemon['is_vip'] = pokemon['name'] in self.bot.config.vips

if pokemon['name'] not in self.config['catch']:
if DEBUG_ON:
self._emit_failure("Not catching {}".format(pokemon['name']))
continue
else:
if DEBUG_ON:
self._emit_log("Catching {}".format(pokemon['name']))


if self.was_caught(pokemon):
continue
Expand All @@ -144,7 +155,7 @@ def get_pokemon_from_social(self):
pokemon['longitude'],
)

if pokemon['dist'] > self.config['max_distance'] or not self.config['snipe']:
if pokemon['dist'] > self.config['max_distance'] and not self.config['snipe']:
continue

# pokemon not reachable with mean walking speed (by config)
Expand Down Expand Up @@ -300,14 +311,16 @@ def work(self):

if (pokeballs_quantity + superballs_quantity + ultraballs_quantity) < self.min_ball:
if DEBUG_ON:
print 'no enough balls'
self._emit_log("Not enough balls to start sniping (have {}, {} needed)".format(pokeballs_quantity + superballs_quantity + ultraballs_quantity, self.min_ball))
return WorkerResult.SUCCESS

self.dump_caught_pokemon()
if self.bot.config.enable_social:
if self.config['snipe']:
self.by_pass_times = self.by_pass_times + 1
if self.by_pass_times < SNIPE_SKIP_IN_ROUND:
if DEBUG_ON:
self._emit_log("Skipping pass {}".format(self.by_pass_times))
return WorkerResult.SUCCESS
self.by_pass_times = 0
pokemon_list = self.get_pokemon_from_social()
Expand All @@ -323,12 +336,12 @@ def work(self):

if len(pokemon_list) < 1:
if DEBUG_ON:
print 'No enough pokemon in list to snip'
self._emit_log("No pokemons in list to snipe")
return WorkerResult.SUCCESS

pokemon = pokemon_list[0]
if DEBUG_ON:
print 'How many pokemon in list: {}'.format(len(pokemon_list))
self._emit_log('How many pokemon in list: {}'.format(len(pokemon_list)))
if self.config['snipe']:
if self.snipe_high_prio_only:
count = 0
Expand All @@ -342,7 +355,7 @@ def work(self):
time.sleep(SNIPE_SLEEP_SEC*5)
else:
if DEBUG_ON:
print 'this pokemon is not good enough to snip {}'.format(pokemon)
self._emit_log('this pokemon is not good enough to snipe {}'.format(pokemon))
return WorkerResult.SUCCESS
else:
return self.snipe(pokemon)
Expand Down
17 changes: 16 additions & 1 deletion pokemongo_bot/cell_workers/telegram_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
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 TelegramTask(BaseTask):
SUPPORTED_TASK_API_VERSION = 1
update_id = None
Expand Down Expand Up @@ -37,8 +41,19 @@ def work(self):
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:
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))
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'))



if update.message.text == "/info":
stats = self._get_player_stats()
if stats:
Expand Down
17 changes: 13 additions & 4 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from pokemongo_bot.event_manager import EventHandler
import thread
import re

DEBUG_ON = False

Expand All @@ -9,15 +10,23 @@ def __init__(self, tbot,master,pokemons):
self.tbot = tbot
self.master=master
self.pokemons=pokemons
self.whoami="TelegramHandler"

def handle_event(self, event, sender, level, formatted_msg, data):
if self.master:
if not re.match(r'^[0-9]+$', str(self.master)):
return
master = self.master

if event == 'level_up':
self.tbot.sendMessage(chat_id=self.master, parse_mode='Markdown', text="level up ({})".format(data["current_level"]))
msg = "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"])
)
msg = "Caught {} CP: {}, IV: {}".format(data["pokemon"],data["cp"],data["iv"])
else:
return
else:
return
self.tbot.sendMessage(chat_id=master, parse_mode='Markdown', text=msg)