-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
191 lines (157 loc) · 7.06 KB
/
utils.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import plivohelper
from time import sleep
from config import *
ANSWERED = 'http://127.0.0.1:5000/'
SOUNDS_ROOT = 'http://176.58.125.166/~csik/sounds/'
EXTRA_DIAL_STRING = "bridge_early_media=true,hangup_after_bridge=true"
def init_logging(file):
try:
import logging
logger = logging.getLogger(file)
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler('logs/telephony.log', mode='a')
handler.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
logger.addHandler(ch)
return logger
except Exception, e:
logger.error('Failed to open logger', exc_info=True)
return None
logger = init_logging('utils')
def call(to_number, from_number, gateway, answered=ANSWERED,extra_dial_string=EXTRA_DIAL_STRING):
"""
Make a call, using (gateway, phone_number)
TODO: actually make the other parameters correspond
"""
logger.info("gateway = "+gateway)
logger.info("from {0}, to {1}".format(from_number, to_number))
logger.info("answered = "+answered)
# Define Channel Variable - http://wiki.freeswitch.org/wiki/Channel_Variables
# Create a REST object
plivo = plivohelper.REST(REST_API_URL, SID, AUTH_TOKEN, API_VERSION)
call_params = {
'From': from_number, # Caller Id
'To' : to_number, # User Number to Call
'Gateways' : gateway, # Gateway string to try dialing separated by comma. First in list will be tried first
'GatewayCodecs' : "", # Codec string as needed by FS for each gateway separated by comma
'GatewayTimeouts' : "20,20", # Seconds to timeout in string for each gateway separated by comma
'GatewayRetries' : "2,1", # Retry String for Gateways separated by comma, on how many times each gateway should be retried
'ExtraDialString' : extra_dial_string,
'AnswerUrl' : answered+'answered/',
'HangupUrl' : answered+'hangup/',
'RingUrl' : answered+'ringing/',
#'TimeLimit' : '15',
#'HangupOnRing': '0',
}
request_uuid = ""
#Perform the Call on the Rest API
try:
result = plivo.call(call_params)
logger.info(str(result))
return result
except Exception, e:
logger.error('Failed to make utils.call', exc_info=True)
pass
return "Error"
# ONLY CONNECTS FIRST SUCCESSFUL CONNECTION
def group_call(gateway, phone_numbers, answered):
print "gateway = "+gateway
print "phone_numbers = "+phone_numbers
print "answered = "+answered
# Define Channel Variable - http://wiki.freeswitch.org/wiki/Channel_Variables
extra_dial_string = "bridge_early_media=true,hangup_after_bridge=true"
# Create a REST object
plivo = plivohelper.REST(REST_API_URL, SID, AUTH_TOKEN, API_VERSION)
# Initiate a new outbound call to user/1000 using a HTTP POST
# All parameters for bulk calls shall be separated by a delimeter
call_params = {
'Delimiter' : '>', # Delimter for the bulk list
'From': SHOW_HOST, # Caller Id
'To' : phone_numbers, # User Numbers to Call separated by delimeter
'Gateways' : gateway, # Gateway string for each number separated by delimeter
'GatewayCodecs' : "", # Codec string as needed by FS for each gateway separated by delimeter
'GatewayTimeouts' : "20>20", # Seconds to timeout in string for each gateway separated by delimeter
'GatewayRetries' : "2>1", # Retry String for Gateways separated by delimeter, on how many times each gateway should be retried
'ExtraDialString' : extra_dial_string,
'AnswerUrl' : answered+'answered/',
'HangupUrl' : answered+'hangup/',
'RingUrl' : answered+'ringing/',
# 'ConfirmSound' : "test.wav",
# 'ConfirmKey' : "1",
# 'RejectCauses': 'NO_USER_RESPONSE,NO_ANSWER,CALL_REJECTED,USER_NOT_REGISTERED',
# 'ConfirmSound': '/usr/local/freeswitch/sounds/en/us/callie/ivr/8000/ivr-requested_wakeup_call_for.wav',
# 'ConfirmKey': '9'
# 'TimeLimit' : '10>30',
# 'HangupOnRing': '0>0',
}
request_uuid = ""
#Perform the Call on the Rest API
try:
result = plivo.call(call_params)
print result
except Exception, e:
print e
raise
return [result.get('Success'),result.get('RequestUUID')]
# CONNECTS MULTIPLES
def bulk_call(to_numbers, from_number, gateway, answered=ANSWERED,extra_dial_string=EXTRA_DIAL_STRING):
# Create a REST object
plivo = plivohelper.REST(REST_API_URL, SID, AUTH_TOKEN, API_VERSION)
# Initiate a new outbound call using a HTTP POST
# All parameters for bulk calls shall be separated by a delimeter
call_params = {
'Delimiter' : '>', # Delimter for the bulk lst
'From': from_number, # Caller Id
'To' : to_numbers, # User Numbers to Call separated by delimeter
'Gateways' : gateway, # Gateway string for each number separated by delimeter
'GatewayCodecs' : "", # Codec string as needed by FS for each gateway separated by delimeter
'GatewayTimeouts' : "20>20", # Seconds to timeout in string for each gateway separated by delimeter
'GatewayRetries' : "2>1", # Retry String for Gateways separated by delimeter, on how many times each gateway should be retried
'ExtraDialString' : extra_dial_string,
'AnswerUrl' : answered_URL+'answered/',
'HangupUrl' : answered_URL+'hangup/',
'RingUrl' : answered_URL+'ringing/',
# 'ConfirmSound' : "test.wav",
# 'ConfirmKey' : "1",
# 'RejectCauses': 'NO_USER_RESPONSE,NO_ANSWER,CALL_REJECTED,USER_NOT_REGISTERED',
# 'ConfirmSound': '/usr/local/freeswitch/sounds/en/us/callie/ivr/8000/ivr-requested_wakeup_call_for.wav',
# 'ConfirmKey': '9'
# 'TimeLimit' : '10>30',
# 'HangupOnRing': '0>0',
}
request_uuid = ""
#Perform the Call on the Rest API
try:
result = plivo.bulk_call(call_params)
logger.info(str(result))
return [result.get('Success'),result.get('RequestUUID')]
except Exception, e:
logger.error('Failed to make utils.bulkcall', exc_info=True)
pass
return "Error"
class ZMQ(object):
def __init__(self, app=None):
self.zmq = None
self.app = None
if app is not None:
self.init_app(app)
def init_app(self, app):
# register extension with app
app.extensions = getattr(app, 'extensions', {})
self._connect(app)
app.extensions['zmq'] = self.zmq
self.app = app
def _connect(self, app):
context = zmq.Context()
self.zmq = context.socket(app.config['ZMQ_SOCKET_TYPE'])
self.zmq.bind(app.config['ZMQ_BIND_ADDR'])
def __getattr__(self, attr):
return getattr(self.zmq, attr)