forked from cs298-398f23/HueLightFinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_receiver
51 lines (39 loc) · 1.62 KB
/
message_receiver
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
# Import libraries needed for sending and receiving SMS text messages
import http.server
import urllib.parse
import twilio.rest
# Global variables with information needed to send and receive messages.
# Your Twilio phone number
# TODO()
# Your Account SID from twilio.com/console
# TODO()
# Your Auth Token from twilio.com/console
# TODO()
# Creates a client application that can be made to send and receive text messages
client = twilio.rest.Client(account_sid, auth_token)
def format_phone_number(from_number):
from_number = "(" + from_number[2:5] + ") " + from_number[5:8] + '-' + from_number[8:12]
return from_number
def receive_message(from_number, message_text):
formatted_num = format_phone_number(from_number)
print("Text message received from {}: {}".format(formatted_num, message_text))
##### The rest of the code handles the receipt of text messages #####
##### IMPORTANT! Do not modify below here!
class SMSReceiver(http.server.BaseHTTPRequestHandler):
def do_GET(self):
try:
qs = urllib.parse.parse_qs(self.path[self.path.index('?') + 1:])
receive_message(qs['From'][0], qs['Body'][0])
except Exception:
import sys, traceback
print("Error while processing received text message:", file=sys.stderr)
traceback.print_exc()
self.send_response(204) # Empty response to Twilio
def log_message(self, *args): # Makes HTTPServer quiet
return
def main():
server_address = ('', 5000)
with http.server.HTTPServer(server_address, SMSReceiver) as httpd:
httpd.serve_forever()
if __name__ == "__main__":
main()