/
CallerNameResolver.py
210 lines (149 loc) · 6.17 KB
/
CallerNameResolver.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
# coding: utf8
import websocket
import thread
import time
import json
import sys, os
import daemon
import daemon.pidfile
import logging
import ConfigParser
sys.path.append(os.path.dirname(__file__)+'/lib')
from GoogleContactName import googleAPI
from AsteriskRESTActions import asteriskRESTActions
from GoogleApiOAuth2 import googleOAuth
from daemon import runner
from translit import transliterate
logDir = "/var/log/asterisk_name_resolver"
logFormat = "[%(asctime)s] %(levelname)s {%(filename)s:%(lineno)d} - %(message)s"
logLevel = logging.DEBUG
class daemonApp:
stdin_path = '/dev/null'
stdout_path = '/dev/tty'
stderr_path = '/dev/tty'
pidfile_path = '/var/run/CallerNameResolver.pid'
pidfile_timeout = 5
class caller_name_resolver(daemonApp):
ws = None
mode = 0
config = None
contactDic = {}
def on_message(self, ws, message):
msg = json.loads(message)
if msg['type'] == 'StasisStart':
logging.debug( u"type={0}:channel_id={1}:caller_id={2}:caller_name={3}:target_number={4}".format(msg['type'], msg['channel']['id'], msg['channel']['caller']['number'], msg['channel']['caller']['name'], msg['channel']['dialplan']['exten']))
number = msg['channel']['caller']['number']
numberLen = len(number)
if numberLen > 10:
number = number[numberLen-10:] #Find by last 10 digit
currentContactContext = self.contactDic.get(msg['channel']['dialplan']['exten'])
if currentContactContext == None:
currentContactContext = self.contactDic.get('default')
if currentContactContext <> None:
logging.debug( "Using contact context:{0}".format(currentContactContext.contactName) )
name = currentContactContext.getContactName(number)
if name <> '':
logging.info( u'Found GoogleContact: {0} by caller number: {1}'.format(name.decode('utf8'), msg['channel']['caller']['number']) )
logging.debug( "Set contact name to channel " )
if msg['args'] <> []:
if msg['args'][0].strip() == 'useTranslit':
name = u"%s" % transliterate(name.decode('utf8'))
logging.debug( u"Transliterate name: %s" % name)
self.asteriskAction.set_channel_caller_name(msg['channel']['id'], name)
else:
logging.debug( "Not found contact context" )
logging.debug( "Set dialplan continue " )
self.asteriskAction.set_continue(msg['channel']['id'])
elif msg['type'] == 'ChannelCallerId':
logging.debug( u"type={0}:channel_id={1}:caller_id={2}:caller_name={3}:target_number={4}".format(msg['type'], msg['channel']['id'], msg['channel']['caller']['number'], msg['channel']['caller']['name'], msg['channel']['dialplan']['exten']))
else:
logging.debug( u"type={0}:channel_id={1}:caller_id={2}:caller_name={3}:target_number={4}".format(msg['type'], msg['channel']['id'], msg['channel']['caller']['number'], msg['channel']['caller']['name'], msg['channel']['dialplan']['exten']))
def on_error(self, ws, error):
logging.error( "### error ###:{0}".format(error) )
def on_close(self, ws):
logging.debug( "### closed ###" )
def on_open(self, ws):
logging.debug( "### open ###" )
def run(self):
if self.ws is None:
ari_url = self.config.get('default', 'ari_url')
ari_login = self.config.get('default', 'ari_login')
ari_pass = self.config.get('default', 'ari_pass')
if self.mode == 1:
websocket.enableTrace(True)
try:
self.ws = websocket.WebSocketApp("ws://{0}/ari/events?api_key={1}:{2}&app=NameResolver".format(ari_url, ari_login, ari_pass),
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
self.ws.on_close = self.on_close
logging.info( "Service start " )
logging.info( "-=============================================================================================-" )
self.ws.run_forever()
except:
logging.debug( 'Erorr websocket:' )
finally:
if self.ws is not None:
logging.debug( "Socket close" )
self.ws.close()
self.ws = None
def __init__(self, config=None, action=None):
self.asteriskAction = action
if config is not None:
self.config = config
for sec in config.sections():
names = sec.rsplit('contact_')
if (len(names) == 2 and (dict(config.items(sec))).get('accesscode') <> None):
self.contactDic[names[1]] = googleAPI(config, names[1])
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
logging.debug( "Service exit " )
def __del__(self):
if self.contactDic <> None:
for x in self.contactDic:
del x
self.contactDic = None
if self.asteriskAction <> None:
del self.asteriskAction
self.asteriskAction = None
if self.ws is not None:
logging.debug( "Socket close" )
self.ws.close()
del self.ws
self.ws = None
logging.info( "-=============================================================================================-" )
logging.info( "Service destruct" )
if __name__ == "__main__":
config = ConfigParser.RawConfigParser()
config.read(os.path.dirname(__file__)+'/CallerNameResolver.config')
if config.get('default', 'log_level') == 'INFO':
logLevel = logging.INFO
if len(sys.argv) > 1:
if not os.path.exists(logDir):
os.makedirs(logDir)
handler = logging.FileHandler(logDir+'/message.log', "a", encoding = "UTF-8")
formatter = logging.Formatter(logFormat)
handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(logLevel)
if sys.argv[1] == 'start':
action = asteriskRESTActions(config)
with caller_name_resolver(config, action) as instance:
daemon_runner = runner.DaemonRunner(instance)
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()
elif sys.argv[1] == 'stop':
# instance = caller_name_resolver()
instance = daemonApp()
daemon_runner = runner.DaemonRunner(instance)
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()
else:
logging.basicConfig(format = logFormat, level = logLevel)
action = asteriskRESTActions(config)
with caller_name_resolver(config, action) as instance:
instance.mode = 1
instance.run()