Skip to content

Commit

Permalink
SpotiPi now communicates over slack. HTTP interface started.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonaves committed Mar 16, 2016
1 parent ababbb5 commit 4f5fafc
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
config.json
3 changes: 3 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"slack_bot_API_key": "your_key_here"
}
29 changes: 5 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
var io = require('socket.io')(3000);
var stdin = process.openStdin();
var spotiPi = require('./src/client.js');
var slack = require('./src/slackInterface.js');

process.stdout.write("play/pause/skip/<track uri>: ");
slack(spotiPi);

stdin.addListener("data", function(d) {
var command = d.toString().trim();

switch(command) {
case 'play':
io.emit('play');
break;
case 'pause':
io.emit('pause');
break;
case 'skip':
io.emit('skip');
break;
default:
io.emit('add track to queue', command);
}
process.stdout.write("play/pause/skip/<track uri>: ");
});

io.on('connection', function(socket) {
});
//TODO: have options for http interface, slack interface, etc, defined in config.
//Perhaps eventually move this out into plugins, but that's probably way down the line.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.1",
"description": "A server component to send SpotiPi events over a websocket connection.",
"dependencies": {
"@slack/client": "^2.2.1",
"express": "^4.13.4",
"request-promise": "^2.0.1",
"socket.io": "^1.4.5"
Expand Down
27 changes: 27 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var io = require('socket.io')(3000);
var request = require('request-promise');

io.on('connection', function() {
console.log('Client connected');
});

exports.searchAndAdd = function(searchQuery, callback) {

console.log(searchQuery);

request('https://api.spotify.com/v1/search?q=track:' + searchQuery + '&type=track&limit=1')
.then(function (response) {

var track = JSON.parse(response).tracks.items[0];
io.emit('add track to queue', track.uri);

callback({
name: track.name,
artist: track.artists[0].name
});
})
.catch(function (err) {
return "error: " + err;
});

};
16 changes: 16 additions & 0 deletions src/httpInterface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var express = require('express');

var app = express();
app.listen(4000);

app.get('/play_from_query', function (req, res) {

if (req.query.hasOwnProperty('q')) {
var searchQuery = req.query.q;
res.send('Searching for "' + searchQuery + '"...');
//TODO: Communicate with client (passed in?)
} else {
res.send('You must provide a query.');
}

});
32 changes: 32 additions & 0 deletions src/slackInterface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var config = require('../config.json');
var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var token = config.slack_bot_API_key;
var rtm = new RtmClient(token);

rtm.start();

module.exports = function(spotiPiClient) {

rtm.on(RTM_EVENTS.MESSAGE, function (message) {
if(message.text.indexOf('<@' + rtm.activeUserId + '>') > -1) {
var messageContent = message.text.toLowerCase();

console.log(messageContent);

var index;

if ((index = messageContent.indexOf('play ')) > -1) {
var searchQuery = messageContent.substring(index + 5, messageContent.length);
spotiPiClient.searchAndAdd(searchQuery, function (track) {
rtm.sendMessage('I\'ve queued up ' + track.name + ' by ' + track.artist, message.channel);
});
} else if(messageContent.indexOf('night') > -1) {
rtm.sendMessage('Night <@' + message.user + '>', message.channel);
} else {
rtm.sendMessage('Hey <@' + message.user + '>, why don\'t you try asking me to play something?', message.channel);
}
}
});

};
Empty file added src/spotifyAPI.js
Empty file.

0 comments on commit 4f5fafc

Please sign in to comment.