Skip to content

Commit

Permalink
Improvements to slack interface so it works around an intent and reac…
Browse files Browse the repository at this point in the history
…t protocol.
  • Loading branch information
leonaves committed Mar 19, 2016
1 parent 7a234e5 commit c3977db
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var spotiPi = require('./src/client.js');
var client = require('./src/client.js');
var slack = require('./src/slackInterface.js');
var http = require('./src/httpInterface.js');

http(spotiPi);
slack(spotiPi);
http(client);
slack(client);

//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.
106 changes: 58 additions & 48 deletions src/slackInterface.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,74 @@
'use strict';

var config = require('../config.json');
var token = config.slack_bot_API_key;
module.exports = function(client)
{
var config = require('../config.json');
var token = config.slack_bot_API_key;

var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var rtm = new RtmClient(token);
var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var rtm = new RtmClient(token);

var spotifyAPI = require('./spotifyAPI.js');
var spotify = new spotifyAPI('https://api.spotify.com/v1/');
var spotifyAPI = require('./spotifyAPI.js');
var spotify = new spotifyAPI('https://api.spotify.com/v1/');

rtm.start();

function userString(userID) {
return '<@' + userID + '>';
}

function stringContains(string, contains) {
return (string.indexOf(contains) > -1);
}

module.exports = function(spotiPiClient) {
rtm.start();

rtm.on(RTM_EVENTS.MESSAGE, function (message) {
let messageIntent = intent(message);

if (!message.hasOwnProperty('text')) return;

if(stringContains(message.text, userString(rtm.activeUserId))) {
var messageContent = message.text.toLowerCase();

console.log(messageContent);

var index;

if ((index = messageContent.indexOf(' add ')) > -1) {

var searchQuery = messageContent.substring(index + 5, messageContent.length);

spotify.search(searchQuery)
.then(tracks => {
return tracks.items[0];
})
.then(track => {
spotiPiClient.add(track);
return track;
})
.then(track => {
rtm.sendMessage("I've queued up " + track.name + " by " + track.artists[0].name, message.channel);
});
if (messageIntent.action === 'add track') {
react('I\'ve queued up %trackName by %trackArtist', addTrack)
} else if (messageIntent.action === 'default') {
react("Hey " + userString(message.user) + ", why don't you try asking me to play something?");
}
});

} else if(messageContent.indexOf(' night ') > -1) {
function addTrack(query) {
spotify.search(query)
.then(tracks => {
let track = tracks.items[0];
client.add(track);
rtm.sendMessage("I've queued up " + track.name + " by " + track.artists[0].name, message.channel);
});
}

function userString(userID) {
return '<@' + userID + '>';
}

function intent(message) {
if (!message.hasOwnProperty('text')) return false;

if(message.text.indexOf(userString(rtm.activeUserId)) > -1) {
let index,
data,
messageText = message.text.toLowerCase();

if ((index = messageText.indexOf(' add ')) > -1) {
data = messageText.substring(index + 5, messageText.length);
return { action: 'add track', data: data };
}

rtm.sendMessage("Night " + userString(message.user), message.channel);
return { action: 'default' };
} else {
return { action: false };
}
}

} else {
function react(response, action) {
let returnVal = action === undefined ? {} : action();
let responseParams = returnVal === null ? {} : returnVal;

rtm.sendMessage("Hey " + userString(message.user) + ", why don't you try asking me to play something?", message.channel);
if (typeof responseParams !== 'object') {
throw new Error('Action must return an object mapping response parameters to strings, a Promise that—when fulfilled–returns the same, or null.');
}

}
for (let param in responseParams) {
response = response.replace('%' + param, responseParams['param']);
}
});

rtm.sendMessage(response, message.channel);
}

};
2 changes: 1 addition & 1 deletion src/spotifyAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ SpotifyAPI.prototype.search = function(query) {
.catch(err => this.handleError(err));
};

module.exports = SpotifyAPI;
module.exports = SpotifyAPI;

0 comments on commit c3977db

Please sign in to comment.