Alexa 技能错误

Alexa Skill error

我正在尝试在我的 Alexa Skill 中调用第 3 方 API,我在 CloudWatch 日志中收到 "Session ended with reason: ERROR"。问题似乎出在我的 NumberIntentHandler 或我的 httpGet 函数中,但我不确定在哪里。

更新代码

-- 被解雇的处理程序 --

  const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      var options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  const myResult = httpGet(options);
         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
  },
};

-- 从 Handler 调用的函数 --

  async function httpGet(options) {
  // return new pending promise
  console.log(`~~~~~~~~~ httpGet ~~~~~~~~~`);
  console.log(`~~~~~${JSON.stringify(options)}~~~~~`);
  return new Promise((resolve, reject) => {    

  const request = http.get(options, (response) => {
      // handle http errors
      if (response < 200 || response > 299) {
        reject(new Error('Failed to load page, status code: ' + response));
      }// temporary data holder
      const body = [];
      // on every content chunk, push it to the data array
      response.on('data', (chunk) => body.push(chunk));
      // we are done, resolve promise with those joined chunks
      response.on('end', () => resolve(body.join('')));
      console.log('body: ', body[0]);
    });
    // handle connection errors of the request
    request.on('error', (err) => reject(err));    
    request.end(); 
  });
}

更新代码 - 删除 async/await/promise

--处理程序--

const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      var options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  //const myResult = httpGet(options);
    const myResult = httpGet(options, res => {

         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
    });         
  },
};

-- 函数--

function httpGet(options, cb) {
  http.get(options, res => {
    console.log(`~~~~~${JSON.stringify(options)}~~~~~`);
    // simplified version without error handling
    let output = []; 
    res.on('data', d => output.push(d)); // or concat to a string instead?
    res.on('end', () => cb(output));
    console.log('output: ', output[0]);
  });
}

我相信您需要在 httpGet 中使用您的响应来调用 resolve。

作为旁注(与您的问题无关)- 我可以推荐使用请求承诺,它围绕 http 实现了一个非常好的承诺 api 并且在这种情况下会简化您的代码。 (我知道我知道,async/await 是新的有趣的工具,但在这种情况下我会选择 "simpler" :))。

此外,如果我没记错的话,http.get 的回调只用一个参数调用。

编辑,修改后:

你可以去掉 promise 和 async 来简化你的代码。 请注意 async/await - 如果 awaited 表达式不是一个 promise,那么它会自动转换为一个。在您当前的代码中,您需要像承诺一样使用它(例如链接 .then())或等待它。

无论如何,这是一个仅使用回调的示例:

function httpGet(options, cb) {
  http.get(options, res => {
    // simplified version without error handling
    let output = []; 
    res.on('data', d => output.push(d)); // or concat to a string instead?
    res.on('end', () => cb(output));
  });
}

httpGet(options, res => {
// building the alexa response, all your intent handler code that needs the response from your request
})