File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+
3+ import json
4+
5+ try :
6+ from urllib .request import urlopen , Request
7+ from urllib .parse import quote
8+ from urllib .error import URLError
9+ except ImportError :
10+ from urllib2 import urlopen , Request
11+ from urllib import quote
12+ from urllib2 import URLError
13+
14+
15+ class Botan (object ):
16+ token = ''
17+ url_template = 'https://api.botan.io/track?token={token}&uid={uid}&name={name}'
18+
19+ def __init__ (self , token ):
20+ self .token = token
21+
22+ def track (self , message , event_name = 'event' ):
23+ try :
24+ uid = message .chat_id
25+ except AttributeError :
26+ print ('no chat_id in message' )
27+ return False
28+ data = json .dumps (message .__dict__ )
29+ try :
30+ url = self .url_template .format (token = str (self .token ), uid = str (uid ), name = quote (event_name ))
31+ request = Request (url ,
32+ data = data ,
33+ headers = {'Content-Type' : 'application/json' })
34+ response = urlopen (request , json .dumps (data ))
35+ if response .getcode () != 200 :
36+ return False
37+ return True
38+ except URLError as error :
39+ print ('botan track error ' + str (error .code ) + ':' + error .reason )
40+ print (url )
41+ return False
42+
You can’t perform that action at this time.
0 commit comments