简体   繁体   English

Alexa API调用未返回任何内容

[英]Alexa API call is not returning anything

Hello and good evening all! 您好,晚上好!

I have setup an API call in my Alexa app and I'm trying to get a basic understanding of why it isn't working with the URL/response I have. 我已经在我的Alexa应用程序中设置了一个API调用,我试图对为什么它不能与我的URL /响应一起工作有一个基本的了解。

I know the API call works because when I replace 'host' with 'api.icndb.com' and 'path' with '/jokes/random' it works (when I access the response data using response.value.quote). 我知道API调用是有效的,因为当我将'host'替换为'api.icndb.com',并将'path'替换为'/ jokes / random'时,它将起作用(当我使用response.value.quote访问响应数据时)。

My API call will not work with the URL I have provided or maybe perhaps it's the way I'm trying to access the data. 我的API调用无法与我提供的URL一起使用,或者也许这是我尝试访问数据的方式。 The API provides data in an array with objects nested inside of it which is different from the aforementioned URL. 该API在数组中提供的数据具有嵌套在其中的对象,该对象与上述URL不同。

To see what I'm referring to, here is the URL for the sample Alexa skill that I built my app from with the 'api.icndb.com' API I'm referring to. 要了解我所指的是什么,这是我使用“ api.icndb.com” API来构建我的应用程序的示例Alexa技能的URL。

Here is my code: 这是我的代码:

/* 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();

This code will work with your httpGet function. 此代码将与您的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();
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM