如何在Alexa技能中获取并使用Yes/No确认触发对应意图
Solution for Handling Yes/No Responses in Your Alexa Skill
Got it, let's get this sorted for you! The core issue here is making sure Alexa properly routes the user's "yes" or "no" response to the right intent after your launch prompt. Here's how to adjust your code and leverage Alexa's built-in tools to make this work smoothly:
Step-by-Step Implementation
First, we'll use Alexa's pre-built AMAZON.YesIntent and AMAZON.NoIntent—these are ready-to-use intents that handle basic yes/no responses without you having to create custom utterances. Here's the updated code:
var handlers = { 'LaunchRequest': function () { const welcomeMsg = this.t("WELCOME_MSG"); const prompt = this.t("ASK_FOR_SOMETHING"); // Should map to "Do you want to perform something ?" const reprompt = this.t("LAUNCH_REPROMPT"); // Example: "I didn't catch that. Do you want to perform something?" this.response.speak(`${welcomeMsg} ${prompt}`).listen(reprompt); this.emit(':responseReady'); }, // Handle user's "yes" response 'AMAZON.YesIntent': function () { // Trigger your custom intent directly this.emit('SomethingIntent'); }, 'SomethingIntent': function () { // Your logic for when the user agrees goes here this.response.speak("Perfect! Let's dive into that task you wanted."); this.emit(':responseReady'); }, // Handle user's "no" response 'AMAZON.NoIntent': function () { this.response.speak("No worries at all! Have a fantastic day."); this.emit(':responseReady'); }, // Gracefully handle unrecognized responses 'Unhandled': function () { const reprompt = this.t("LAUNCH_REPROMPT"); this.response.speak("Sorry, I didn't get that. " + reprompt).listen(reprompt); this.emit(':responseReady'); } };
Key Details to Note
- Built-in Intents: You don't need to create custom intents or utterances for yes/no—
AMAZON.YesIntentandAMAZON.NoIntentare enabled by default in most Alexa skill interaction models, but double-check your skill's console to confirm. - Intent Redirection: Using
this.emit('SomethingIntent')in the yes handler keeps your code clean by reusing your existing intent logic instead of duplicating code. - Reprompt & Fallback: The
Unhandledhandler ensures Alexa stays in the conversation if the user says something unclear, guiding them back to the yes/no prompt.
内容的提问来源于stack exchange,提问作者AkshayM
相关产品推荐
相关产品推荐

