If you have a lot of questions and you want each of them answered, you can use sessionAttributes
variable. The session variable can retain it's value through out the course of the session. You can make a dictionary to be saved in your sessionAttributes
(It has to be a dictionary).
You can save something like this in your session variable.
sessionAttributes: {
"AllQuestions" : ["string", "string"],
"LastQuestionIndex" : integer
}
I'm assuming you're able to get the list of questions (using a GET request).
Step 1 : Make a slot
Answer
would be a slot which is going to store your answer.
Step 2 : Get your questions ready
When your intent has just started and you don't have anything in your sessionAttributes
(use a simple if-else) you'll have to make the GET request and gather all of your questions (maybe in a list or something).
You make your GET request and store all the questions in your sessionAttributes['AllQuestions']
. And set LastQuestionIndex = -1
.
Now the tricky part comes in. (I'm also assuming you're able to use Dialog.ElicitSlot
Directive).
Step 3 : Ask questions one-by-one.
Now you have a list of all the questions and you also have the index of last question that was asked. And now you just have to increment the index, get the Next Question, and use Dialog.ElicitSlot
Directive to ask this new question. And update the LastQuestionIndex
in your sessionAttributes.
Step 4 : Getting the answer
Before proceeding to next question, you'll also have to check if the slot Answer
has any value or not? If it does have a value (it is not "None"), then you can use the LastQuestionIndex
variable and store the answer for that particular question.
If you're looking for code, here you go:
# Line 1 - 22 should be in your intent function
sessionAttributes = dict()
if 'sessionAttributes' in event['session']:
sessionAttributes = event['session']['sessionAttributes']
if not sessionAttributes.has_key('AllQuestions') :
# Make the GET REQUEST
Questions = ["all", "of", "your", "Questions", "stored", "in", "a", "list"]
sessionAttributes['AllQuestions'] = Questions
sessionAttributes['LastQuestionIndex'] = -1
Answer = getSlotValue('Answer')
if Answer != None:
# AllAnswers is a dictionary which has key as the question number and value is the answer
# AllAnswers = {
# 0 : "Answer to your first question",
# 1 : "Answer to your second question"
# }
AllAnswers[sessionAttributes['LastQuestionIndex']] = Answer
return ansNextQuestion(sessionAttributes)
def askNextQuestion(sessionAttributes) :
Last = sessionAttributes['LastQuestionIndex']
sessionAttributes['LastQuestionIndex'] = Last + 1
if Last < len(sessionAttributes['AllQuestions']):
outputSpeech = "Your next question is " + sessionAttributes['AllQuestions'][Last + 1]
return {
"version": "1.0",
"sessionAttributes": sessionAttributes,
"response": {
"outputSpeech": {
"type": "PlainText",
"text": outputSpeech
},
"shouldEndSession": False,
"directives": [
{
"type": "Dialog.ElicitSlot",
"slotToElicit": "Question",
"updatedIntent": {
"name": "GetMovieDetails",
"confirmationStatus": "NONE",
"slots": {
"Answer": {
"name": "Answer",
"value": "NONE" # You can change the value of Answer to get new answer, this is allowed.
}
}
}
}
]
}
}
else :
# You are out of questions, now proceed to what you should do after getting all the answers.