如何在Alexa与Dialogflow同一意图中获取多组动态及未知文本
Hey there! I’ve dealt with similar setup challenges when building voice experiences with Alexa and Dialogflow, so let’s break this down step by step for your two questions:
The key here is tracking conversation state so your assistant knows what information it still needs to gather. Here’s how to implement this for both platforms:
For Dialogflow
- Use Contexts to keep track of what input you’re expecting next. Create a context (e.g.,
awaiting_content_details) with a lifespan of 2-3 rounds (enough to cover the user’s response). - Optimize Training Phrases to include both single and multi-input scenarios, like:
- "I want to add content: title is [title], description is [description]"
- "First the title: [title], then the description is [description]"
- "Title is [title], I’ll tell you the description later"
- Build Fulfillment Logic to check which fields are missing, prompt the user, and retain the context. Example Node.js code:
function handleContentIntent(agent) { const title = agent.parameters.title; const description = agent.parameters.description; const currentContext = agent.context.get('awaiting_content_details'); if (!title) { agent.context.set({ name: 'awaiting_content_details', lifespan: 2 }); agent.add("Could you tell me the title of your content?"); } else if (!description) { agent.context.set({ name: 'awaiting_content_details', lifespan: 2 }); agent.add(`Got it, title is ${title}. Now can you share the description?`); } else { agent.add(`Perfect! I've saved your content: Title - ${title}, Description - ${description}`); } }
- Don’t forget to add
awaiting_content_detailsas an Input Context for your intent, so Dialogflow routes follow-up responses back to this intent.
For Alexa
- Use Session Attributes to store information collected so far during the conversation.
- Implement Intent Handling that checks the session state to know what input to request next. Example Node.js code:
const ContentIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ContentIntent'; }, handle(handlerInput) { const sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); const userInput = handlerInput.requestEnvelope.request.intent.slots.FreeText.value; if (!sessionAttributes.title) { sessionAttributes.title = userInput; handlerInput.attributesManager.setSessionAttributes(sessionAttributes); return handlerInput.responseBuilder .speak(`Got the title: ${userInput}. Now please tell me the description.`) .reprompt("Could you share the description for your content?") .getResponse(); } else if (!sessionAttributes.description) { sessionAttributes.description = userInput; return handlerInput.responseBuilder .speak(`Great! I've recorded your content: Title is ${sessionAttributes.title}, Description is ${userInput}`) .getResponse(); } } };
- Use
AMAZON.SearchQueryas the slot type (equivalent to Dialogflow’s@sys.any) to capture arbitrary user text.
This relies on guiding the conversation and training your model to infer which input corresponds to which field:
Leverage Step-by-Step Prompting: Instead of letting the user ramble, explicitly ask for one field at a time. For example:
- First prompt: "What’s the title of your content?"
- After receiving the title: "Thanks! Now can you describe the content?"
This eliminates ambiguity because each user response is tied to a specific request.
Optimize Training Phrases for Ambiguous Inputs: Add examples where the user provides both fields without labels, like:
- "[title] [description]"
- "[title], it’s about [description]"
Dialogflow’s ML will learn to map the first part to the title slot and the rest to the description slot over time.
Use Dialogflow’s Auto Slot Filling: If you’ve defined
titleanddescriptionas separate slots (both using@sys.any), enable auto slot filling. Configure prompts for each missing slot, and Dialogflow will automatically ask for the missing information if the user only provides one field.Lightweight Semantic Checks (Optional): In fulfillment, you can add simple logic to guess which field the user provided (e.g., short text = title, longer text = description), but this is a fallback—context-guided prompting is much more reliable.
内容的提问来源于stack exchange,提问作者Ruby

