大家好,晚上好!
我已经在我的Alexa应用程序中设置了一个API调用,我试图基本了解它为什么不能与我拥有的URL /响应一起使用。
我知道API调用是有效的,因为当我将'host'替换为'api.icndb.com',并将'path'替换为'/ jokes / random'时,它将起作用(当我使用response.value.quote访问响应数据时) )。
我的API调用无法使用我提供的URL,或者也许这是我尝试访问数据的方式。 该API在数组中提供的数据具有嵌套在其中的对象,该对象与上述URL不同。
要了解我所指的是什么,这是我使用“ api.icndb.com” API构建我的应用程序所用的示例Alexa技能的URL。
这是我的代码:
/* eslint-disable func-names */
/* eslint-disable no-console */
const Alexa = require('ask-sdk');
var https = require('https')
const LaunchRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest'
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('Welcome to Simpson Speak')
.getResponse();
}
};
const GetQuoteHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'GetQuote';
},
async handle(handlerInput) {
const response = await httpGet();
console.log(response);
return handlerInput.responseBuilder
.speak(response[0].author)
.getResponse()
}
};
function httpGet(){
return new Promise(((resolve, reject) => {
var options = {
host: 'thesimpsonsquoteapi.glitch.me',
port: 443,
path: '/quotes',
method: 'GET',
};
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();
}));
};
const skillBuilder = Alexa.SkillBuilders.standard();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestHandler,
GetQuoteHandler
)
.lambda();
答案 0 :(得分:1)
此代码将与您的httpGet函数一起使用。
const GetQuoteHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'GetQuote';
},
async handle(handlerInput) {
const {responseBuilder } = handlerInput;
const response = await httpGet();
console.log(response);
const items = response[0]
const item = items.quote
var speechText = "Your quote is" + JSON.stringify(item)
return responseBuilder
.speak(speechText)
.reprompt("don't even know that one")
.getResponse();
}
}