-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
95 lines (79 loc) · 3.02 KB
/
lambda.py
File metadata and controls
95 lines (79 loc) · 3.02 KB
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
import json
import boto3
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Dataoria')
def lambda_handler(event, context):
if event['request']['type'] == "LaunchRequest":
return on_launch(event)
elif event['request']['type'] == "IntentRequest":
return on_intent(event)
def on_launch(event):
return get_welcome_response()
def on_intent(event):
intent_name = event['request']['intent']['name']
if intent_name == "AnswerIntent":
return handle_answer_intent(event)
else:
raise ValueError("Invalid intent")
def get_welcome_response():
session_attributes = {}
card_title = "Welcome"
speech_output = "Welcome to Dataoria. Let's start the quiz. Here is your first question."
reprompt_text = "Please answer the question."
should_end_session = False
first_question = get_question(1)
speech_output += " " + first_question
return build_response(session_attributes, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def handle_answer_intent(event):
session_attributes = event['session']['attributes']
user_answer = event['request']['intent']['slots']['Answer']['value']
question_number = session_attributes['question_number']
correct_answer = get_answer(question_number)
if user_answer.lower() == correct_answer.lower():
question_number += 1
next_question = get_question(question_number)
speech_output = "Correct! Here is your next question. " + next_question
session_attributes['question_number'] = question_number
should_end_session = False
else:
speech_output = "Sorry, that's incorrect. Game over."
should_end_session = True
return build_response(session_attributes, build_speechlet_response(
"Answer", speech_output, None, should_end_session))
def get_question(question_number):
response = table.query(
KeyConditionExpression=Key('S. No').eq(question_number)
)
return response['Items'][0]['Question']
def get_answer(question_number):
response = table.query(
KeyConditionExpression=Key('S. No').eq(question_number)
)
return response['Items'][0]['Answer']
def build_speechlet_response(title, output, reprompt_text, should_end_session):
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'card': {
'type': 'Simple',
'title': title,
'content': output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
def build_response(session_attributes, speechlet_response):
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
}