Alexa Flask Ask Yes / No response handling

Question:

I am trying to create a simple Alexa skill using Flask Ask in python.

I have an intent called "SearchIntent" with a "searchterm" slot and the python code looks something like this:

@ask.intent("SearchIntent")
def SearchIntent(searchterm):
    resList = []
    searchterm = searchterm.lower()

    for item in somelist:
        if item.find(searchterm) != -1:
            resList.append(item)
    
            
    return question("I Found " + str(len(resList)) + ", Do you want me to list them all?")

I want to check if the response from the user, if he says "Yes" than read all the results:

return statement('n'.join(resList))

and if the user says no, to perform some other action

something like:

...

    return question("I Found " + str(len(resList)) + ", Do you want me to list them all?")

if "return question" == "yes":
   do something
else:
   do something else

I don’t want to create the search function again in a YesIntent, Is it possible to do something like this within the same function?

Asked By: NightRanger

||

Answers:

This is not possible in the suggested way using flask ask. After you call return, you leave your SearchIntent() function and have no way to check the answer or run additional code.
However, you can still make it work: after the user answers your question a new intent is sent and flask-ask calls the according function. By using session attributes, as suggested by @user3872094, you can process your searchterm in this new function. Session attributes are used to preserve user input during a session between different intent requests.
Check this minimal example:

@ask.intent("SearchIntent")
def SearchIntent(searchterm):
    session.attributes['term'] = searchterm
    return question("I understood {}. Is that correct?".format(searchterm))

@ask.intent('AMAZON.YesIntent')
def yes_intent():
    term = session.attributes['term'] 
    return statement("Ok. So your word really was {}.".format(term))

@ask.intent('AMAZON.NoIntent')
def no_intent():    
    return statement("I am sorry I got it wrong.")

Add the Amazon Yes and No intents to your intent_schema:

{
    "intents": [
        {
        "intent": "SearchIntent",
        "slots": [{
            "name": "searchterm",
            "type": "AMAZON.LITERAL"
        }]
        },{
            "intent": "AMAZON.NoIntent"
        }, {
            "intent": "AMAZON.YesIntent"
        }      
    ]
}
Answered By: mc51