forked from pedosb/celcombiller
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcelcombiller_reducer.py
More file actions
65 lines (52 loc) · 1.84 KB
/
celcombiller_reducer.py
File metadata and controls
65 lines (52 loc) · 1.84 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
"""
celcombiller AGI:
* Consult the user balance and authorize a call
* Save the call CDRs
* Update the user balance
"""
# pylint: disable=C0103
from datetime import datetime
import asterisk.agi
#from asterisk.agi import AGIAppError
from models import User
import requests
import json
adm_user = 'admin'
adm_pssw = 'adm123'
agi = asterisk.agi.AGI()
from_user = User.query.filter_by(
clid=agi.env['agi_callerid']).first()
to_user = User.query.filter_by(
clid=agi.get_variable('CDR(B-Number)')).first()
if agi.get_variable('DIALSTATUS') == 'ANSWER':
# Time from answer to hangup
billsec = int(agi.get_variable('CDR(billsec)'))
# Time the user answered
answer = datetime.fromtimestamp(float(agi.get_variable('CDR(answer,u)')))
# Create a session
s = requests.Session()
# Login the session
s.post('http://localhost:5000/login',\
data={'username':adm_user, 'password': adm_pssw})
# Create a new CDR record
payload = '{"answer":"'+ str(answer)+'", "billsec":"'+ \
str(billsec)+'", "from_user_id": '+\
str(from_user.id_)+', "to_user_id":'+ str(to_user.id_)+'}'
#Send the requestto update the user balance
r = s.post('http://localhost:5000/api/cdr', json=json.loads(payload),\
headers={'content-type': 'application/json'})
#TODO: Handle when the request fail
if r.ok == False:
pass
print from_user.id_
payload = '{"signal":"-", "type_":"decrease", "value": "'+ str(billsec) +\
'", "userId":'+ str(from_user.id_) +'}'
# Send the requestto update the user balance
r = s.post('http://localhost:5000/api/balance',\
json=json.loads(payload),\
headers={'content-type': 'application/json'})
#TODO: Handle when the request fail
if r.ok == False:
pass
s.close()