Alexa Flask问是/否回应处理 [英] Alexa Flask Ask Yes / No response handling
问题描述
我正在尝试使用python中的Flask Ask创建一个简单的Alexa技能。
I am trying to create a simple Alexa skill using Flask Ask in python.
我有一个名为 SearchIntent的意图,该意图带有搜索项插槽,而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
类似以下内容:
...
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
我不想在YesIntent中再次创建搜索功能,是否可以在同一函数中执行类似的操作?
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?
谢谢
推荐答案
这不能通过建议的方式使用flask ask来实现。调用 return
后,您将离开SearchIntent()函数,而无法检查答案或运行其他代码。
但是,您仍然可以使其起作用:在用户回答您的问题后,将发送新的意图,然后flask-ask调用相应的函数。通过使用@ user3872094建议的会话属性,您可以在此新函数中处理您的 searchterm
。会话属性用于在不同意图请求之间的会话期间保留用户输入。
请检查以下最小示例:
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.")
将Amazon Yes and No intent添加到您的intent_schema中:
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"
}
]
}
这篇关于Alexa Flask问是/否回应处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!