-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
113 lines (75 loc) · 3.13 KB
/
main.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
import webapp2
from google.cloud import translate
import twilio
from twilio.rest import TwilioRestClient
import twilio.twiml
import os
from config import PHONE_DIRECTORY, FROM_PARENTS, MY_PHONE_NUMBER, PARENTS
# Twilio Account Information
TWILIO_ACCOUNT_SID=os.environ.get("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN=os.environ.get("TWILIO_AUTH_TOKEN")
TWILIO_NUMBER=os.environ.get("TWILIO_NUMBER")
translate_client = translate.Client()
class Home(webapp2.RequestHandler):
"""A GET Request Handler"""
def get(self):
"""Receives a GET request"""
self.response.write('Hello, DevelopHerDevelopHim Viewers!')
class ProcessText(webapp2.RequestHandler):
"""A POST Request Handler"""
def post(self):
"""Receives a POST request"""
from_phone_number = self.request.get("From")[1:]
message = self.request.get("Body").lower()
if from_phone_number not in PHONE_DIRECTORY:
send_text_message(from_phone_number, "This is for family only")
to_phone_number, response = process_message(from_phone_number, message)
send_text_message(to_phone_number, response)
def translates(to_phone_number, message):
"""Makes an API call to translate the message.
>>> translates(MY_PHONE_NUMBER, "\xec\x9e\x98 \xec\xa7\x80\xeb\x83\x88\xec\x96\xb4\xec\x9a\x94?")
'How are you?'
"""
translation = translate_client.translate(
message,
target_language=PHONE_DIRECTORY[to_phone_number]['language'])
return translation[u'translatedText'].encode('utf-8')
def process_message(from_phone_number, message):
"""Processes message, returns from who and translated message
>>> process_message(PARENTS["dad"], "\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x84\xb8\xec\x9a\x94") # doctest: +ELLIPSIS
('...', 'DAD: Good morning')
"""
to_phone_number = MY_PHONE_NUMBER
if from_phone_number == MY_PHONE_NUMBER:
to_who = message[:3]
to_phone_number = PARENTS[to_who]
message = message[3:]
translated_message = translates(to_phone_number, message)
# if its from mom or dad, add that its from them in the message
if from_phone_number != MY_PHONE_NUMBER:
translated_message = "{}: {}".format(FROM_PARENTS[from_phone_number].upper(), translated_message.encode('utf-8'))
return (to_phone_number, translated_message)
def send_text_message(phone, message):
"""sends a text message to the phone number
>>> send_text_message(MY_PHONE_NUMBER, "this is only a test")
>>>
"""
try:
twilio_client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
message = twilio_client.messages.create(
body=message,
to="+"+phone,
from_=TWILIO_NUMBER
)
except twilio.TwilioRestException as e:
print e
app = webapp2.WSGIApplication([
(r'/', Home),
(r'/translate', ProcessText)
],
debug=True)
def main():
from paste import httpserver
httpserver.serve(app, host='127.0.0.1', port='8080')
if __name__ == '__main__':
main()