-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to slack interface so it works around an intent and reac…
…t protocol.
- Loading branch information
Showing
3 changed files
with
62 additions
and
52 deletions.
There are no files selected for viewing
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
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. |
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
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); | ||
} | ||
|
||
}; |
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