public
Created

  • Download Gist
gistfile1.txt
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#!/usr/bin/env node
 
// Basic XMPP bot using node.js for NoTube NScreen / Buttons sharing
//
// Dan Brickley <danbri@danbri.org> w/ a load of help from Libby Miller as usual
//
// To use, check config is OK and password is accessible, then `node notube_bot.js`
// See NScreen Web UI at http://nscreen.notu.be/testing/ or connect via PSI desktop app for debugging.
//
// This is a Javascript utility written for Node.js, that connects to NoTube's NScreen MUC (multi-user chat)
// service. It shows up there alongside human participants, and can listen to group and individual messages.
// If you drag an item to it, it can study that item and send back a response, individually or to group.
//
// Initial behaviour is a simple echo; items sent will be returned to the entire group.
// Other ideas:
// * more-like-this (using Mahout's similarity measures).
// * a bookwork persona; send back links to books
// * DVD Extras: send back other info, eg. youtube video interview with cast or director or writer
// * Invent your own, customise and run your own bot.
//
// Questions: can we have custom icons? what variety of item types are understood in NScreen UI? Channels vs shows?
 
 
// based on https://raw.github.com/gist/940969/413b5601292df26e06f325cbd6e3944a728fb590/hipchat_bot.js
// also for exec'ing, npm install node-ffi might be useful (to query SQL etc on commandline?)
// http://stackoverflow.com/questions/4443597/node-js-execute-system-command-synchronously
 
 
var request = require('request'); // github.com/mikeal/request
var sys = require('sys');
var util = require('util');
var xmpp = require('node-xmpp');
 
// shell hacks
var FFI = require("node-ffi");
var libc = new FFI.Library(null, {
"system": ["int32", ["string"]]
});
var run = libc.system;
 
 
// Config
//var jid = "bob.notube@gmail.com"
var jid = "bob@jabber.notu.be"
var password = process.env.NOTUBEPASS // or however...
 
//var room_jid = "default_muc11@conference.jabber.notu.be"
var room_jid = "default_muc11@conference.jabber.notu.be"
var room_nick = "foafbot"
 
var hello = '{"name":"foafbot","obj_type":"person","id":"foafbot","suggestions":[],"shared":[],"history":[]}';
 
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' })
);
 
var hello = "{\"name\":\"" + room_nick + "\",\"obj_type\":\"person\",\"id\":\"" + room_nick + "\",\"suggestions\":[],\"shared\":[],\"history\":[]}";
var msg = { to: room_jid, type: 'groupchat' };
cl.send(new xmpp.Element('message', msg ).c('body').t( hello ));
 
// 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();
// e.g.
// {"id":"b017z1kn","pid":"b017z1kn","video":"undefined","image":"http://www.bbc.co.uk/iplayer/images/episode/b017z1kn_303_170.jpg","title":"Top Gear USA: Episode 9","description":"The guys set out to answer the question of which is America's toughest truck in Alaska.","explanation":"Recommended because you watched Top Gear USA which also has Tanner Foust in it"}
if (message.indexOf('video') ) {
console.log("Got a video. From "+stanza.attrs.from);
console.log("Incoming: "+message);
var msg = { to: room_jid, type: 'groupchat' };
cl.send(new xmpp.Element('message', msg ).c('body').t( message ));
 
}
}
);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.