从 json 和 url 检索数据
Retrieving data from json and a url
我有一个问题,当我输入 !test
时,我该如何取回特定数据并发送到我的频道?
通常,当我向 URL 发出请求时,我会收到以下响应:
http://192.168.1.12/JSON?request=getstatus&ref=4030
{"Name":"HomeSeer Devices","Version":"1.0","Devices":[{"ref":4030,"name":"ttt","location":"ttt","location2":"ttt","value":0,"status":"Off","device_type_string":"AC Input Device Unknown Sensor","last_change":"\/Date(1548247933316)\/","relationship":0,"hide_from_view":false,"associated_devices":[],"device_type":{"Device_API":4,"Device_API_Description":"Plug-In API","Device_Type":73,"Device_Type_Description":"Plug-In Type 73","Device_SubType":97,"Device_SubType_Description":"AC[16B5BB2-10]a\u0002y\u0002\u00020\u00020\u00020\u00020\u00020\u00020\u0002n\u00021\u00020"},"device_image":"","UserNote":"","UserAccess":"Any","status_image":"/images/HomeSeer/status/off.gif","voice_command":"tttt","misc":4864}]}
我希望机器人在每次执行 !test
命令时都回复该状态。
我该怎么做?
下一个问题:如何设置发送带value
参数的请求?
http://192.168.1.12/JSON?request=controldevicebyvalue&ref=4030&value=0
我希望如果我键入 !Device 0
,它会通过发出该请求将 value
设置为 0。
这就是我处理命令的方式:
client.on('message', message => {
// If the message is "ping"
if (message.content === '!ping') {
// Send "pong" to the same channel
message.channel.send('pong');
}
});
尝试安装并导入 opn 模块:
命令行:$ npm install opn
然后将其安装到您的代码中:const opn = require('opn')
然后是
if (message.content == "!Device 0") {
opn('http://192.168.1.12/JSON?request=controldevicebyvalue&ref=4030&value=0');
}
您可以使用 npm 中的 request
包。您可以使用以下命令安装它:
要使用它,您首先需要要求它,然后只需将您要请求的 URL 放入:结果将传递给回调:
const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
对于你的情况,我会这样做:
client.on('message', message => {
// Detect the command
if (message.content.startsWith('!status')) {
// Issue the request
request('http://192.168.1.12/JSON?request=getstatus&ref=4030', (error, response, body) => {
// If there has been an error, log it
if (error) console.error(error);
// Otherwise, you can reply with the JSON you got back
else message.channel.send("```json\n" + body + "\n```");
});
}
});
如果你想把那个 body
字符串变成一个对象,你需要 JSON.parse()
它。
request('http://192.168.1.12/JSON?request=getstatus&ref=4030', (error, response, body) => {
let object = JSON.parse(body);
// Once you have the object you can get all of its properties like you'd normally do
});
你的第二个问题可以用同样的方法解决:你只需要根据参数设置thonURL。
如果你还没有,你需要创建一个参数解析器:有很多方法可以做到这一点,我将只向你展示这个例子中最简单的一个:
client.on('message', message => {
let args = message.content.split(' '), // Get the arguments
command = args.shift(); // Let the first be the command
// If there's no first argument, reply with this message
if (!args[0]) return message.reply("Please enter a value.");
if (command == '!device') {
request('http://192.168.1.12/JSON?request=controldevicebyvalue&ref=4030&value=' + args[0], (error, response, body) => {
// If there has been an error, log it
if (error) console.error(error);
// Otherwise, you can reply with the JSON you got back
else message.channel.send("```json\n" + body + "\n```");
});
}
});
如果您出于某种原因需要 body
中的对象,您可以按上述方式解析它。
我有一个问题,当我输入 !test
时,我该如何取回特定数据并发送到我的频道?
通常,当我向 URL 发出请求时,我会收到以下响应:
http://192.168.1.12/JSON?request=getstatus&ref=4030
{"Name":"HomeSeer Devices","Version":"1.0","Devices":[{"ref":4030,"name":"ttt","location":"ttt","location2":"ttt","value":0,"status":"Off","device_type_string":"AC Input Device Unknown Sensor","last_change":"\/Date(1548247933316)\/","relationship":0,"hide_from_view":false,"associated_devices":[],"device_type":{"Device_API":4,"Device_API_Description":"Plug-In API","Device_Type":73,"Device_Type_Description":"Plug-In Type 73","Device_SubType":97,"Device_SubType_Description":"AC[16B5BB2-10]a\u0002y\u0002\u00020\u00020\u00020\u00020\u00020\u00020\u0002n\u00021\u00020"},"device_image":"","UserNote":"","UserAccess":"Any","status_image":"/images/HomeSeer/status/off.gif","voice_command":"tttt","misc":4864}]}
我希望机器人在每次执行 !test
命令时都回复该状态。
我该怎么做?
下一个问题:如何设置发送带value
参数的请求?
http://192.168.1.12/JSON?request=controldevicebyvalue&ref=4030&value=0
我希望如果我键入 !Device 0
,它会通过发出该请求将 value
设置为 0。
这就是我处理命令的方式:
client.on('message', message => {
// If the message is "ping"
if (message.content === '!ping') {
// Send "pong" to the same channel
message.channel.send('pong');
}
});
尝试安装并导入 opn 模块:
命令行:$ npm install opn
然后将其安装到您的代码中:const opn = require('opn')
然后是
if (message.content == "!Device 0") {
opn('http://192.168.1.12/JSON?request=controldevicebyvalue&ref=4030&value=0');
}
您可以使用 npm 中的 request
包。您可以使用以下命令安装它:
要使用它,您首先需要要求它,然后只需将您要请求的 URL 放入:结果将传递给回调:
const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
对于你的情况,我会这样做:
client.on('message', message => {
// Detect the command
if (message.content.startsWith('!status')) {
// Issue the request
request('http://192.168.1.12/JSON?request=getstatus&ref=4030', (error, response, body) => {
// If there has been an error, log it
if (error) console.error(error);
// Otherwise, you can reply with the JSON you got back
else message.channel.send("```json\n" + body + "\n```");
});
}
});
如果你想把那个 body
字符串变成一个对象,你需要 JSON.parse()
它。
request('http://192.168.1.12/JSON?request=getstatus&ref=4030', (error, response, body) => {
let object = JSON.parse(body);
// Once you have the object you can get all of its properties like you'd normally do
});
你的第二个问题可以用同样的方法解决:你只需要根据参数设置thonURL。
如果你还没有,你需要创建一个参数解析器:有很多方法可以做到这一点,我将只向你展示这个例子中最简单的一个:
client.on('message', message => {
let args = message.content.split(' '), // Get the arguments
command = args.shift(); // Let the first be the command
// If there's no first argument, reply with this message
if (!args[0]) return message.reply("Please enter a value.");
if (command == '!device') {
request('http://192.168.1.12/JSON?request=controldevicebyvalue&ref=4030&value=' + args[0], (error, response, body) => {
// If there has been an error, log it
if (error) console.error(error);
// Otherwise, you can reply with the JSON you got back
else message.channel.send("```json\n" + body + "\n```");
});
}
});
如果您出于某种原因需要 body
中的对象,您可以按上述方式解析它。