forked from vonage-garage-rip/AnsweringMachineDetection
-
Notifications
You must be signed in to change notification settings - Fork 15
/
websocket-client.py
60 lines (47 loc) · 1.43 KB
/
websocket-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
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
#!/usr/bin/python
import websocket
import os
import nexmo
import json
import time
from dotenv import load_dotenv
load_dotenv()
def _get_private_key():
try:
return os.environ['PRIVATE_KEY']
except:
with open('private.key', 'r') as f:
private_key = f.read()
return private_key
HOSTNAME = os.getenv("HOSTNAME")
APP_ID = os.getenv("APP_ID")
PRIVATE_KEY = _get_private_key()
client = nexmo.Client(application_id=APP_ID, private_key=PRIVATE_KEY)
def on_message(ws, message):
data = json.loads(message)
print(data)
if data["beep_detected"] == True:
for id in data["uuids"]:
response = client.send_speech(id, text='Answering Machine Detected')
print(response)
time.sleep(4)
for id in data["uuids"]:
try:
client.update_call(id, action='hangup')
except:
pass
# response = client.send_speech(data["uuid"], text='Answering Machine Detected')
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("### opened ###")
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://"+HOSTNAME+"/socket",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()