如何在Alexa Skill的Fallback Intent中获取用户完整提问文本
获取Alexa Skill Fallback Intent中的完整用户提问文本
没问题!要在Fallback Intent里拿到用户的完整提问内容,其实不用纠结槽位提取——Alexa在发送给Skill的请求里,已经自带了用户的完整语音转写文本。核心就是用inputTranscript这个字段,不管触发的是哪个Intent(包括Fallback),它都会返回用户说的完整内容。
具体实现方法
1. Node.js SDK(v3版本)
在你的Fallback Intent处理器里,直接从请求信封中读取inputTranscript:
const FallbackIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent'; }, handle(handlerInput) { // 这里拿到完整的用户提问文本 const fullUserQuery = handlerInput.requestEnvelope.request.inputTranscript; // 你可以根据这个文本做后续处理,比如记录日志、个性化回复等 const speakOutput = `你刚才问了:${fullUserQuery},抱歉我暂时没法解答这个问题哦`; return handlerInput.responseBuilder .speak(speakOutput) .reprompt(speakOutput) .getResponse(); } };
2. Python SDK
用Python开发的话,逻辑完全一致,从请求信封的input_transcript属性获取:
from ask_sdk_core.dispatch_components import AbstractRequestHandler from ask_sdk_core.utils import is_intent_name, is_request_type class FallbackIntentHandler(AbstractRequestHandler): def can_handle(self, handler_input): return (is_request_type("IntentRequest")(handler_input) and is_intent_name("AMAZON.FallbackIntent")(handler_input)) def handle(self, handler_input): # 获取完整用户提问 full_user_query = handler_input.request_envelope.request.input_transcript speak_output = f"你刚才问的是:{full_user_query},抱歉我暂时没法回答这个问题" return handler_input.response_builder.speak(speak_output).ask(speak_output).response
几个注意点
- 这个字段不只是Fallback Intent能用,所有IntentRequest类型的请求里都有它——如果你在其他自定义Intent里也需要完整用户文本,同样可以这么获取。
- 默认情况下不需要额外配置权限,
inputTranscript是Alexa请求的标准字段,只要你的Skill能正常接收请求就能拿到。 - 不管用户是语音输入还是在Alexa App里打字输入,这个字段都会返回对应的完整文本内容。
内容的提问来源于stack exchange,提问作者NKS
相关产品推荐
相关产品推荐

