我正在尝试建立一种技能,该技能可以决定您当日是否应该穿短裤,但仍然要按照标题中所述获得错误。关于此的其他文章很多,但似乎没有一个有效的答案。
我尝试将运行时切换到节点8.10,再切换回10.X,但是似乎没有任何作用。
我知道API密钥是哈希。它不在我的实际代码中。
/* eslint-disable no-console */
const Alexa = require('ask-sdk-core');
const cookbook = require('./alexa-cookbook.js');
const request = require('request');
require('dotenv').config();
let apiKey = '###################';
let city = 'leeds'
let url = `http://api.openweathermap.org/data/2.5/forecast?q=${city}&units=metric&appid=${apiKey}`
//=========================================================================================================================================
//TODO: The items below this comment need your attention.
//=========================================================================================================================================
const SKILL_NAME = 'Should I Wear Shorts';
const HELP_MESSAGE = 'You can say should I wear shorts, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const FALLBACK_MESSAGE = 'The Should I Wear Shorts skill can\'t help you with that. What can I help you with?';
const FALLBACK_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';
const yesSpeech = ['Yes, you should definitely wear shorts!'];
const noSpeech = ['No, you should not wear shorts today!'];
//=========================================================================================================================================
//Editing anything below this line might break your skill.
//=========================================================================================================================================
function getAnswer() {
request(url, function (err, response, body) {
if (err){
console.log('error', err);
}
else{
console.log('body:', body);
const weather = JSON.parse(body);
}
});
if(weather.main.temp < 20){
return true;
}
else{
return false;
}
}
const GetAnswerHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'GetAnswerIntent');
},
handle(handlerInput) {
if(getAnswer()){
const speechOutput = yesSpeech[Math.floor(Math.random() * yesSpeech.length)];
}
else {
const speechOutput = noSpeech[Math.floor(Math.random() * noSpeech.length)];
}
return handlerInput.responseBuilder
.speak(speechOutput)
.getResponse();
},
};
const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(HELP_MESSAGE)
.reprompt(HELP_REPROMPT)
.getResponse();
},
};
const FallbackHandler = {
// 2018-May-01: AMAZON.FallbackIntent is only currently available in en-US locale.
// This handler will not be triggered except in that locale, so it can be
// safely deployed for any locale.
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.FallbackIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(FALLBACK_MESSAGE)
.reprompt(FALLBACK_REPROMPT)
.getResponse();
},
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(STOP_MESSAGE)
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('Sorry, an error occurred.')
.reprompt('Sorry, an error occurred.')
.getResponse();
},
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
GetAnswerHandler,
HelpHandler,
ExitHandler,
FallbackHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();
我希望输出为是或否,但是正如我已经说过的那样,只是说“所请求的技能的响应存在问题。”
JSON输入:
"version": "1.0",
"session": {
"new": false,
"sessionId": "amzn1.echo-api.session.eaf07559-9dca-44b4-bff0-a64aedde4f54",
"application": {
"applicationId": "amzn1.ask.skill.4609edff-de26-47de-b3bf-aa2f894b841d"
},
"user": {
"userId": "amzn1.ask.account.AEL3JJK2BVA6K3EVV2T7LBQJS4VROUS22B7P4BWODLXENSNJBCNIH62ECVZ7HSSECCEUAEJ6GYSMLTMBWWFG45C3LQCMNSSET4LCMD7C64SOSZZBNTG7AOZL27J2FFLOH7FPNDFCULOLST3QXOR2GGTC57ZP7A5K5KOIWCSRJY4L6AGSUKOSXEFHVWPAN4MIIAWAGVRPJYQWUNA"
}
},
"context": {
"System": {
"application": {
"applicationId": "amzn1.ask.skill.4609edff-de26-47de-b3bf-aa2f894b841d"
},
"user": {
"userId": "amzn1.ask.account.AEL3JJK2BVA6K3EVV2T7LBQJS4VROUS22B7P4BWODLXENSNJBCNIH62ECVZ7HSSECCEUAEJ6GYSMLTMBWWFG45C3LQCMNSSET4LCMD7C64SOSZZBNTG7AOZL27J2FFLOH7FPNDFCULOLST3QXOR2GGTC57ZP7A5K5KOIWCSRJY4L6AGSUKOSXEFHVWPAN4MIIAWAGVRPJYQWUNA"
},
"device": {
"deviceId": "amzn1.ask.device.AF65CFND4RS4FFXCS7WVYUH3KMTDFQY7BJMKJUCG6VESIFV6HMWPKAYDVYM6UNE3ZF3JN4DXLL4BU33T7DAYJB574KAJL6RXCYD62EMGFLLAQYHQ6KYIW2CIVNGY3MKV5IA64H6OYGZXSDID2AG2N36FEBPHNKKP65A3WIMXGSX3UPBESI4BM",
"supportedInterfaces": {}
},
"apiEndpoint": "https://api.eu.amazonalexa.com",
"apiAccessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjEifQ.eyJhdWQiOiJodHRwczovL2FwaS5hbWF6b25hbGV4YS5jb20iLCJpc3MiOiJBbGV4YVNraWxsS2l0Iiwic3ViIjoiYW16bjEuYXNrLnNraWxsLjQ2MDllZGZmLWRlMjYtNDdkZS1iM2JmLWFhMmY4OTRiODQxZCIsImV4cCI6MTU2NzA2MjE4MCwiaWF0IjoxNTY3MDYxODgwLCJuYmYiOjE1NjcwNjE4ODAsInByaXZhdGVDbGFpbXMiO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HWVNNTFRNQldXRkc0NUMzTFFDTU5TU0VUNExDTUQ3QzY0U09TWlpCTlRHN0FPWkwyN0oyRkZMT0g3RlBOREZDVUxPTFNUM1FYT1IyR0dUQzU3WlA3QTVLNUtPSVdDU1JKWTRMNkFHU1VLT1NYRUZIVldQQU40TUlJQVdBR1ZSUEpZUVdVTkEifX0.LknWDy50k4NtjIi_n0MxWbFEN-tYpLOOJlj3-NTV-wNch5qXU_P9ZK0e85Az80pxWV9atVOphpHppVgYN9vmzMlyjZih-tBUDEznjhEjhUlL3Up5OyKHvnIz75EzJD7iRbeGcluSQI1CKiXhWm-_RUz8aTpYkU9kOy2Ensk6m0U0IY3uWLvd7PBAfSHIzZi3wpXJVncmvGBS8q_wQRgxkmFVAXEq5Mw2nFzl1tj0pJ33tyW_w6y93Xk62UrtNAue3c1299VLpnplrDmcPd82EpE-1bSQj1ZJzDEj_IaSmiwmn3CH9CUmh8rwomPYn6BvURma-zckhiW94ZI_0XeXNw"
},
"Viewport": {
"experiences": [
{
"arcMinuteWidth": 246,
"arcMinuteHeight": 144,
"canRotate": false,
"canResize": false
}
],
"shape": "RECTANGLE",
"pixelWidth": 1024,
"pixelHeight": 600,
"dpi": 160,
"currentPixelWidth": 1024,
"currentPixelHeight": 600,
"touch": [
"SINGLE"
],
"video": {
"codecs": [
"H_264_42",
"H_264_41"
]
}
}
},
"request": {
"type": "SessionEndedRequest",
"requestId": "amzn1.echo-api.request.c36a55a7-cf3f-4840-ad96-85f1779fb4fc",
"timestamp": "2019-08-29T06:58:01Z",
"locale": "en-GB",
"reason": "ERROR",
"error": {
"type": "INVALID_RESPONSE",
"message": "An exception occurred while dispatching the request to the skill."
}
}
}
JSON输出为空。