Exemplo n.º 1
0
    def telegram_command(self, message, inline=False):

        chat_id = message['chat']['id']

        if inline:
            if inline.startswith('/metrika_start'):
                cmd = inline[14:]
                token_id, counter_id = cmd.split("#")
                self.add(chat_id, token_id, counter_id)
                self.metrika_help(chat_id)

            if inline.startswith('/metrika_stop'):
                cmd = inline[13:]
                token_id, counter_id = cmd.split("#")
                self.delete(chat_id, token_id, counter_id)
                self.help(chat_id)
            return

        if message['text'] == '/metrika' or message['text'] == '/metrika@codex_bot':

            send_to_chat(self.help(chat_id), chat_id)

        if message['text'].startswith('/metrika_start'):

            Handler.start(chat_id)

        if message['text'].startswith('/metrika_stop'):

            self.metrika_stop(chat_id)

        if message['text'].startswith('/metrika_help'):

            self.metrika_help(chat_id)

        if message['text'].startswith('/metrika_get'):

            logging.info("Get metrika statistics: [%s]" % (chat_id))

            self.get(chat_id)

        if message['text'].startswith('/metrika_counters'):

            params = message['text'].split(' ')
            if len(params) == 2 and params[0] == "/metrika_counters":
                metrikaAPI = MetrikaAPI(params[1], '', chat_id)
                send_to_chat(metrikaAPI.get_counters(), chat_id)
            else:
                send_to_chat(self.help(), chat_id)

        if message["text"].startswith("/today"):
            self.get(chat_id, "today")
        if message["text"].startswith("/weekly"):
            self.get(chat_id, "weekly")
        if message["text"].startswith("/monthly"):
            self.get(chat_id, "monthly")
Exemplo n.º 2
0
 def get_counters(token, cmd):
     metrikaAPI = MetrikaAPI(token.token, '', token.chat_id)
     counters = metrikaAPI.get_counters()
     buttons = []
     buttons_row = []
     for counter in counters:
         buttons_row.append({'text': counter["name"], 'callback_data': "/metrika_%s%s#%s" % (cmd, token.id, counter["id"])})
         CConfig.CACHE['metrika_%s_%s' % (token.id, counter["id"])] = counter["name"]
         if len(buttons_row) == 2:
             buttons.append(buttons_row[:])
             buttons_row = []
     if len(buttons_row):
         buttons.append(buttons_row[:])
     return buttons
Exemplo n.º 3
0
    def get(self, chat_id, cmd):
        metrikas = Metrika.select().where(Metrika.chat_id == chat_id)
        message = "%s\n\n" % self.stats(cmd)
        for metrika in metrikas:
            try:
                metrikaAPI = MetrikaAPI(metrika.oauth_token, metrika.counter_id, chat_id)
                result = metrikaAPI.get_visit_statistics(cmd)
                if result:
                    users, hits = result
                    message += "%s:\n%d уникальных посетителей\n%d просмотров\n\n" % (metrika.counter_name,
                                                                                  users, hits)
            except Exception as e:
                logging.warning("Metrika API exception: %s" % e)

        send_to_chat(message, chat_id)