-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
56 lines (43 loc) · 1.28 KB
/
lambda_function.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
import json
import random
import requests
""" --- Helpers to call jService (https://jservice.io/) --- """
def get_question(category):
id = {
'Dog' : 196,
'Food' : 1723,
'Geography' : 88
}
try:
payload = { 'id' : str(id[category]) }
url = 'https://jservice.io/api/category'
r = requests.get(url, params=payload)
questions = r.json()['clues']
q = random.choice(questions)
return q['question'] + ' → ' + q['answer']
except:
return 'Oops! Our question set is out of service. Please try it later.'
""" --- Main handler --- """
def lambda_handler(event, context):
intent_name = event['interpretations'][0]['intent']['name']
slots = event['interpretations'][0]['intent']['slots']
category = slots['CategoryName']['value']['interpretedValue']
message = get_question(category)
response = {
'sessionState' : {
'dialogAction' : {
'type' : 'Close'
},
'intent' : {
'name' : intent_name,
'state' : 'Fulfilled'
}
},
'messages': [
{
'contentType' : 'PlainText',
'content' : message
}
]
}
return response