Created
January 22, 2014 13:58
-
-
Save thunder-spb/8559148 to your computer and use it in GitHub Desktop.
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
// Basic XMPP bot example for MUC using node.js | |
// To use: | |
// 1. Set config variables | |
// 2. Run `node xmpp_bot.js` | |
// 3. Send a message like "!weather 94085" in the room with the bot | |
var request = require('request'); // npm install request | |
var sys = require('sys'); | |
var util = require('util'); | |
var xmpp = require('node-xmpp'); // npm install node-xmpp | |
// Config | |
var jid = "bot@jabber.cz" | |
var password = "secret" | |
var xmpp_server = "talk.google.com" | |
var room_jid = "nette@conf.netlab.cz" | |
var room_nick = "Nella" | |
var room_passwd = "" | |
var cl = new xmpp.Client({ | |
jid: jid + '/bot', | |
password: password, | |
host: xmpp_server || null | |
}); | |
// Log all data received | |
//cl.on('data', function(d) { | |
// util.log("[data in] " + d); | |
//}); | |
// Once connected, set available presence and join room | |
cl.on('online', function() { | |
util.log("We're online!"); | |
// set ourselves as online | |
cl.send(new xmpp.Element('presence', { }). | |
c('show').t('chat') | |
); | |
// join room (and request no chat history) | |
cl.send(function() { | |
el = new xmpp.Element('presence', { to: room_jid+'/'+room_nick }); | |
x = el.c('x', { xmlns: 'http://jabber.org/protocol/muc' }); | |
x.c('history', { maxstanzas: 0, seconds: 1}); | |
if (room_passwd != "") { | |
x.c('password').t(room_passwd); | |
} | |
return x; | |
}()); | |
// send keepalive data or server will disconnect us after 150s of inactivity | |
setInterval(function() { | |
cl.send(' '); | |
}, 30000); | |
}); | |
cl.on('stanza', function(stanza) { | |
// always log error stanzas | |
if (stanza.attrs.type == 'error') { | |
util.log('[error] ' + stanza); | |
return; | |
} | |
// ignore everything that isn't a room message | |
if (!stanza.is('message') || !stanza.attrs.type == 'groupchat') { | |
return; | |
} | |
// ignore messages we sent | |
if (stanza.attrs.from == room_jid+'/'+room_nick) { | |
return; | |
} | |
var body = stanza.getChild('body'); | |
// message without body is probably a topic change | |
if (!body) { | |
return; | |
} | |
var message = body.getText(); | |
// Look for messages like "!weather 94085" | |
if (message.indexOf('!weather') !== -1) { | |
var search = message.substring(9); | |
util.log('Fetching weather for: "' + search + '"'); | |
// hit Yahoo API | |
var query = 'select item from weather.forecast where location = "'+search+'"'; | |
var uri = 'http://query.yahooapis.com/v1/public/yql?format=json&q='+encodeURIComponent(query); | |
request({'uri': uri}, function(error, response, body) { | |
body = JSON.parse(body); | |
var item = body.query.results.channel.item; | |
if (!item.condition) { | |
response = item.title; | |
} else { | |
response = item.title+': '+item.condition.temp+' degrees and '+item.condition.text; | |
} | |
// is private ? | |
var params = {}; | |
if (stanza.attrs.from.indexOf(room_jid) === 0) { | |
params.to = room_jid; | |
params.type = 'groupchat'; | |
// mentation ? | |
if (message.substring(0, room_nick.length) == room_nick) { | |
response = stanza.attrs.from.substring(room_jid.length + 1) + ': ' + response; | |
} | |
} else { | |
params.to = stanza.attrs.from; | |
params.type = 'chat'; | |
} | |
// send response | |
cl.send(new xmpp.Element('message', params). | |
c('body').t(response) | |
); | |
}); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment