Skip to content

Instantly share code, notes, and snippets.

@gellerby
Created July 20, 2017 22:49
Show Gist options
  • Save gellerby/5af9a89e143e97358218cde5a957cc38 to your computer and use it in GitHub Desktop.
Save gellerby/5af9a89e143e97358218cde5a957cc38 to your computer and use it in GitHub Desktop.
Slack to Sonos Integration with Node.js
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const SONOS_IP = 'http://localhost:5005';
const SONOS_LOGO = 'https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2017-07-20/215502049474_61c514345eda4191188d_72.jpg';
const SLACK_API_URL = 'https://slack.com/api/chat.postMessage';
const TOKEN = 'xoxp-2161828497-14839469607-49280511392-a0596dcbd0';
const CHANNEL = 'C024RQCET'; // Big Sea Hangout
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const server = app.listen(3000, () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
function nowPlaying() {
const sonosOptions = {
method: 'GET',
url: SONOS_IP + '/zones'
};
request(sonosOptions, getSonosStatus);
}
function sendToSlack(data) {
var
currentTrack = data[0].coordinator.state.currentTrack.artist,
nextTrack = data[0].coordinator.state.nextTrack.artist;
currentTrack += ' - ' + data[0].coordinator.state.currentTrack.title;
nextTrack += ' - ' + data[0].coordinator.state.nextTrack.title;
var attachments = [{
title: "Now Playing in The Pit:",
text: currentTrack,
color: "#8AB4B8"
},{
title: "Up Next:",
text: nextTrack,
color: "#6eaaaf"
}];
var slackOptions = {
method: 'POST',
url: SLACK_API_URL,
qs: {
token: TOKEN,
channel: CHANNEL,
attachments: JSON.stringify(attachments),
icon_url: SONOS_LOGO
},
headers: {
authorization: TOKEN
}
};
request(slackOptions, function(error, response, body) {
if (error) {
throw new Error(error);
}
console.log(body);
});
}
function getSonosStatus(error, response, body) {
if (error) {
throw new Error(error);
}
var data = JSON.parse(body);
if (data[0].coordinator.state.playbackState === 'PLAYING') {
sendToSlack(data);
}
}
app.post('/nowplaying', (req, res) => {
let data = {
response_type: 'ephemeral', // hidden
text: 'Sonos song info requested'
};
res.json(data);
nowPlaying();
});
app.post('/playsonos', (req, res) => {
let playPayload = {
method: 'GET',
url: SONOS_IP + '/The Pit/play'
}
let data = {
response_type: 'in_channel', // public to the channel
text: 'Sonos resumed by ' + req.body.user_name
};
request(playPayload, function(error, resp, body) {
if (resp) {
nowPlaying();
}
});
res.json(data);
});
app.post('/pausesonos', (req, res) => {
let pausePayload = {
method: 'GET',
url: SONOS_IP + '/The Pit/pause'
}
let data = {
response_type: 'in_channel', // public to the channel
text: 'Sonos paused by ' + req.body.user_name
};
request(pausePayload, function(error, resp, body) {
if (resp) {
console.log('Response from SONOS: ' + JSON.stringify(resp));
}
});
res.json(data);
});
app.post('/next-track', (req, res) => {
let skipPayload = {
method: 'GET',
url: SONOS_IP + '/The Pit/next'
}
let data = {
response_type: 'in_channel', // public to the channel
text: 'Track skipped by ' + req.body.user_name
};
request(skipPayload, function(error, resp, body) {
if (resp) {
console.log('Response from SONOS: ' + JSON.stringify(resp));
}
nowPlaying();
});
res.json(data);
});
app.post('/prev-track', (req, res) => {
let backPayload = {
method: 'GET',
url: SONOS_IP + '/The Pit/previous'
}
let data = {
response_type: 'in_channel', // public to the channel
text: 'Previous track played by ' + req.body.user_name
};
request(backPayload, function(error, resp, body) {
if (resp) {
console.log('Response from SONOS: ' + JSON.stringify(resp));
}
});
res.json(data);
});
app.post('/shrekt', (req, res) => {
let backPayload = {
method: 'GET',
url: SONOS_IP + '/The Pit/playlist/Mouth Sounds'
}
let data = {
response_type: 'in_channel', // public to the channel
text: 'SOME SOME SOME SOME SOME',
attachments: [{
title: "The Pit just got SHREKT",
image_url: "https://media1.giphy.com/media/MDXomrcGshGso/giphy.gif"
}]
};
request(backPayload, function(error, resp, body) {
if (resp) {
console.log('Response from SONOS: ' + JSON.stringify(resp));
}
});
res.json(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment