javascript - 永远不会触发 Alexa LaunchRequest
问题描述
我正在使用 Alexa 技能包的 ask-sdk-core。我有以下 LaunchRequestHandler 和 AudioIntentHandler。我遇到的问题是,当我通过说“Alexa,与 SkillName 交谈”或“Alexa,开始 SkillName”来触发技能时,请求以某种方式发送 AudioIntent 而不是 LaunchRequest 到 lambda 函数,因此,音频流开始播放. 我能够通过开发者控制台触发正常行为和错误行为。
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, 0, null, null)
.withShouldEndSession(true)
.getResponse();
}
};
音频意图处理程序由 ResumeIntentHandler 支持
const AudioResumeIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.ResumeIntent';
},
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, 0, null, 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, null, 2));
return handlerInput.responseBuilder
.speak('Something went wrong')
.getResponse();
}
return handlerInput.responseBuilder
.getResponse();
}
};
该技能按预期操作,例如 Aelxa,请向 SkillName 寻求帮助。我能够达到我所有的非音频意图。还可以帮助意图、取消、停止等按预期工作。为什么启动时会触发音频意图?
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,与技能名称交谈
- Alexa,开始技能名称
这就是问题所在。在德语中,只有“开始”又名。“开始”有效。“交谈”又名。“spreche mit”似乎在技能中随机触发了一个意图,而不是 LaunchRequest。
我用第三方技能对此进行了测试,并能够重现奇怪的行为。
=> 修复:在德语中,以“Alexa,starte Skillname”而不是“Alexa,spreche mit Skillname”开始技能。
推荐阅读
- java - Immortal Service
- php - XAMPP php executing both if and else parts
- sql - Select all rows that have at least a list of features with wildcard support
- html - html中心垂直对齐不起作用
- javascript - 木偶表演时间表?
- r - 数据框中两列的双重匹配
- python - QSortFilterProxyModel 按日期范围过滤
- java - JavaFX getText()、setText()、toString() 给出空指针
- android - 如何防止 Android CursorLoader 重新加载?
- sql-server - 将 SQL Server 数据库迁移到 Azure 时出错