当用户没有响应时每 10 秒提示一次,不确定我们是否可以这样做。
但我们可以实现是/否部分。一种方法是使用状态。在此示例中,我使用node-cache 模块进行状态管理。
考虑以下名为“ConfirmationQuestionIntent”的意图。它将状态设置为“确认名称”。
const ConfirmationQuestionIntentHandler = {
canHandle(handlerInput) {
return (
handlerInput.requestEnvelope.request.type === "IntentRequest" &&
handlerInput.requestEnvelope.request.intent.name === "ConfirmationQuestionIntent"
);
},
handle(handlerInput) {
const speechText = "Please confirm your name as 'John'.";
myCache.set('state','confirm-name');
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
现在,启用/添加两个内置意图,AMAZON.YesIntent 和 AMAZON.NoIntent。
考虑下面的 AMAZON.NoIntent,
在处理函数中。它检查是否有任何名为“confirm-name”的状态。如果它存在,它会回复“请确认您的名字是 'John'”。如果没有,则使用默认响应进行响应。
const NoBuiltInIntent = {
canHandle(handlerInput) {
return (
handlerInput.requestEnvelope.request.type === "IntentRequest" &&
handlerInput.requestEnvelope.request.intent.name === "AMAZON.NoIntent"
);
},
handle(handlerInput) {
const state = myCache.get("state");
let speechText = "I didn't get this!. Could you please rephrase.";
if(state === "confirm-name") speechText = "Please confirm your name as 'John'.";
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
考虑下面的 AMAZON.YesIntent,
在句柄函数中,它检查是否有任何名为“confirm-name”的状态。如果是,那么它会回复“感谢您的确认”并从缓存中删除状态。如果不是,那么它会要求用户重新措辞。
const YesBuiltInIntent = {
canHandle(handlerInput) {
return (
handlerInput.requestEnvelope.request.type === "IntentRequest" &&
handlerInput.requestEnvelope.request.intent.name === "AMAZON.YesIntent"
);
},
handle(handlerInput) {
const state = myCache.get("state");
let speechText = "I didn't get this!. Could you please rephrase.";
if(state === "confirm-name") speechText = "Thanks for the confirmation.";
myCache.del('state');
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
因此,您可以使用“状态”来识别用户响应的场景,然后提供正确的响应。