Skip to content

Commit

Permalink
Dynamic subscriptions and password-based authentication with Telegram (
Browse files Browse the repository at this point in the history
…#5238)

* Implemented username to UID conversion persistence

* removed debug code

* fixed bugs

* New dynamic TelegramTask notifications

* Update telegramtask.md

* Update configuration_files.md

* some more error handling
  • Loading branch information
DBa2016 authored and solderzzc committed Sep 6, 2016
1 parent abb2f73 commit 1e62ec4
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 28 deletions.
8 changes: 5 additions & 3 deletions docs/configuration_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,9 @@ Bot answer on command '/info' self stats.
### Options

* `telegram_token` : bot token (getting [there](https://core.telegram.org/bots#6-botfather) - one token per bot)
* `master` : id (without quotes) of bot owner, who will get alerts and may issue commands.
* `master` : id (without quotes) of bot owner, who will get alerts and may issue commands or a (case-sensitive!) user name.
* `alert_catch` : dict of rules pokemons catch.
* `password` : a password to be used to authenticate to the bot

The bot will only alert and respond to a valid master. If you're unsure what this is, send the bot a message from Telegram and watch the log to find out.

Expand All @@ -1016,7 +1017,8 @@ The bot will only alert and respond to a valid master. If you're unsure what thi
"alert_catch": {
"all": {"operator": "and", "cp": 1300, "iv": 0.95},
"Snorlax": {"operator": "or", "cp": 900, "iv": 0.9}
}
},
"password": "alwoefhq348"
}
}
```
Expand Down Expand Up @@ -1062,4 +1064,4 @@ Available `team` :
"team": 2
}
}
```
```
45 changes: 45 additions & 0 deletions docs/telegramtask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
TelegramTask configuration and subscriptions for updates


**Authentication**

There are two ways to be authenticated with the Telegram task of the bot:
* authentication for one predefined userid or username (config parameter: `master`, can be given as a number (userid) or as a string(username); for username, please note that it is case-sensitive); this will automatically authenticate all requests coming from the particular userid/username (note: when providing username, you will need to send a message to the bot in order for the bot to learn your user id). Hardcoded notifications will only be sent to this username/userid.
* authentication by a password (config parameter: `password`): this will wait for a `/login <password>` command to be sent to the bot and will from then on treat the corresponding userid as authenticated until a `/logout` command is sent from that user id.

**Hardcoded notifications - please consider this feature deprecated, it will be removed in the future**

Certain notifications (egg_hatched, bot_sleep, spin_limit, catch_limit, level_up) will always be sent to the "master" and cannot be further configured.
The notification for pokemon_caught can be configured by using the "alert_catch" configuration parameter. This parameter can be:
* either a plain list (in this case, a notification will be sent for every pokemon on the list caught, with "all" matching all pokemons)
* or a "key: value" dict of the following form

> "pokemon_name": {"operator": "and/or", "cp": cpmin, "iv": ivmin }
Again, "all" will apply to all pokemons. If a matching pokemon is caught, depending on "operator" being "and" or "or", a notification will be sent if both or one of the criteria is met.
Example:
> "Dratini": { "operator": "and", "cp": 1200, "iv": 0.99 }
This will send a notification if a Dratini is caught with at least 1200CP and at least 0.99 potential.

**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.

In order to subscribe to a certain event, e.g. to "no_pokeballs", you simply send the `/sub` command as follows:
> /sub no_pokeballs
In order to remove this subscription:
> /unsub no_pokeballs
*Note: the `/unsub` command must match exactly the corresponding `/sub` command*
A special case is `/sub all` - it will subscribe you to all events. Be prepared for huge amount of events! `/unsub all` will remove this subscription, without changing other subscriptions.
Another special case is `/unsub everything` - this will remove all your subscriptions.
Currently, only pokemon_caught event can be configured with more detail, here is an example:
> /sub pokemon_caught operator:and cp:1200 pokemon:Dratini iv:0.99
This will subscribe you to be notified every time a Dratini has been caught with cp equal or higher than 1200 and iv equal or higher than 0.99 (same as in "Hardcoded notifications" above)

`/showsubs` will show your current subscriptions.


Loading

1 comment on commit 1e62ec4

@davidakachaos
Copy link
Contributor

@davidakachaos davidakachaos commented on 1e62ec4 Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DBa2016 I get an error with the latest dev, and I think it has to do with this commit.

Traceback (most recent call last):
  File "pokecli.py", line 803, in <module>
    main()
  File "pokecli.py", line 150, in main
    bot = start_bot(bot, config)
  File "pokecli.py", line 108, in start_bot
    initialize_task(bot, config)
  File "pokecli.py", line 92, in initialize_task
    tree = TreeConfigBuilder(bot, config.raw_tasks).build()
  File "/home/david/Broncode/PokemonGo-Bot/pokemongo_bot/tree_config_builder.py", line 79, in build
    instance = worker(self.bot, task_config)
  File "/home/david/Broncode/PokemonGo-Bot/pokemongo_bot/base_task.py", line 23, in __init__
    self.initialize()
  File "/home/david/Broncode/PokemonGo-Bot/pokemongo_bot/cell_workers/telegram_task.py", line 21, in initialize
    self.bot.event_manager.add_handler(TelegramHandler(self.bot, self.config))
  File "/home/david/Broncode/PokemonGo-Bot/pokemongo_bot/event_handlers/telegram_handler.py", line 292, in __init__
    initiator = TelegramDBInit(bot.database)
  File "/home/david/Broncode/PokemonGo-Bot/pokemongo_bot/event_handlers/telegram_handler.py", line 264, in __init__
    self.initDBstructure()
  File "/home/david/Broncode/PokemonGo-Bot/pokemongo_bot/event_handlers/telegram_handler.py", line 276, in initDBstructure
    self.initDBobject(objname, db_structure[objname])
  File "/home/david/Broncode/PokemonGo-Bot/pokemongo_bot/event_handlers/telegram_handler.py", line 282, in initDBobject
    if len(res) > 0 and res[0] != sql: # object exists and sql not matching
TypeError: object of type 'NoneType' has no len()

Please sign in to comment.