Created
July 6, 2011 01:15
-
-
Save mcroydon/1066321 to your computer and use it in GitHub Desktop.
An IRC bot for looking up APRS checkins using node.js and node-irc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var irc = require('irc'); | |
var http = require('http'); | |
var name = 'aprsbot'; | |
var channel = '#avgeek'; | |
var api_key = 'XXXXX'; | |
var client = new irc.Client('irc.freenode.net', name, {channels: [channel]}) | |
client.addListener('message', function(from, to, message) { | |
// Speak when spoken to | |
if (message.indexOf(name) === 0) { | |
var action = message.replace(name + ':', '').trim(); | |
// Map a name to a callsign, otherwise pass callsign through. | |
if (action === 'adam') { | |
action = 'kc0ylk'; | |
} | |
console.log('action: ' + action); | |
// Get info | |
var options = { | |
host: 'api.aprs.fi', | |
port: 80, | |
path: '/api/get?name=' + action + '&what=loc&apikey=' + api_key + '&format=json' | |
} | |
// Accumulate data | |
var data = ''; | |
http.get(options, function(res) { | |
res.on('data', function(chunk) { | |
data += chunk; | |
}).on('end', function(chunk) { | |
if (chunk !== undefined) { | |
data += chunk; | |
} | |
console.log(data); | |
var parsed = JSON.parse(data); | |
var entry = parsed['entries'][0]; | |
var time = new Date(entry.lasttime * 1000) | |
// Say response in channel | |
client.say(channel, entry.name + ': (' + entry.lat + ',' + entry.lng + ') at ' + time.toUTCString()); | |
client.say(channel, 'More info: http://aprs.fi/?call=' + action); | |
}).on('error', function(e) { | |
console.log('error: ' + e.message); | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment