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

added /top command to Telegram interface #5274

Merged
merged 6 commits into from
Sep 8, 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
18 changes: 17 additions & 1 deletion docs/telegramtask.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ This will send a notification if a Dratini is caught with at least 1200CP and at

**Dynamic notifications(subscriptions)**

Every authenticated user can subscribe to be notified in case a certain event is emitted. The list of currently available events can be retrieved by sending "/events" command.
Every authenticated user can subscribe to be notified in case a certain event is emitted. The list of currently available events can be retrieved by sending `/events` command:
> /events

List all available eventy (MANY!)

> /events egg

List all events matching regular expression .\*egg.\*

In order to subscribe to a certain event, e.g. to "no_pokeballs", you simply send the `/sub` command as follows:
> /sub no_pokeballs
Expand All @@ -43,3 +50,12 @@ This will subscribe you to be notified every time a Dratini has been caught with
`/showsubs` will show your current subscriptions.


**Listing pokemon**

> /top 10 iv

List top 10 pokemon, ordered by IV

> /top 15 cp

List top 15 pokemon, ordered by CP
52 changes: 48 additions & 4 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
from pokemongo_bot.datastore import Datastore
import pprint
from pokemongo_bot import inventory


DEBUG_ON = False
Expand Down Expand Up @@ -162,6 +163,43 @@ def authenticate(self, update):
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="Authentication successful, you can now use all commands")
return

def display_events(self, update):
cmd = update.message.text.split(" ", 1)
if len(cmd) > 1:
# we have a filter
event_filter = ".*{}-*".format(cmd[1])
else:
# no filter
event_filter = ".*"
events = filter(lambda k: re.match(event_filter, k), self.bot.event_manager._registered_events.keys())
self.sendMessage(chat_id=update.message.chat_id, parse_mode='HTML', text=("\n".join(events)))

def showtop(self, chatid, num, order):
if not num.isnumeric():
num = 10
else:
num = int(num)

if order not in ["cp", "iv"]:
order = "iv"

pkmns = sorted(inventory.pokemons().all(), key=lambda p: getattr(p, order), reverse=True)[:num]

outMsg = "\n".join(["{} CP:{} IV:{} ID:{} Candy:{}".format(p.name, p.cp, p.iv, p.unique_id, inventory.candies().get(p.pokemon_id).quantity) for p in pkmns])
self.sendMessage(chat_id=chatid, parse_mode='HTML', text=outMsg)

return

def evolve(self, chatid, uid):
# TODO: here comes evolve logic (later)
self.sendMessage(chat_id=chatid, parse_mode='HTML', text="Evolve logic not implemented yet")
return

def upgrade(self, chatid, uid):
# TODO: here comes upgrade logic (later)
self.sendMessage(chat_id=chatid, parse_mode='HTML', text="Upgrade logic not implemented yet")
return

def run(self):
time.sleep(1)
while True:
Expand All @@ -175,11 +213,12 @@ def run(self):
"/info - info about bot",
"/login <password> - authenticate with the bot; once authenticated, your ID will be registered with the bot and survive bot restarts",
"/logout - remove your ID from the 'authenticated' list",
"/sub <event_name> [<parameters>] - subscribe to event_name, with optional parameters, event name=all will subscribe to ALL events (LOTS of output!)",
"/sub <event\_name> [<parameters>] - subscribe to event_name, with optional parameters, event name=all will subscribe to ALL events (LOTS of output!)",
"/unsub <event_name> [<parameters>] - unsubscribe from event_name; parameters must match the /sub parameters",
"/unsub everything - will remove all subscriptions for this uid",
"/showsubs - show current subscriptions",
"/events - show available events"
"/events <filter> - show available events, filtered by regular expression <filter>",
"/top <num> <cp-or-iv> - show top X pokemons, sorted by CP or IV"
)
self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))
continue
Expand Down Expand Up @@ -213,8 +252,8 @@ def run(self):
if update.message.text == "/info":
self.send_player_stats_to_chat(update.message.chat_id)
continue
if update.message.text == "/events":
self.sendMessage(chat_id=update.message.chat_id, parse_mode='HTML', text=(", ".join(self.bot.event_manager._registered_events.keys())))
if re.match("^/events", update.message.text):
self.display_events(update)
continue
if update.message.text == "/logout":
self.sendMessage(chat_id=update.message.chat_id, parse_mode='HTML', text=("Logged out."))
Expand All @@ -231,6 +270,10 @@ def run(self):
if re.match(r'^/showsubs', update.message.text):
self.showsubs(update.message.chat_id)
continue
if re.match(r'^/top ', update.message.text):
(cmd, num, order) = self.tokenize(update.message.text, 3)
self.showtop(update.message.chat_id, num, order)
continue

self.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="Unrecognized command: {}".format(update.message.text))
def showsubs(self, chatid):
Expand Down Expand Up @@ -365,6 +408,7 @@ def handle_event(self, event, sender, level, formatted_msg, data):
else:
msg = formatted_msg
except KeyError:
msg = "Error on event {}".format(event)
pass
# first handle subscriptions; they are independent of master setting.
with self.bot.database as conn:
Expand Down