From 24c9363f0c3f5573074756606e7e1529bba99d14 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Thu, 19 Apr 2018 22:03:49 +0100 Subject: [PATCH] Add Slack support --- app.js | 3 +++ docker-compose.yml | 3 +-- public/src/views/index.vue | 21 +++++++++------ routes/slack.js | 54 ++++++++++++++++++++++++++++++++++++++ views/index.html | 1 + 5 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 routes/slack.js diff --git a/app.js b/app.js index 9718aa0..32ec692 100644 --- a/app.js +++ b/app.js @@ -16,6 +16,7 @@ import index from './routes/index'; import recent from './routes/recent'; import search from './routes/search'; import share from './routes/share'; +import slack from './routes/slack'; import errorHandler from './lib/error-handler'; const debug = debuglog('combine.fm'); @@ -57,6 +58,8 @@ app.use(route.get('/recent', recent)); app.use(route.post('/search', search)); app.use(route.get('/:service/:type/:id.:format?', share)); +app.use(route.post('/slack', slack)); + if (!module.parent) { app.listen(process.env.PORT || 3000, () => { debug(`Koa HTTP server listening on port ${(process.env.PORT || 3000)}`); diff --git a/docker-compose.yml b/docker-compose.yml index a63d1ca..578a8a7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,6 +20,7 @@ services: AWS_ACCESS_KEY_ID: AWS_SECRET_ACCESS_KEY: AWS_TAG: + SLACK_TOKEN: volumes: - ./:/app:cached - node_modules:/app/node_modules @@ -61,8 +62,6 @@ services: POSTGRES_DB: "combinefm" redis: image: "redis:4.0.2-alpine" - ports: - - "6379:6379" volumes: node_modules: diff --git a/public/src/views/index.vue b/public/src/views/index.vue index f04ad1e..b20a18d 100644 --- a/public/src/views/index.vue +++ b/public/src/views/index.vue @@ -38,14 +38,19 @@

Tools

-
-

- Download the Chrome Extension and get Combine.fm links right from your address bar. -

-

- Download the Chrome Extension -

-
+

+ Add to Slack +

+

+ Add to Slack and get links matched inline. +

+
+

+ Download the Chrome Extension +

+

+ Download the Chrome Extension and get Combine.fm links right from your address bar. +

diff --git a/routes/slack.js b/routes/slack.js new file mode 100644 index 0000000..41a07ea --- /dev/null +++ b/routes/slack.js @@ -0,0 +1,54 @@ +import kue from 'kue'; +import request from 'superagent'; +import debuglog from 'debug'; +import { inspect } from 'util'; + +import lookup from '../lib/lookup'; +import services from '../lib/services'; +import { find, create } from '../lib/share'; + +const debug = debuglog('combine.fm:slack'); + +const queue = kue.createQueue({ + redis: process.env.REDIS_URL, +}); + +const slackToken = process.env.SLACK_TOKEN; + +export default async function (ctx) { + if (ctx.request.body.challenge) { + ctx.body = ctx.request.body.challenge; + return; + } + + for (const link of ctx.request.body.event.links) { + if (link.domain === 'combine.fm') { + continue; + } + + const music = await lookup(link.url); + debug(music); + const payload = { + channel: ctx.request.body.event.channel, + attachments: [ + { + color: '#FE4365', + author_name: `${music.name} by ${music.artist.name}`, + author_link: `https://combine.fm/${music.service}/${music.type}/${music.id}`, + author_icon: music.artwork.small, + }, + ] + } + try { + const { body } = await request.post('https://slack.com/api/chat.postMessage') + .set('Authorization', `Bearer ${slackToken}`) + .send(payload); + debug(body) + } catch (err) { + debug(err); + } + } + + ctx.body = 'OK'; +} + diff --git a/views/index.html b/views/index.html index 8b64071..8fd34b9 100644 --- a/views/index.html +++ b/views/index.html @@ -12,6 +12,7 @@ +