Alexa - 通过 Lambda 访问外部 API
Alexa - Accessing External API via Lambda
我正在制作 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();
},
};
您可以尝试使用此代码进行 HTTP GET API 调用
const playersOnlineHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest'
&& request.intent.name === 'playersOnlineIntent');
},
handle(handlerInput) {
let data;
const request = require("request");
let options = { method: 'GET',
url: 'http://exaple.com/api.php',
qs:
{ action: 'query' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
let json = body;
let obj = JSON.parse(json);
data = obj.element.value;
});
const x = "Hello";
const speechOutput = "There is currently" + data + "players online";
return handlerInput.responseBuilder
.speak(speechOutput)
.getResponse();
},
};
您可以参考以下片段。适用于 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();
});
});
},
我正在制作 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();
},
};
您可以尝试使用此代码进行 HTTP GET API 调用
const playersOnlineHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest'
&& request.intent.name === 'playersOnlineIntent');
},
handle(handlerInput) {
let data;
const request = require("request");
let options = { method: 'GET',
url: 'http://exaple.com/api.php',
qs:
{ action: 'query' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
let json = body;
let obj = JSON.parse(json);
data = obj.element.value;
});
const x = "Hello";
const speechOutput = "There is currently" + data + "players online";
return handlerInput.responseBuilder
.speak(speechOutput)
.getResponse();
},
};
您可以参考以下片段。适用于 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();
});
});
},