-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvoice.py
554 lines (489 loc) · 21.7 KB
/
voice.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
from flask import Flask
from flask_ask import Ask, statement, question, session
from controller import welcome, re, condolences,ideas, get_alexa_location
import geocoder
import traceback
import random
import os
import requests
import logging
import hehe
import sms
import events
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
app = Flask(__name__)
ask = Ask(app, '/')
""" These functions handle what are essentially the beginning and end of the main use case of the skill."""
@ask.launch
def start_session():
""" This function is what initializes the application. It calls the welcome() method from controller.py
to generate a different welcome message each time """
welcome_text = welcome()
welcome_re_text = re()
return question(welcome_text).reprompt(welcome_re_text)
def callLvl2():
count=0
if session.attributes["Bed"] == "No":
count = count +1
if session.attributes["Eaten"] == "No":
count = count +1
if session.attributes["Showered"] == "No":
count = count +1
if session.attributes["Dressed"] == "No":
count = count +1
if session.attributes["Outside"] == "No":
count = count +1
if count>=3:
test = hehe.decide_mental_state("anubhavp28")
if test == True:
sms.sms_help("8709519238")
return test
def evaluate_answers():
"""This function evaluates the user's answers to the questions the skill poses if "NegativeFeeling" is called.
It evaluates the functions in order of usual routine. """
if session.attributes["Bed"] == "No":
return "Okay, well let's start by getting out of bed. You can do it!"
elif session.attributes["Eaten"] == "No":
return "That's okay, but you should try to eat. You got this!"
elif session.attributes["Showered"] == "No":
return "Hmmm. Maybe you should take a nice, hot shower. It will help you feel better."
elif session.attributes["Dressed"] == "No":
return "Alright. Well let's try getting dressed. You can do it!"
elif session.attributes["Outside"] == "No":
return "Well let's try going outside. It might elate you. "
else:
return "Good job doing all those things. When you're depressed, those little things can be the most difficult"
"""These functions handle intent logic for the voice interface. """
# @ask.intent('StartReport')
# def start_reporting():
# try:
# if session.attributes['State'] == 'Startedreport':
# return question("""Okay, please tell me the email address you would like the report sent to""")
# elif session.attributes["State"] == "Question 0 Answered":
# return question("""Okay, please tell me the email address you would like the report sent to""")
# elif session.attributes["State"] == "Question 1 Answered":
# return question("""Okay, please tell me the email address you would like the report sent to""")
# else:
# return question("""Okay, I can send a report for you. If you haven't told me how you feel,
# tell me after this, otherwise tell me the email address you would like the report sent to""")
# except:
# return question("""Okay, I can send a report for you. If you haven't told me how you feel,
# tell me after this, otherwise tell me the email address you would like the report sent to""")
# @ask.intent('Email')
# def get_name(email_address):
# session.attributes["Email"] = email_address
# return question("Okay, and what is your name?")
#
# @ask.intent('Name')
# def finish_report(person_name):
# message = "Okay, I've sent a report to {} about your day. Is there anything else I can do?"
# return question(message)
@ask.intent('PositiveFeeling')
def user_feels_good():
"""This function is triggered if the PositiveFeeling intent is detected. """
congrats = [
'That is so good to hear!',
'I am happy you feel good today',
'I am glad to hear that.',
'Oh happy day!',
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
'I am so happy about that!',
'That is so great!'
]
session.attributes["feeling"] = "Good"
session.attributes["State"] = "Question 0 Answered"
return question((random.choice(congrats)) + ' ' + 'Is there anything else you need? Want me to recommend a therapist?')
@ask.intent('NegativeFeeling')
def user_feels_bad():
"""This function is triggered if the NegativeFeeling intent is detected. This also kicks off the question to guage
whether the user has perfomed daily activities."""
condolence = condolences()
session.attributes["feeling"] = "Down"
session.attributes["State"] = "Question 1 Answered"
return question(condolence + " " + "Have you gotten out of bed today?")
""" The following functions are called depending on the user's answers."""
@ask.intent('BedYes')
def out_of_bed():
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Bed"] = "Yes"
session.attributes["State"] = "Question 2 Answered"
return question (message + " " + "Have you eaten today?")
@ask.intent('BedNo')
def not_out_of_bed():
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's too bad",
"It's okay."
])
session.attributes["State"] = "Question 2 Answered"
session.attributes["Bed"] = "No"
return question(message + " " + "Have you eaten today?")
@ask.intent('AteYes')
def eaten():
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Eaten"] = "Yes"
session.attributes["State"] = "Question 3 Answered"
return question(message + " " + "Have you showered today?")
@ask.intent('AteNo')
def not_eaten():
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Eaten"] = "No"
session.attributes["State"] = "Question 3 Answered"
return question(message + " " + "Have you showered today?")
@ask.intent('ShowerYes')
def showered():
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Showered"] = "Yes"
session.attributes["State"] = "Question 4 Answered"
return question(message + " " + "Have you gotten dressed?")
@ask.intent('ShowerNo')
def not_showered():
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Showered"] = "No"
session.attributes["State"] = "Question 4 Answered"
return question(message + " " + "Have you gotten dressed?")
@ask.intent('DressedYes')
def dressed():
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Dressed"] = "Yes"
session.attributes["State"] = "Question 5 Answered"
return question(message + " " + "Have you gone outside at all today?")
@ask.intent('DressedNo')
def not_dressed():
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Dressed"] = "No"
session.attributes["State"] = "Question 5 Answered"
return question(message + " " + "Have you gone outside at all today?")
@ask.intent('OutsideYes')
def outside():
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Outside"] = "Yes"
session.attributes["State"] = "Suggested"
response = evaluate_answers()
count=callLvl2()
if response == "Good job doing all those things. When you're depressed, those little things can be the most difficult.":
suggestion_inquiry = "Let's try something else to improve your mood."
else:
suggestion_inquiry = "Here's an idea for an extra way to improve your mood."
idea = ideas()
return question(message + " " + suggestion_inquiry + " " + idea + " " + "I hope I could help. Would you like another suggestion?")
@ask.intent('OutsideNo')
def not_outside():
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Outside"] = "No"
session.attributes["State"] = "Suggested"
response = evaluate_answers()
count=callLvl2()
suggestion_inquiry = "Let's also try something else to improve your mood."
idea = ideas()
if idea=="Try some events near you.":
event = events.events()
return question(
message + " " + response + " " + suggestion_inquiry + " " + idea + " " + event + " " + "would be a good start.")
else:
return question(message + " " + response + " " + suggestion_inquiry + " " + idea + " " + "I hope I could help. Would you like another suggestion?")
""" The following functions handle the built-in Amazon intents based on the session state. """
@ask.intent('AMAZON.NoIntent')
def handle_no():
try:
if session.attributes["State"] == "Question 0 Answered":
return statement("Okay. Check in with me again later!")
elif session.attributes["State"] == "Question 1 Answered":
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's too bad",
"It's okay."
])
session.attributes["State"] = "Question 2 Answered"
session.attributes["Bed"] = "No"
return question(message + " " + "Have you eaten today?")
elif session.attributes["State"] == "Question 2 Answered":
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Eaten"] = "No"
session.attributes["State"] = "Question 3 Answered"
return question(message + " " + "Have you showered today?")
elif session.attributes["State"] == "Question 3 Answered":
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Showered"] = "No"
session.attributes["State"] = "Question 4 Answered"
return question(message + " " + "Have you gotten dressed?")
elif session.attributes["State"] == "Question 4 Answered":
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Dressed"] = "No"
session.attributes["State"] = "Question 5 Answered"
return question(message + " " + "Have you gone outside at all today?")
elif session.attributes["State"] == "Question 5 Answered":
message = random.choice([
"That's too bad.",
"That's okay, we all have days like that.",
"I'm sorry. ",
"That's not good.",
"It's okay."
])
session.attributes["Outside"] = "No"
count=callLvl2()
session.attributes["State"] = "Suggested"
response = evaluate_answers()
suggestion_inquiry = "Let's also try something else to improve your mood."
idea = ideas()
if idea=="Try some events near you.":
event = events.events()
return question(
message + " " + response + " " + suggestion_inquiry + " " + idea + " " + event + " " + "would be a good start.")
else:
return question(
message + " " + response + " " + suggestion_inquiry + " " + idea + " " + "I hope I could help. Anything else I can do?")
elif session.attributes["State"] == "Suggested":
session.attributes["State"] = "AnythingElse"
return question("Okay, I hope that helped. Anything else I can do for you?")
elif session.attributes["State"] == "AnythingElse":
return statement("No problem. Check in with me later. Goodbye")
else:
return question("I'm sorry, I didn't get that. How are you feeling? ")
except:
return question("I'm sorry, I didn't get that. How are you feeling?")
@ask.intent('AMAZON.YesIntent')
def handle_yes():
try:
if session.attributes["State"] == "Question 0 Answered":
return question("Okay. What can I do for you?")
elif session.attributes["State"] == "Question 1 Answered":
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Bed"] = "Yes"
session.attributes["State"] = "Question 2 Answered"
return question(message + " " + "Have you eaten today?")
elif session.attributes["State"] == "Question 2 Answered":
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Eaten"] = "Yes"
session.attributes["State"] = "Question 3 Answered"
return question(message + " " + "Have you showered today?")
elif session.attributes["State"] == "Question 3 Answered":
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Showered"] = "Yes"
session.attributes["State"] = "Question 4 Answered"
return question(message + " " + "Have you gotten dressed?")
elif session.attributes["State"] == "Question 4 Answered":
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Dressed"] = "Yes"
session.attributes["State"] = "Question 5 Answered"
return question(message + " " + "Have you gone outside at all today?")
elif session.attributes["State"] == "Question 5 Answered":
message = random.choice([
'Awesome.',
'Good to hear!',
'Wonderful!',
'Great!',
])
session.attributes["Outside"] = "Yes"
session.attributes["State"] = "Suggested"
response = evaluate_answers()
if response == "Good job doing all those things. When you're depressed, those little things can be the most difficult.":
suggestion_inquiry = "Let's try something else to improve your mood."
else:
suggestion_inquiry = "Here's an idea for an extra way to improve your mood."
idea = ideas()
session.attributes["State"] = "AnythingElse"
return question(
message + " " + suggestion_inquiry + " " + idea + " " + "I hope I could help. Is there anything else I can do?")
elif session.attributes["State"] == "Suggested":
message = "Okay, here's another idea. "
idea = ideas()
session.attributes["State"] = "Suggested"
return question(
message + " " + idea + " " + "Would you like another suggestion?")
elif session.attributes["State"] == "AnythingElse":
return question("Okay, I love to help. What can I do for you? Say help if you would like to learn about my other capabilities.")
else:
return question("I'm sorry, I didn't get that. How are you feeling? ")
except:
return question("I'm sorry, I didn't get that. How are you feeling? ")
""" The following functions handle the additional use cases of recommending a therapist, detecting and preventing suicide, and
giving ideas on how to improve the user's mood."""
@ask.intent('SuggestIdea')
def suggest_ideas():
suggestion_inquiry = "Okay. Here's an idea for an extra way to improve your mood."
idea = ideas()
session.attributes["State"] = "Suggested"
return question(suggestion_inquiry + " " + idea + " " + "Would you like another suggestion?")
@ask.intent('HotLine')
def hot_line():
sms.sms_emergency("8709519238")
return statement("""Please don't hurt yourself or anyone else. I may just be a robot, but I
was created by a person who wants to help you and thinks you are worth it. Please call the National Suicide
Prevention Hotline at 1-800-273-8255. They are available to talk to you 24 hours a day, 7 days a week.
I've placed their number on a card in your Alexa app for reference.""") \
.standard_card(title='National Suicide Prevention Hot Line', text='Call Now 1-800-273-8255 ', large_image_url='https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Lifelinelogo.svg/1200px-Lifelinelogo.svg.png' )
@ask.intent('FindTherapist')
def find_therapist():
"""This function uses the Google Places API to recommend a therapist based on the user's location. """
keyword = "counseling OR therapist OR psychiatrist"
try:
address = get_alexa_location()
logging.debug(address)
pass
except:
logging.error("COULD NOT GET ALEXA LOCATION")
logging.debug(traceback.format_exc())
return statement("""Hmm. It appears that I can't find your location. Please allow access to your
location in the Alexa app and try again """).consent_card("read::alexa:device:all:address")
try:
gcodeurl = 'https://maps.googleapis.com/maps/api/geocode/json'
params = {'sensor': 'false', 'address': address}
gc = requests.get(gcodeurl, params=params, verify=False)
results = gc.json()['results']
location = results[0]['geometry']['location']
location = "{},{}".format(location['lat'], location['lng'])
except:
logging.error('ERROR using google geocoder')
logging.debug(gc.json())
return statement("Sorry, I'm having trouble doing that right now. Please try again later.")
print(location)
key = os.environ['AIzaSyAj7uvndRvjAXgXsuRnhFoACl1_0t-sam4']
URL2 = "https://maps.googleapis.com/maps/api/place/textsearch/json?location={}&query={}&key={}".format(location,keyword,key)
print(URL2)
r2 = requests.get(URL2, verify=False)
if r2.status_code == 200:
first_output = r2.json()
else:
return "Sorry, I'm having trouble doing that right now. Please try again later."
results = first_output['results']
idnum = (results[1]['place_id'])
name = (results[1]['name'])
# print(results[1])
# print(idnum)
URL3 = "https://maps.googleapis.com/maps/api/place/details/json?placeid={}&key={}".format(idnum, key)
r3 = requests.get(URL3, verify=False)
if r3.status_code == 200:
second_output = r3.json()
phone = (second_output['result'])['international_phone_number']
# print(second_output)
# print(phone)
session.attributes["State"] = "Null"
message = """I've found a therapist near you.
Their name is: {}, and their number is: {}. I've added their contact info to a card in the Alexa app.
Is there anything else I can do?""".format(name,phone)
card = "Name:{} \n Phone:{}".format(name,phone)
return question(message).standard_card(title="I've found you a possible therapist",
text=card,
large_image_url="https://images.unsplash.com/photo-1489533119213-66a5cd877091?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=7c006c52fd09caf4e97536de8fcf5067&auto=format&fit=crop&w=1051&q=80")
else:
return statement("Sorry, I'm having trouble doing that right now. Please try again later.")
@ask.intent('AMAZON.StopIntent')
def handle_stop():
"""
This handles the 'stop' built-in intention.
"""
farewell_text = "Have a good day. I hope to hear from you soon."
return statement(farewell_text)
@ask.intent('AMAZON.CancelIntent')
def handle_cancel():
"""
This handles the 'cancel' built-in intention.
"""
farewell_text = "Goodbye"
return statement(farewell_text)
@ask.intent('AMAZON.HelpIntent')
def handle_help():
"""
This handles the 'help' built-in intention.
"""
help_text = """There's a few things I can do to help. I can recommend a therapist or offer a suggestion for
a way to improve your mood. I also possess the capability to detect suicidal intentions,
but I really hope that you won't need me to do that. """
return question(help_text)
if __name__ == '__main__':
app.run(debug=True, port=5000)