-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
executable file
·51 lines (44 loc) · 1.73 KB
/
server.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/env python3
import sys
import json
import asyncio
import websockets
from switchio import get_connection
msisdn = None
sockets = []
async def commands(websocket, path):
global msisdn
sockets.append(websocket)
while(loop.is_running()):
message = await websocket.recv()
command = json.loads(message)
print(command)
if (command['action'] == 'call' and command['destination']):
connection.api('originate {origination_caller_id_number='+msisdn+'}sofia/gateway/mss/'+command['destination']+' &park()')
async def events(host, loop):
global connection
while(loop.is_running()):
event = await connection.recv_event()
name = event['Event-Name']
uuid = event['Unique-ID']
caller = event['Caller-Caller-ID-Number']
called = event['Caller-Destination-Number']
event = caller+' => '+called+' : '+name
print(event)
global sockets
for socket in sockets:
await socket.send(event)
if name == 'CHANNEL_PARK':
connection.api('uuid_answer '+uuid)
elif name == 'CHANNEL_ANSWER':
connection.api('uuid_broadcast '+uuid+' playback::/opt/media/welcome.wav')
elif name == 'PLAYBACK_STOP':
connection.api('uuid_kill '+uuid+' playback::/opt/media/welcome.wav')
if len(sys.argv) == 2:
msisdn = sys.argv[1]
loop = asyncio.get_event_loop()
connection = get_connection('sipbox', loop=loop)
connection.connect()
connection.subscribe(['CHANNEL_PARK','CHANNEL_ANSWER','PLAYBACK_START','PLAYBACK_STOP','CHANNEL_HANGUP'])
loop.run_until_complete(websockets.serve(commands, 'localhost', 8765))
loop.run_until_complete(events('sipbox',loop))