-
Notifications
You must be signed in to change notification settings - Fork 1
/
getReplies.py
executable file
·51 lines (43 loc) · 1.35 KB
/
getReplies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
import urllib
import urllib2
import json
import datetime
import sys
smsMessageId = sys.argv[1]
key="YOURKEY"
secret="YOURSECRET"
authfile = '/tmp/telstraauth'
now = datetime.datetime.today()
def GetAuthToken( url, now, key, secret ):
response = urllib2.urlopen(url)
newtokendict = json.loads(response.read())
newtokendict["datetime"] = str(now)
authtoken = newtokendict["access_token"]
newjsontoken = json.JSONEncoder().encode(newtokendict)
f = open(authfile,'w')
print "new token : "+newjsontoken
f.write(newjsontoken)
f.close()
return authtoken
f = open(authfile,'r+')
filetoken = f.read()
f.close()
try:
dicttoken = json.loads(filetoken)
authtoken = dicttoken["access_token"]
dtobj = datetime.datetime.strptime(dicttoken["datetime"], '%Y-%m-%d %H:%M:%S.%f')
expires = dtobj + datetime.timedelta(0,int(dicttoken["expires_in"]))
if expires < now:
print "expired"
authtoken = GetAuthToken(url, now, key, secret)
else:
print "not expired"
except ValueError:
print "Invalid JSON. Retrieving New Token"
authtoken = GetAuthToken(url, now, key, secret)
headers = { 'Authorization':'Bearer '+str(authtoken) }
url = "https://api.telstra.com/v1/sms/messages/"+smsMessageId+"/response"
req = urllib2.Request(url,headers=headers)
msg = urllib2.urlopen(req)
print msg.read()