问题描述
我正在使用 Alexa 技能套件的 ask-sdk-core。我有以下 LaunchRequestHandler 和 AudioIntentHandler。我面临的问题是,当我通过说:“Alexa,与技能名称交谈”或“Alexa,启动技能名称”触发技能时,请求以某种方式向 lambda 函数发送 AudioIntent 而不是 LaunchRequest,因此,音频流开始播放.我能够通过开发者控制台触发正常和错误行为。
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},handle(handlerInput) {
const speakOutput = 'This message should be replied if the user launches the skill';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.addAudioPlayerStopDirective()
.getResponse();
}
};
以及其他几个 IntentHandler。其中一个 IntentHandler 启动音频流。
const AudioIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AudioIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.NextIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.PrevIoUsIntent');
},handle(handlerInput) {
// this code should only be triggered if an utterance of the intent has been said and
// not on launch
const streamId = crypto.randomBytes(16).toString("hex");
return handlerInput.responseBuilder
.addAudioPlayerPlayDirective('REPLACE_ALL',mediaPath,streamId,null,null)
.withShouldEndSession(true)
.getResponse();
}
};
音频意图处理程序由 ResumeIntentHandler 支持
const AudioResumeIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.ResumeIntent';
},null)
.withShouldEndSession(true)
.getResponse();
}
};
以及以下 AudioPlayer 处理程序
const AudioRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope).includes('AudioPlayer.');
},handle(handlerInput) {
console.log(`AudioRequestHandler: ${handlerInput.requestEnvelope}`)
if(Alexa.getRequestType(handlerInput.requestEnvelope) === 'AudioPlayer.PlaybackFailed') {
console.log("Playback Failed : " + JSON.stringify(handlerInput.requestEnvelope.request.error,2));
return handlerInput.responseBuilder
.speak('Something went wrong')
.getResponse();
}
return handlerInput.responseBuilder
.getResponse();
}
};
该技能按预期发挥作用,例如一次性Aelxa,向技能名称寻求帮助。我能够达到我所有的非音频意图。还可以帮助意图、取消、停止等按预期工作。为什么音频 Intent 会在启动时触发?
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,AudioRequestHandler,AudioPauseIntentHandler,AudioResumeIntentHandler,normalReplyIntentHandler,AudioIntentHandler,HelpIntentHandler,CancelAndStopIntentHandler,FallbackIntentHandler,SessionEndedRequestHandler,IntentReflectorHandler)
.addErrorHandlers(
ErrorHandler)
.lambda();
我添加了更好的日志记录。错误行为似乎发生在用户第二次启动技能之后。第一次技能交互效果很好。之后,“start SkillName”触发“AudioIntentHandler”。请求如下所示:
type: IntentRequest
intent: {
name: AudioIntent
confirmationStatus: NONE
}
所以在我看来,我的 addRequestHandlers 不是这里的问题,因为它的 Alexa 发送了错误的意图。 AudioIntentHandler 根据错误请求正确触发。
错误的请求包含 AudioPlayer 信息。可能从最后一次(第一次交互)开始。我相信这可能是错误的来源?
AudioPlayer {
offsetInMilliseconds: 3239,playerActivity: STOPPED,}
如果用户启动技能并且 Alexa 检测到存在停止的音频,Alexa 是否会继续音频意图?我在暂停或停止时没有正确清除音频播放器吗?
const AudioPauseIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.PauseIntent';
},handle(handlerInput) {
console.log('AudioPauseIntentHandler')
const speakOutput = 'Audio stopped.';
return handlerInput.responseBuilder
.speak(speakOutput)
.addAudioPlayerStopDirective()
.addAudioPlayerClearQueueDirective('CLEAR_ALL')
.getResponse();
}
};
解决方法
我发现了这个问题。据我所知,它与我的 Skill.json 或 lambda 代码无关。这是 Alexa 的事情,我能够在其他 Alexa 技能中重现相同的奇怪行为。
我主要用德语进行测试。在英语中,我通常使用以下唤醒词来启动技能:
- Alexa,与 Skillname 交谈
- Alexa,开始技能名称
这就是问题所在。在德语中,只有“开始”。 “开始”工作。又名“交谈”。 “spreche mit”似乎在技能中随机触发一个意图,而不是 LaunchRequest。
我使用第三方技能对此进行了测试,并且能够重现这种奇怪的行为。
=> 修正:在德语中,以“Alexa,starte Skillname”而不是“Alexa,spreche mit Skillname”开始技能。