# -*- coding: utf-8 -*-
import datetime
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

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
    
    def initialize(self):
        if not self.enabled:
            return
        api_key = self.bot.config.telegram_token
        if api_key == 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')))
        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)
    def work(self):
        if not self.enabled:
            return
        if datetime.now()<self.next_job:
            return
        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))
                    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:
                        with self.bot.database as conn:
                            cur = conn.cursor()
                            cur.execute("SELECT DISTINCT COUNT(encounter_id) FROM catch_log WHERE dated >= datetime('now','-1 day')")
                            catch_day = cur.fetchone()[0]
                            cur.execute("SELECT DISTINCT COUNT(pokestop) FROM pokestop_log WHERE dated >= datetime('now','-1 day')")
                            ps_day = cur.fetchone()[0]
                            res = (
                                "*"+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)+" _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))
                            self.tbot.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")
                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))

    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:
            # 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)
            json_inventory = []
        except:
            raise FileIOException("Unexpected error reading from {}".web_inventory)
            
        return next((x["inventory_item_data"]["player_stats"]
                     for x in json_inventory
                     if x.get("inventory_item_data", {}).get("player_stats", {})),
                    None)

pokemongo_bot/cell_workers/telegram_task.py
# -*- coding: utf-8 -*-
from datetime import datetime
from datetime import timedelta
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

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
    
    def initialize(self):
        if not self.enabled:
            return
        api_key = self.bot.config.telegram_token
        if api_key == 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')))
        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)
    def work(self):
        if not self.enabled:
            return
        if datetime.now()<self.next_job:
            return
        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))
                    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:
                        with self.bot.database as conn:
                            cur = conn.cursor()
                            cur.execute("SELECT DISTINCT COUNT(encounter_id) FROM catch_log WHERE dated >= datetime('now','-1 day')")
                            catch_day = cur.fetchone()[0]
                            cur.execute("SELECT DISTINCT COUNT(pokestop) FROM pokestop_log WHERE dated >= datetime('now','-1 day')")
                            ps_day = cur.fetchone()[0]
                            res = (
                                "*"+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)+" _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))
                            self.tbot.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")
                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))

    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:
            # 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)
            json_inventory = []
        except:
            raise FileIOException("Unexpected error reading from {}".web_inventory)
            
        return next((x["inventory_item_data"]["player_stats"]
                     for x in json_inventory
                     if x.get("inventory_item_data", {}).get("player_stats", {})),
                    None)

pokemongo_bot/cell_workers/telegram_task.py
diff --git a/pokemongo_bot/cell_workers/telegram_task.py b/pokemongo_bot/cell_workers/telegram_task.py
index c5bdd2ae58..c27b9eb1fe 100644
--- a/pokemongo_bot/cell_workers/telegram_task.py
+++ b/pokemongo_bot/cell_workers/telegram_task.py
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
-import datetime
+from datetime import datetime
+from datetime import timedelta
 import telegram
 import os
 import logging

[BUG] Error in Telegram: "AttributeError: 'module' object has no attribute 'now'"
### Expected Behavior

<!-- Tell us what you expect to happen -->

Bot running with Telegram enabled
### Actual Behavior

<!-- Tell us what is happening -->

Bot not starting due to error message
### Your FULL config.json (remove your username, password, gmapkey and any other private info)

<!-- Provide your FULL config file, feel free to use services such as pastebin.com to reduce clutter -->

http://pastebin.com/5nQC2ceh
### Output when issue occurred

<!-- Provide a reasonable sample from your output log (not just the error message), feel free to use services such as pastebin.com to reduce clutter -->

Traceback (most recent call last):
  File "pokecli.py", line 781, in <module>
    main()
  File "pokecli.py", line 128, in main
    bot = start_bot(bot, config)
  File "pokecli.py", line 88, in start_bot
    initialize_task(bot, config)
  File "pokecli.py", line 79, in initialize_task
    tree = TreeConfigBuilder(bot, config.raw_tasks).build()
  File "/PokemonGo-Bot/pokemongo_bot/tree_config_builder.py", line 79, in build
    instance = worker(self.bot, task_config)
  File "/PokemonGo-Bot/pokemongo_bot/base_task.py", line 23, in **init**
    self.initialize()
  File "/PokemonGo-Bot/pokemongo_bot/cell_workers/telegram_task.py", line 42, in initialize
    self.next_job=datetime.now() + timedelta(seconds=self.min_interval)
AttributeError: 'module' object has no attribute 'now'
### Steps to Reproduce

<!-- Tell us the steps you have taken to reproduce the issue -->

Start the bot with the above config.
### Other Information

OS: CentOS

<!-- Tell us what Operating system you're using --> 

Branch: dev

<!-- dev or master --> 

Git Commit: 9e81c6ed90d79e181599ec7f0a0cfa2ecd4d09f5

<!-- run 'git log -n 1 --pretty=format:"%H"' --> 

Python Version: Python 2.7.5

<!-- run 'python -V' and paste it here) --> 

Any other relevant files/configs (eg: path files) 

<!-- Anything else which may be of relevance -->

<!-- ===============END OF ISSUE SECTION=============== -->

