-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (43 loc) · 2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var config = require('./qrz.config.json');
var request = require('then-request');
var FormData = require('then-request').FormData;
var xmlParser = require('xml-js');
var rgx = /^(<@[A-Za-z0-9]+> )?callsign ([A-Za-z0-9]+)$/;
module.exports.PluginName = 'QRZ.com Callsign Lookup';
module.exports.CanHandleMessage = function(messageText){
return rgx.test(messageText.toLowerCase());
};
module.exports.HandleMessage = function(event, sendMsg){
let callsignToLookUp = event.text.toLowerCase().match(rgx)[2];
let data = new FormData();
data.append('username', config.username);
data.append('password', config.password);
request('POST', 'https://xmldata.qrz.com/xml/1.33/?agent=orionbot', {form: data}).getBody('utf8')
.then(function(body){
let authResponse = xmlParser.xml2js(body, {compact: true});
//todo ensure auth response wasn't an error
return authResponse.QRZDatabase.Session.Key._text;
})
.then(function(sessionKey){
return request('GET', 'http://xmldata.qrz.com/xml/1.33/?s=' + sessionKey + ';callsign=' + callsignToLookUp).getBody('utf8');
})
.then(function(body){
let callsignResponse = xmlParser.xml2js(body, {compact: true});
//todo ensure callsign response wasn't an error
let callsignObj = callsignResponse.QRZDatabase.Callsign;
let response = callsignObj.call._text + " (" + callsignObj.land._text + "): ";
response += ((!!callsignObj.fname? callsignObj.fname._text : "") + " " + callsignObj.name._text).trim() + "\n";
response += callsignObj.addr1._text + "\n"
response += callsignObj.addr2._text;
if(!!callsignObj.state)
response += ", " + callsignObj.state._text;
response += " " + callsignObj.zip._text + "\n";
response += callsignObj.country._text;
sendMsg(response, event.channel);
return true;
})
.catch(function(err){
console.log('An error occurred: ', err);
sendMsg('An error occurred.', event.channel);
});
};