Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created September 9, 2013 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilukasz/6497103 to your computer and use it in GitHub Desktop.
Save hilukasz/6497103 to your computer and use it in GitHub Desktop.
////////////////
// REQUIRES
////////////////
var Campfire = require("../lib/campfire").Campfire;
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
////////////////
// SETUP
////////////////
server.listen(1337);
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.use(express.static(__dirname + '/public'));
app.get("/", function(req, res){
res.render("page");
});
// catch all random errors and print them out
process.on('uncaughtException', function (err){
console.error(err);
});
var instance = new Campfire({
ssl : true,
token : "MYTOKEN", //I actually have the real token here
account : "MYACCOUNT" // changed this as well
});
////////////////
// SOCKETS
////////////////
io.sockets.on('connection', function (socket) {
socket.on('disconnect', function()
{
console.log("A client has disconnected.");
});
//////////////////
// EMPLOYEES
//////////////////
var lukasz = {name: "Lukasz", id: 1216806, status: ""};
var dana = {name: "Dana", id: 1054576, status: ""};
var users = [lukasz, dana];
function returnUsernameUsingID(userID){
for (var j=0; j<users.length; j++) {
if (users[j].id == userID) return users[j].name;
}
return -1;
}
////////////////
// RegExp
////////////////
function returnWordAfter(theSentence, theWord){
var TheRegEx = new RegExp(theWord+"\\s(\\w*)");
var matches = theSentence.match(TheRegEx, '');
return matches[1];
}
function wordIsInSentence(theWord, theSentence){
if (theSentence.indexOf(theWord) > -1) {
return true;
} else {
return false;
}
}
////////////////
// Campfire API
////////////////
instance.join(571821, function(error, room) {
room.listen(function(message) {
var username = returnUsernameUsingID(message.userId);
if (wordIsInSentence("wfh", message.body)) {
room.speak(username+" is working from home");
socket.emit('message', { message: username+",wfh" });
}else if (wordIsInSentence("back", message.body)) {
room.speak(username+" is back");
socket.emit('message', { message: username+",back" });
}else if (wordIsInSentence("out", message.body)) {
room.speak(username+" is out");
socket.emit('message', { message: username+",out" });
}else if (wordIsInSentence("help", message.body)) {
console.log(message);
room.speak("your user ID is: "+message.userId+"\n/wfh - working from home \n/back - in office and available \n/headsdown - in office but busy \n/sick - outsick \n/vacation - on vacation \n/out - out for the day");
}else if (wordIsInSentence("id", message.body)) {
console.log(message);
room.speak("your user ID is: "+message.userId);
}else {
console.log("Received unknown message:");
console.log(message);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment