-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms-switch.py
127 lines (95 loc) · 3.18 KB
/
sms-switch.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
from bottle import Bottle, run, route, request, static_file
import toml
import json
from pprint import pprint
app = Bottle()
with open("config.toml") as f:
config = toml.load(f)
#
# Storing users is probably better done in a database, but as this is a demo
# I'll just store them in files on disk for now.
#
def get_users():
return json.load(open("users.json", "r"))
"""
Example content of users.json
[
{"name": "Martin", "phonenumber":"+4600000000"},
{"name": "Rene", "phonenumber":"+46700000000"},
]
"""
def get_active_user_name():
try:
with open("active_user.txt", "r") as f:
return f.read().strip()
except:
return None
def set_active_user(username):
with open("active_user.txt", "w") as f:
f.write(username)
#
# Some helper functions to make the code below a bit more readable
#
def get_user_from_number(phonenumber):
for user in get_users():
if user['phonenumber'] == phonenumber:
return user
def get_user_from_name(name):
for user in get_users():
if user['name'].lower() == name.lower():
return user
def get_active_user():
username = get_active_user_name()
if username is not None:
return get_user_from_name(username)
#
# Request handler for incoming SMS
#
@app.route('/incoming-sms', method='POST')
def incoming_sms():
to_number = request.forms.get('to')
from_number = request.forms.get('from')
message = request.forms.get('message')
# Verify that the SMS is from a trusted user
user = get_user_from_number(from_number)
if user is None:
return "Hey, you are not allowed to be here"
# Set a new user as active
if message.lower().startswith("set "):
newUser = get_user_from_name(message[4:])
if newUser is None:
return f"No user with name '{message[4:]}' found"
# Store the new active user
set_active_user(newUser['name'])
return f"Set {newUser['name']} as active, they will recieve incoming calls now"
# Ask the system which user is active
elif message.lower().startswith("who"):
activeUser = get_active_user()
if activeUser == None:
return "No active user at the moment"
else:
return f"{activeUser['name']} is the active user"
elif message.lower().startswith("disable"):
set_active_user("")
return f"Disabled the phone switch (removed the active user)"
return "Sorry, I don't understand that command"
#
# Request handler for incoming calls
#
@app.route('/incoming-call', method='POST')
def incoming_call():
to_number = request.forms.get('to')
from_number = request.forms.get('from')
active_user = get_active_user()
if active_user is not None:
# Connect the call to the active user. If the connect failes
# or the active user is already in a call, play a recording
voice_start = {
'connect': active_user['phonenumber'],
}
else:
voice_start = {
"play": "https://files.46elks.com/alge/demo_busy.wav"
}
return json.dumps(voice_start)
run(app, host=config['host'], port=config['port'])