Alexa-通过Lambda访问外部API

时间:2018-11-30 22:41:11

标签: javascript lambda alexa

我正在学习Alexa技能,需要它来查询API,但是它似乎根本不起作用,我尝试了100万种不同的方法。如果有人可以看看下面的代码并添加基本的API查询,那就太好了,谢谢!

const playersOnlineHandler = {
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        return (request.type === 'IntentRequest'
        && request.intent.name === 'playersOnlineIntent');
    },
    handle(handlerInput) {
        const data =https.get("URL");
        const x = "Hello";
        const speechOutput = "There is currently" + data + "players online";
        return handlerInput.responseBuilder
        .speak(speechOutput)
        .getResponse();
    },
};

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码进行HTTP GET API调用

StreamingRecognizeRequest

答案 1 :(得分:0)

您可以参考以下代码段。与https内置模块配合使用

handle(handlerInput) {

  https.get('https://jsonplaceholder.typicode.com/todos/1', res => {
    res.setEncoding("utf8");
    let body = "";

    res.on("data", data => {
        body += data;
    });
    //On receiving the entire info from the API
    res.on("end", () => {
        body = JSON.parse(body);

        speechOutput += 'Sample Info' + body.userId;

        return handlerInput.responseBuilder
              .speak(speechOutput)
              .getResponse();

    });
  });
},