Created
September 11, 2013 17:31
-
-
Save hilukasz/6526969 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
//////////////// | |
// REQUIRES | |
//////////////// | |
var Campfire = require("../lib/campfire").Campfire, | |
express = require("express"), | |
app = require('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.engine('jade', require('jade').__express); | |
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", | |
account : "MYACCOUNT" | |
}); | |
////////////////// | |
// 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"); | |
//var until = returnWordAfter(message.body, "wfh") | |
io.sockets.emit('message', { message: username+",wfh" }); | |
}else if (wordIsInSentence("back", message.body)) { | |
room.speak(username+" is back"); | |
io.sockets.emit('message', { message: username+",back" }); | |
}else if (wordIsInSentence("out", message.body)) { | |
room.speak(username+" is out"); | |
io.sockets.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