从json和url检索数据

时间:2019-01-23 13:08:33

标签: javascript node.js discord.js

我有一个问题,当我输入!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');
  }
});

2 个答案:

答案 0 :(得分:2)

您可以使用npm中的request软件包。您可以使用以下命令进行安装:

npm package

要使用它,您首先需要它,然后只需将您要请求的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中的对象,则可以如上所示对其进行解析。

答案 1 :(得分:-1)

尝试安装和导入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');
}