Skip to content

Instantly share code, notes, and snippets.

@skipjac
Created January 14, 2011 23:27
Show Gist options
  • Save skipjac/780492 to your computer and use it in GitHub Desktop.
Save skipjac/780492 to your computer and use it in GitHub Desktop.
Shows the users in a hipchat room and the time of the last message
<div id='hipchat_widget'>
</div>
<div id="hipchat_message"></div>
<script type="javascript">
$j(document).ready(function() {
d = new Date();
if(d.getMonth()>9){
month = d.getMonth()+1;
}
else {
month = d.getMonth()+1;
month ="0"+month;
}
if(d.getDate()>9){
day = d.getDate();
}
else {
day = "0"+d.getDate();
}
hipChatURL = 'http://api.hipchat.com/v1/rooms/';
hipChatroomID = encodeURIComponent('room_id= 11093&'); //encodeURIComponent is needed for the ampersand
hipChatauth = 'auth_token=629b1ed13f7e5be1dfb4d4232b1d20';
hipChatDate = encodeURIComponent("date="+d.getFullYear()+"-"+month+"-"+day+"&");
hipChatTZ = encodeURIComponent("timezone=PST&");
$j.getJSON('/proxy/direct?url='+hipChatURL+"show?"+hipChatroomID+hipChatauth, function(data) {
lastActive = new Date(data.room['last_active']*1000);
roomTitle = "<div class=\"hipentry\"><h2 class=\"roomTitle\">Room Name: " + data.room['name'] + "<\/h2>";
roomTitle += "<em class=\"hiptopic\">Topic: " + data.room['topic'] + "</em>";
roomTitle += "<p>Last Activity: "+lastActive+"<p>";
roomTitle += "<h4>Users in the Room:</h4><ul>";
$j('#hipchat_widget').append(roomTitle);
var b = eval(data.room['participants']);
if(b.length === 0 ){
$j('#hipchat_widget').append("<li>there is no one online</li>");
}
$j.each(data.room.participants, function(key, value){
$j('#hipchat_widget').append("<li>"+value.name+"</li>");
});
$j('#hipchat_widget').append('</ul></div>');
});
$j.getJSON('/proxy/direct?url='+hipChatURL+"history?"+hipChatroomID+hipChatDate+hipChatTZ+hipChatauth, function(message){
roomMessage = "<div class=\"hipmessage\"><h2 class=\"roomTitle\">Room Messages:<\/h2>";
$j('#hipchat_message').append(roomMessage);
if(message.length === 0){ $j('#hipchat_message').append("no messages today"); }
else{
//put the date at the top of the message
$j('#hipchat_message').append(message.messages.first().date.toString().substring(0,10));
//reverse the array so the latest is at the top
var latestMessages = message.messages.reverse();
//only grab the latest 5 messages in a new object
var latestFiveMessages = $j.map(latestMessages, function(i, count) {
// removes all items > 5
if (count++ >= 5) {return null;}
return i;
});
//display the messages
$j.each(latestFiveMessages, function(key, value){
$j('#hipchat_message').append("<li>"+value.date.toString().substring(11,16)+" "+value.from.name+": "+value.message+"</li>");
});
}
$j('#hipchat_message').append('</ul></div>');
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment