Skip to content

Instantly share code, notes, and snippets.

@powdahound
Created April 25, 2011 18:35
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save powdahound/940969 to your computer and use it in GitHub Desktop.
Save powdahound/940969 to your computer and use it in GitHub Desktop.
Basic XMPP bot example for HipChat using node.js
// Basic XMPP bot example for HipChat using node.js
// To use:
// 1. Set config variables
// 2. Run `node hipchat_bot.js`
// 3. Send a message like "!weather 94085" in the room with the bot
var request = require('request'); // github.com/mikeal/request
var sys = require('sys');
var util = require('util');
var xmpp = require('node-xmpp');
// Config (get details from https://www.hipchat.com/account/xmpp)
var jid = "X_X@chat.hipchat.com"
var password = ""
var room_jid = "X_X@conf.hipchat.com"
var room_nick = "Xxx Xxx"
var cl = new xmpp.Client({
jid: jid + '/bot',
password: password
});
// 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', { type: 'available' }).
c('show').t('chat')
);
// join room (and request no chat history)
cl.send(new xmpp.Element('presence', { to: room_jid+'/'+room_nick }).
c('x', { xmlns: 'http://jabber.org/protocol/muc' })
);
// 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') === 0) {
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;
}
// send response
cl.send(new xmpp.Element('message', { to: room_jid+'/'+room_nick, type: 'groupchat' }).
c('body').t(response)
);
});
}
});
@powdahound
Copy link
Author

You'd have to add the password element to the room join presence as shown in the spec: http://xmpp.org/extensions/xep-0045.html#enter-pw

@Vrtak-CZ
Copy link

@zhangskills
Copy link

good!

@CWSpear
Copy link

CWSpear commented Oct 11, 2013

Lines #44-46 at the time of this comment:

  setInterval(function() {
    cl.send(' ');
  }, 30000);

crashes the app when it cl.send(' '); I ended up replacing it with cl.send(new xmpp.Message({}));, which still threw an error, but at least it was handleable.

@adnanmuhammad
Copy link

please share how to receive the message onto client/front end page (.ejs).
i can print the messages in console but not able to show messages on front end.

Please share code

@adnanmuhammad
Copy link

please share how to receive the message onto client/front end page (.ejs).
i can print the messages in console but not able to show messages on front end.

I just want to display the messages on front end via jquery. Please share code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment