-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
231 lines (182 loc) · 8.36 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from flask import Flask, request, redirect, url_for
import twilio.twiml
import twilio.rest
from twilio.twiml.messaging_response import MessagingResponse
from twilio.twiml.voice_response import *
import os
from googletrans import Translator
app = Flask(__name__)
# Host number
host_number = "+18733715591"
bot_number = "+16474909749"
callee_number = None
account_sid = os.environ['TWILIO_ACCOUNT_SID']
account_token = os.environ['TWILIO_AUTHY_TOKEN']
conference_name = "meeting123"
client = twilio.rest.Client(account_sid, account_token)
hostUrl = "https://92974f61.ngrok.io"
record_url = None
current_bot_in_meeting = "meeting123"
# chosen langs
caller_lang = None
callee_lang = None
# Language Maps
google_langs = {'1': 'en', '2': 'zh-cn', '3':'hi'}
alice_gather_langs = {'1': 'en-US', '2': 'cmn-Hans-CN','3': 'hi-IN'}
alice_say_langs = {'1': 'en-US', '2': 'zh-CN','3':'hi-IN'}
voice_dict = {'1': 'polly', '2': 'alice', '3': 'Polly.Aditi'}
@app.route('/', methods=['GET', 'POST'])
def index():
return 'Hello'
@app.route('/sms', methods=['GET', 'POST'])
def sms_reply():
from_number = request.form['From'] # get message sender phone number Ida's phone number
to_number = request.form['To'] # get message receiver phone number Twilio number
body = request.form['Body'] # get message body which should contain geocoded address, destination
resp = MessagingResponse()
# Add a text message
resp.message(from_number + " " + to_number + " " + body)
return str(resp)
# host number gets called
@app.route('/voice', methods=['GET','POST'])
def answer_call():
resp = VoiceResponse()
with resp.gather(numDigits=1, action="/select_caller_language",
method="POST") as g:
g.say("Please select your language: 1 for English 2 for Mandarin and 3 for Hindi", voice="polly")
return str(resp)
@app.route('/select_caller_language', methods=['GET','POST'])
def select_caller_language():
global caller_lang
caller_lang = request.values.get('Digits', None)
resp = VoiceResponse()
with resp.gather(numDigits=1, action="/select_callee_language",
method="POST") as g:
g.say("Please select your friend's language: 1 for English 2 for Mandarin and 3 for Hindi", voice="polly")
return str(resp)
@app.route('/select_callee_language', methods=['GET','POST'])
def select_callee_language():
global callee_lang
callee_lang = request.values.get('Digits', None)
resp = VoiceResponse()
with resp.gather(numDigits=10, action="/connect_callee",
method="POST") as g:
g.say("Please enter 10 digit phone number", voice="polly")
return str(resp)
# call callee and put caller in meeting 123
@app.route('/connect_callee', methods=['GET','POST'])
def connect_callee():
global callee_number # callee
callee_number = request.values.get('Digits', None)
# make the call to callee
client.calls.create(to=callee_number, from_=host_number,
url=hostUrl + "/voice/callee_on_connect" )
conference_room_name = "meeting123"
# call robot from host
client.calls.create(to=bot_number, from_=host_number, url=hostUrl + '/voice/on_caller_connect_robot')
resp = VoiceResponse()
# put caller to meeting 123
with resp.dial(method="POST") as d:
d.conference(name=conference_room_name, muted=False, beep=False,
statusCallbackEvent="join leave", statusCallback="/voice/conference",
statusCallbackMethod="POST")
return str(resp)
# put callee to meeting 1234
@app.route("/voice/callee_on_connect", methods=['GET', 'POST'])
def handle_host_call_customer_service():
global callee_call_sid
callee_call_sid = request.values.get('CallSid')
resp = VoiceResponse()
conference_room_name = "meeting1234"
with resp.dial(method="POST") as d:
d.conference(name=conference_room_name, muted=False, beep=False,
statusCallbackEvent="join leave", statusCallback="/voice/conference",
statusCallbackMethod="POST")
return str(resp)
@app.route("/voice/on_caller_connect_robot", methods=['GET', 'POST'])
def handle_on_caller_connect_robot():
global current_bot_in_meeting
resp = VoiceResponse()
resp.say("bot joins meeting 123 ", voice="polly")
current_bot_in_meeting = "meeting123"
print("before caller connect to robot 123")
# put bot to meeting 123
with resp.dial(method="POST") as d:
d.conference(name="meeting123", muted=False, beep=False,
statusCallbackEvent="join leave", statusCallback="/voice/conference",
statusCallbackMethod="POST")
print("no sth to play")
return str(resp)
@app.route("/voice/on_callee_connect_robot", methods=['GET', 'POST'])
def handle_on_callee_connect_robot():
global current_bot_in_meeting
resp = VoiceResponse()
resp.say("bot joins meeting 1234 ", voice="polly")
current_bot_in_meeting = "meeting1234"
print("before callee connect to robot 1234")
# put bot to meeting 1234
with resp.dial(method="POST") as d:
d.conference(name="meeting1234", muted=False, beep=False,
statusCallbackEvent="join leave", statusCallback="/voice/conference",
statusCallbackMethod="POST")
return str(resp)
# handler when call comes into robot number
@app.route("/robot", methods=['GET', 'POST'])
def handle_robot():
global record_url
resp = VoiceResponse()
translator = Translator()
if record_url != None:
resp.say("reply as follows",loop=1,voice='polly')
# "meeting1234" record: english
#meeting123 record: chinese
#resp.pause(length=2)
if current_bot_in_meeting:
print("current meeting: " + current_bot_in_meeting + "record:" + record_url)
if current_bot_in_meeting == "meeting1234":
resp.say(translator.translate(record_url, src=google_langs[callee_lang], dest=google_langs[caller_lang]).text,loop=1, voice=voice_dict[caller_lang], language=alice_say_langs[caller_lang])
else:
resp.say(translator.translate(record_url, src=google_langs[caller_lang], dest=google_langs[callee_lang]).text,loop=1, voice=voice_dict[callee_lang], language=alice_say_langs[callee_lang])
print("has record to play")
print("record_url = " + record_url)
print("no sth to play")
if current_bot_in_meeting == "meeting1234":
gather = Gather(input='speech', action='/handle_transcribe', speechTimeout="auto",timeout= 5, language=alice_gather_langs[caller_lang])
else:
gather = Gather(input='speech', action='/handle_transcribe', speechTimeout="auto",timeout= 5, language=alice_gather_langs[callee_lang])
gather.say("please reply",voice='polly')
resp.append(gather)
print("bot hangs up the call")
return str(resp)
@app.route("/handle_transcribe", methods=['GET', 'POST'])
def handle_transcribe():
global current_bot_in_meeting
global record_url
resp = VoiceResponse()
record_url = request.values.get('SpeechResult', None)
print("record: " + record_url)
resp.hangup()
return str(resp)
# handler when participant join or leave meeting
@app.route("/voice/conference", methods=['GET', 'POST'])
def handle_conference():
event = request.values.get("StatusCallbackEvent")
if event == "participant-join":
print("someone join the meeting")
elif event == "participant-leave":
print("bot leaves the meeting")
if current_bot_in_meeting == "meeting123":
client.calls.create(to=bot_number, from_=host_number,
url=hostUrl + "/voice/on_callee_connect_robot" )
elif current_bot_in_meeting == "meeting1234":
client.calls.create(to=bot_number, from_=host_number,
url=hostUrl + "/voice/on_caller_connect_robot" )
return ""
@app.route('/completed', methods=['GET','POST'])
def response_call():
x = request.form['SpeechResult']
resp = VoiceResponse()
resp.say(x, voice='polly')
return str(resp)
if __name__ == "__main__":
app.run()