问题描述
我正在尝试使用 Python 中的 Flask Ask 创建一个简单的 Alexa 技能.
I am trying to create a simple Alexa skill using Flask Ask in python.
我有一个名为SearchIntent"的意图,带有一个searchterm"槽,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('
'.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 和 No 意图添加到您的 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 询问是/否响应处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!