Skip to content

Commit

Permalink
sending slack notifications when radio creation and plaback completes
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwards, Chris authored and Edwards, Chris committed Aug 14, 2017
1 parent 9c985d7 commit 355dec7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
24 changes: 10 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const { parseFormString } = require('./util/parse');
const {
INVALID_TOKEN,
TYPE_PRIVATE,
slackResp
} = require('./slack');
const slack = require('./slack');
const {
convertLinkToUri,
extractFromUri
Expand All @@ -28,14 +24,12 @@ function handler(event, context, callback) {
response_url
} = parseFormString(body);

console.log('INCOMING SLACK MESSAGE', body);

if (token !== SLACK_TOKEN) {
callback(null,
slackResp(
INVALID_TOKEN,
slack.slackResp(
slack.INVALID_TOKEN,
401,
TYPE_PRIVATE
slack.TYPE_PRIVATE
)
);
} else {
Expand All @@ -48,12 +42,12 @@ function handler(event, context, callback) {
)
.catch((error) => {
callback(null,
slackResp(error.message)
slack.slackResp(error.message)
);
})
.then((trackInfo) => {
callback(null,
slackResp(radio.SLACK_PENDING_MESSAGE(trackInfo))
slack.slackResp(radio.SLACK_PENDING_MESSAGE(trackInfo))
);
radio
.playBasedOnTrack(
Expand All @@ -63,10 +57,12 @@ function handler(event, context, callback) {
SPOTIFY_LOCAL_URL
)
.then((msg) => {
console.log('Send Slack notification that this worked:', msg, response_url);
console.log('notify', response_url, msg);

slack.notify(response_url, msg);
})
.catch(({ message }) => {
console.log('Send Slack notification that this failed:', message, response_url);
slack.notify(response_url, `Error creating playlist: ${message}`)
});
});

Expand Down
16 changes: 15 additions & 1 deletion src/slack/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const request = require('request-promise-native');
const { response } = require('../util/lambda')

// response types
Expand All @@ -19,5 +20,18 @@ module.exports = {
TYPE_PUBLIC,
TYPE_PRIVATE,
INVALID_TOKEN,
slackResp
slackResp,
notify(uri, text) {
return request
.post({
uri,
headers: {
'content-type': 'application/json'
},
body: {
text
},
json: true
})
}
};
16 changes: 13 additions & 3 deletions test/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const sinon = require('sinon');
const dotenv = require('dotenv');
const { INVALID_TOKEN } = require('../src/slack');
const { handler } = require('../src');
const slack = require('../src/slack');
const radio = require('../src/spotify/radio');
const track = require('../src/spotify/track');

Expand Down Expand Up @@ -52,7 +53,8 @@ describe('The Index Lambda Handler', () => {
context('with an request event with a valid token', () => {

const spotifyTrack = 'spotify:track:2771LMNxwf62FTAdpJMQfM';
const slackBody = `text=${spotifyTrack}&token=foo_bar_baz`;
const notificationUri = encodeURIComponent('https://hooks.slack.com/commands/T4ZLYGVSN/227562856215/B4XvvRukWrmUzSJ0cMC0arpE');
const slackBody = `text=${spotifyTrack}&token=foo_bar_baz&response_url=${notificationUri}`;
const event = { body: slackBody };
const trackInfo = {
name: 'Bodak Yellow',
Expand All @@ -68,12 +70,19 @@ describe('The Index Lambda Handler', () => {

sinon
.stub(track, 'getTrackInfo')
.resolves(trackInfo)
.resolves(trackInfo);

// todo assert calls
sinon
.stub(slack, 'notify')
.resolves({});

});

afterEach(() => {
radio.playBasedOnTrack.restore();
track.getTrackInfo.restore();
slack.notify.restore();
});

it('sends a response body', (done) => {
Expand All @@ -96,7 +105,8 @@ describe('The Index Lambda Handler', () => {
.to.eq(respMsg);
done()
} catch (error) {
done(error);
done(error); console.log('Send Slack notification that this worked:', msg, response_url);

}
});
});
Expand Down

0 comments on commit 355dec7

Please sign in to comment.