Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
Some statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
YogurtTheHorse committed Sep 23, 2016
1 parent b8e9c41 commit 36ab81a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
47 changes: 47 additions & 0 deletions botan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ----------------
# dont forget 'pip install requests' first
# ----------------
# usage example:
#
# import botan
#
# print botan.track(1111, 1, {'text':2}, 'Search')

import requests
import json


TRACK_URL = 'https://api.botan.io/track'
SHORTENER_URL = 'https://api.botan.io/s/'


def track(token, uid, message, name='Message'):
try:
r = requests.post(
TRACK_URL,
params={"token": token, "uid": uid, "name": name},
data=json.dumps(message),
headers={'Content-type': 'application/json'},
)
return r.json()
except requests.exceptions.Timeout:
# set up for a retry, or continue in a retry loop
return False
except (requests.exceptions.RequestException, ValueError) as e:
# catastrophic error
print(e)
return False


def shorten_url(url, botan_token, user_id):
"""
Shorten URL for specified user of a bot
"""
try:
return requests.get(SHORTENER_URL, params={
'token': botan_token,
'url': url,
'user_ids': str(user_id),
}).text
except:
return url
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import config

import statistics

from telegram.ext.dispatcher import run_async
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram.ext import Job
Expand Down Expand Up @@ -90,6 +92,8 @@ def start(bot, update):
image = None
buttons = None

statistics.track(c_id, update.message.text, 'Start')

def rep(txt, btns=None, photo=None):
global msg, image, buttons

Expand Down Expand Up @@ -119,8 +123,11 @@ def rep(txt, btns=None, photo=None):
reply(c_id, bot, msg, buttons, image)

def setname(bot, update):

txt = update.message.text
if len(txt) > len('/setname'):
statistics.track(update.message.chat_id, update.message.text, 'Setname')

name = txt[len('/setname')+1:]
usermanager.setname(update.message.chat_id, name)

Expand Down Expand Up @@ -294,6 +301,7 @@ def rep(txt, btns=None, photo=None):

def msg(bot, update):
c_id = update.message.chat_id
statistics.track(c_id, update.message.text)

global msg, image, buttons
msg = ''
Expand Down Expand Up @@ -370,6 +378,7 @@ def leaderboard(bot, update):
bot.sendMessage(update.message.chat_id, text=msg)

def stop(bot, update):
statistics.track(update.message.chat_id, update.message.text, 'Stop')
usermanager.delete(update.message.chat_id)

def cesar(bot, update):
Expand Down
12 changes: 12 additions & 0 deletions statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import botan
import config

tkn = None

if hasattr(config, 'BOTANIO_TOKEN'):
tkn = config.BOTANIO_TOKEN

def track(uid, message, name='Message'):
if tkn is not None:
botan.track(tkn, uid, message, name)

0 comments on commit 36ab81a

Please sign in to comment.