-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
executable file
·29 lines (26 loc) · 1.28 KB
/
client.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
#!/usr/bin/env python3
import sys
import json
import asyncio
import websockets
async def handler(loop):
try:
async with websockets.connect('ws://localhost:8765') as websocket:
if len(sys.argv) == 2:
await websocket.send(json.dumps({ 'action' : 'call', 'destination' : sys.argv[1] }))
while(loop.is_running()):
message = await websocket.recv()
event = json.loads(message)
print('EVENT : '+json.dumps(event))
if event['Event-Name'] == 'CHANNEL_PARK':
await websocket.send(json.dumps({ 'action' : 'answer', 'uuid' : event['Unique-ID'] }))
elif event['Event-Name'] == 'CHANNEL_ANSWER':
await websocket.send(json.dumps({ 'action' : 'play', 'uuid' : event['Unique-ID'], 'file' : 'welcome.wav' }))
elif event['Event-Name'] == 'PLAYBACK_STOP':
await websocket.send(json.dumps({ 'action' : 'hangup', 'uuid' : event['Unique-ID'] }))
elif event['Event-Name'] == 'CHANNEL_HANGUP':
await websocket.close()
except BaseException as e:
print("ERROR : {} : {}".format(type(e).__name__, e))
loop = asyncio.get_event_loop()
loop.run_until_complete(handler(loop))