Last active
August 4, 2016 17:25
-
-
Save cheeaun/5234591 to your computer and use it in GitHub Desktop.
Sonos-2-HipChat notification node.js script
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
npm install sonos | |
npm install git://github.com/balbeko/node-hipchat.git |
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
var config = { | |
apiKey: 'HIPCHAT API KEY', | |
ipAddress: 'IP ADDRESS TO SONOS', | |
roomID: 'ROOM ID IN HIPCHAT', | |
msgFrom: 'Sonos', | |
msgColor: 'purple', | |
pollInterval: 15000 | |
}; | |
var Sonos = require('sonos').Sonos; | |
var https = require('https'); | |
var HipChat = require('node-hipchat'); | |
var hipClient = new HipChat(config.apiKey); | |
// hipClient.listRooms(function(rooms){ | |
// console.dir(rooms) | |
// }); | |
console.log('Connecting Sonos...'); | |
var sonos = new Sonos(config.ipAddress); | |
var lastTitle = null; | |
setInterval(function(){ | |
console.log('Polling...'); | |
sonos.currentTrack(function(e, track){ | |
if (e){ | |
console.error(e); | |
return; | |
} | |
if (!track){ | |
console.error(track); | |
return; | |
}; | |
var title = track.title; | |
if (!title) return; | |
if (title == lastTitle) return; | |
lastTitle = title; | |
var item = track._item; | |
var path = ''; | |
if (item){ | |
console.log(item); | |
try { | |
path = decodeURIComponent(item.res[0]['_']); | |
} catch (e){} | |
} | |
var message = '♫ ' + track.title + (track.artist ? (' – ' + track.artist) : ''); | |
hipClient.postMessage({ | |
room_id: config.roomID, | |
from: config.msgFrom, | |
message: message, | |
color: config.msgColor | |
}); | |
}); | |
}, config.pollInterval); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
are there any instructions for how to install/run this?