如果您在使用Alexa Skill开发过程中遇到了“403 Forbidden”错误,可能是由于使用了不正确的权限,在Alexa Developer Console中编辑您的技能并添加必要的权限。 另外,根据您使用的语言和框架不同,可能需要对代码进行相应的更改。以下是Node.js中使用渐进响应的示例代码:
const request = require('request');
const Alexa = require('ask-sdk-core');
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechOutput = 'Welcome to my skill!';
const repromptOutput = 'Please tell me how can I help you.';
const attributes = handlerInput.attributesManager.getSessionAttributes();
attributes.lastResponse = '';
return handlerInput.responseBuilder
.speak(speechOutput)
.reprompt(repromptOutput)
.getResponse();
}
};
const MyIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'MyIntent';
},
handle(handlerInput) {
const attributes = handlerInput.attributesManager.getSessionAttributes();
const firstResponse = 'This is my first response. ';
attributes.lastResponse = firstResponse;
handlerInput.responseBuilder
.speak(firstResponse)
.addDirective(buildProgressiveResponseDirective('Processing your request.', '3000'))
.getResponse();
// Call external API
// return response
const secondResponse = 'This is my second response. ';
attributes.lastResponse = secondResponse;
return handlerInput.responseBuilder
.speak(secondResponse)
.reprompt('Please tell me how can I help you with.')
.getResponse();
}
};
function buildProgressiveResponseDirective(directive, delayMs){
return {
"type": "Dialog.UpdateDynamicEntities",
"updateBehavior": "REPLACE",
"types": []
},
{
"type": "VoicePlayer.Speak",
"delayInMilliseconds": delayMs,
"text": directive
};
}
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestHandler,
MyIntentHandler
)
.withSkillId(YOUR_SKILL_ID)
.lambda();
通过使用上面提供的示例代码,您可以在Node.js中使用渐进响应,同时确保在Alexa Developer Console中正确设置了所需的权限。