我正在开发Alexa技能(使用v2和javascript),并且试图对USDA API进行API GET
调用。
至此,我已经从here复制/粘贴了,并且我正在使用here的USDA API示例(有一些小的更改)。作为尝试使连接正常工作并返回任何证明有效的尝试。
我当前收到的错误是: Error handled: TypeError: Cannot read property 'generalSearchInput' of undefined at Object.handle (/var/task/index.js:39:32)
at process._tickCallback (internal/process/next_tick.js:68:7)
直接在错误发生之前,我得到了返回: USDA API中关于干酪的每个条目,即[ 控制台日志告诉我,我正在从呼叫中收到数据,因此我知道它正在工作。我的问题是:如何将 我正在寻找的 我收到的答复是:console.log(response)
中的[[WAY too too]]。我只想要第一个,甚至就是名字。speechOutput
设置为所需的特定信息,而不是返回的整个API对象?speechOutput
应该说:对不起,我无法按您的要求去做。请再试一次
index.jsconst Alexa = require('ask-sdk-core');
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
const speakOutput = 'Welcome to food points! What food would you like to know about?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const FoodPointsIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'FoodPointsIntent';
},
async handle(handlerInput) {
console.log("THIS.EVENT = " + JSON.stringify(this.event));
var speakOutput = 'Sorry, there was an error';
//var https = require('https');
const { requestEnvelope } = handlerInput;
const userInput = Alexa.getSlotValue(requestEnvelope, 'FoodQuery');
const response = await httpGet();
console.log(response);
/*
const food = userInput;
speakOutput = food;
*/
speakOutput = response.value.generalSearchInput;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
const TestIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'TestIntent';
},
handle(handlerInput) {
console.log("THIS.EVENT = " + JSON.stringify(this.event));
var speakOutput = 'Sorry, there was an error';
const { requestEnvelope } = handlerInput;
const userInput = Alexa.getSlotValue(requestEnvelope, 'TestQuery');
const food = userInput;
speakOutput = food;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
function httpGet() {
return new Promise(((resolve, reject) => {
var options = {
host: 'api.nal.usda.gov',
port: 443,
path: '/fdc/v1/foods/search?api_key=DEMO_KEY&query=Cheddar%20Cheese',
method: 'GET',
};
var https = require('https');
const request = https.request(options, (response) => {
response.setEncoding('utf8');
let returnData = '';
response.on('data', (chunk) => {
returnData += chunk;
});
response.on('end', () => {
resolve(JSON.parse(returnData));
});
response.on('error', (error) => {
reject(error);
});
});
request.end();
}));
}
/****************************REMEMBER TO UPDATE THIS*************************/
const HelpIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speakOutput = 'You can say hello to me! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
},
handle(handlerInput) {
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse();
}
};
// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
},
handle(handlerInput) {
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
const speakOutput = `You just triggered ${intentName}`;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.stack}`);
const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
FoodPointsIntentHandler,
TestIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();
我正在开发Alexa技能(使用v2和javascript),并且尝试对USDA API进行API GET调用。至此,我已经从此处复制/粘贴了,并且正在使用来自...