Skip to content

Instantly share code, notes, and snippets.

@gobengo
Created January 22, 2011 06:17
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gobengo/790919 to your computer and use it in GitHub Desktop.
Save gobengo/790919 to your computer and use it in GitHub Desktop.
node.js-driven Google Chat bot
var xmpp = require('node-xmpp'),
secret = require('./secret'),
util = require('util');
var creds = {
jid: 'bengoering@gmail.com',
password: secret.pw, //string
};
var GChat = function(creds) {
var self = this,
c = this.client = new xmpp.Client(creds);
c.on('online', function() {
c.send(new xmpp.Element('presence'));
});
c.on('stanza', function(stanza) {
//util.log('IN: '+stanza.name);
if (stanza.is('presence')) c.emit('presence', stanza);
else if (stanza.is('message')) c.emit('message', stanza);
});
c.on('presence', function(p) {
var show = p.getChild('show');
util.print('Friend: '+p.attrs.from);
if (show) util.print(' ('+show.getText()+')');
util.print('\n');
});
c.on('message', function(msg) {
var from = msg.attrs.from,
body = msg.getChild('body'),
text = body ? body.getText() : '';
util.print('\nNew message from: '+from+'\n');
util.print('\t'+text+'\n');
});
};
var gc = new GChat(creds);
@gobengo
Copy link
Author

gobengo commented Jan 22, 2011

Sample output

$) node gchat.js

Friend: r--ch-@gmail.com/------ (dnd)
Friend: je-a--g-r@gmail.com/------ (away)
Friend: ben@livefyre.com/benmbp

New message from: ben@livefyre.com/benmbp
    And it can receive messages too!

@gobengo
Copy link
Author

gobengo commented Jan 22, 2011

Oh yeah, and node-xmpp and its dependencies appear to require node.js v0.2.6 right now, which is the latest stable version.

Trying to make it work with v0.3 will only waste you hours of time. Trust me...

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