最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

让Alexa技能通过HTTP工作正确地从JSON文件中说出来

IT培训 admin 6浏览 0评论

让Alexa技能通过HTTP工作正确地从JSON文件中说出来

如果我调用doHttp函数,它会在日志中获取数据而不会出现问题。我似乎无法让数据返回并被允许说出来。我正在使用nodejs使用visual studio代码。我对此很新,所以我知道我错过了一些东西。

const url = "";

const linkIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'linkIntent';
 },
handle(handlerInput) {

var data = doHttp();
var speechText = data;

return handlerInput.responseBuilder
  .speak(speechText)
  .withSimpleCard('Card title', speechText)
  .getResponse();
  },
};


function doHttp() {
  var data2 = '';
  https.get(url, (resp) => {
    let data = '';
    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });
    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      data2 = JSON.parse(data).title;
    });
  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });
  return data2;
 }


//Working function
function doHttp() {
  https.get(url, (resp) => {
    let data = '';
    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });
    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      console.log(JSON.parse(data).title);
    });
  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });
}
回答如下:

HTTP请求是一个异步函数,您的代码不会等待响应。您可以将http functon调用包含在promise中并返回它。然后可以在句柄输入函数中应用async / await。以下是使用weather API的示例代码。

const url =
  "https://samples.openweathermap/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";

const linkIntentHandler = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === "IntentRequest" &&
      handlerInput.requestEnvelope.request.intent.name === "linkIntent"
    );
  },
  async handle(handlerInput) {
    const data = await doHttp();
    console.log("data in handle input ", data);
    const speechText = data;
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .withSimpleCard(speechText, speechText)
      .getResponse();
  }
};

function doHttp() {
  var data2 = "";
  return new Promise((resolve, reject) => {
    https
      .get(url, resp => {
        let data = "";
        // A chunk of data has been recieved.
        resp.on("data", chunk => {
          data += chunk;
        });
        // The whole response has been received. Print out the result.
        resp.on("end", () => {
          console.log("data ", data);
          data2 = JSON.parse(data).weather[0].description;
          console.log("weather ", data2);
          resolve(data2);
        });
      })
      .on("error", err => {
        console.log("Error: " + err.message);
      });
  });
}

使用以下链接了解有关进行外部API调用的更多信息。 https://developer.amazon/blogs/alexa/post/4a46da08-d1b8-4d8e-9277-055307a9bf4a/alexa-skill-recipe-update-call-and-get-data-from-external-apis

让Alexa技能通过HTTP工作正确地从JSON文件中说出来

如果我调用doHttp函数,它会在日志中获取数据而不会出现问题。我似乎无法让数据返回并被允许说出来。我正在使用nodejs使用visual studio代码。我对此很新,所以我知道我错过了一些东西。

const url = "";

const linkIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'linkIntent';
 },
handle(handlerInput) {

var data = doHttp();
var speechText = data;

return handlerInput.responseBuilder
  .speak(speechText)
  .withSimpleCard('Card title', speechText)
  .getResponse();
  },
};


function doHttp() {
  var data2 = '';
  https.get(url, (resp) => {
    let data = '';
    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });
    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      data2 = JSON.parse(data).title;
    });
  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });
  return data2;
 }


//Working function
function doHttp() {
  https.get(url, (resp) => {
    let data = '';
    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });
    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      console.log(JSON.parse(data).title);
    });
  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });
}
回答如下:

HTTP请求是一个异步函数,您的代码不会等待响应。您可以将http functon调用包含在promise中并返回它。然后可以在句柄输入函数中应用async / await。以下是使用weather API的示例代码。

const url =
  "https://samples.openweathermap/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";

const linkIntentHandler = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === "IntentRequest" &&
      handlerInput.requestEnvelope.request.intent.name === "linkIntent"
    );
  },
  async handle(handlerInput) {
    const data = await doHttp();
    console.log("data in handle input ", data);
    const speechText = data;
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .withSimpleCard(speechText, speechText)
      .getResponse();
  }
};

function doHttp() {
  var data2 = "";
  return new Promise((resolve, reject) => {
    https
      .get(url, resp => {
        let data = "";
        // A chunk of data has been recieved.
        resp.on("data", chunk => {
          data += chunk;
        });
        // The whole response has been received. Print out the result.
        resp.on("end", () => {
          console.log("data ", data);
          data2 = JSON.parse(data).weather[0].description;
          console.log("weather ", data2);
          resolve(data2);
        });
      })
      .on("error", err => {
        console.log("Error: " + err.message);
      });
  });
}

使用以下链接了解有关进行外部API调用的更多信息。 https://developer.amazon/blogs/alexa/post/4a46da08-d1b8-4d8e-9277-055307a9bf4a/alexa-skill-recipe-update-call-and-get-data-from-external-apis

发布评论

评论列表 (0)

  1. 暂无评论