├── Procfile ├── .gitignore ├── package.json ├── index.js └── README.md /Procfile: -------------------------------------------------------------------------------- 1 | web: node index.js 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | auth.md 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "slack-genius", 3 | "version": "1.0.0", 4 | "description": "_A Slackbot that searches genius.com_", 5 | "main": "index.js", 6 | "dependencies": { 7 | "body-parser": "^1.14.2", 8 | "ejs": "2.3.3", 9 | "express": "4.13.3", 10 | "request": "^2.67.0" 11 | }, 12 | "devDependencies": {}, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/mager/slack-genius.git" 19 | }, 20 | "author": "", 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/mager/slack-genius/issues" 24 | }, 25 | "homepage": "https://github.com/mager/slack-genius#readme" 26 | } 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var app = express(); 3 | var url = require('url'); 4 | var request = require('request'); 5 | 6 | var bodyParser = require('body-parser'); 7 | app.use(bodyParser.json()); 8 | app.use(bodyParser.urlencoded({ extended: true })); 9 | 10 | app.set('port', (process.env.PORT || 9001)); 11 | 12 | app.get('/', function(req, res){ 13 | res.send('It works!'); 14 | }); 15 | 16 | app.post('/post', function(req, res){ 17 | var parsed_url = url.format({ 18 | pathname: 'https://api.genius.com/search', 19 | query: { 20 | access_token: process.env.GENIUS_ACCESS, 21 | q: req.body.text 22 | } 23 | }); 24 | 25 | request(parsed_url, function (error, response, body) { 26 | if (!error && response.statusCode == 200) { 27 | var data = JSON.parse(body); 28 | var first_url = data.response.hits[0].result.url; 29 | 30 | var body = { 31 | response_type: "in_channel", 32 | text: first_url 33 | }; 34 | 35 | res.send(body); 36 | } 37 | }); 38 | }); 39 | 40 | app.listen(app.get('port'), function() { 41 | console.log('Node app is running on port', app.get('port')); 42 | }); 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slack Genius 2 | 3 | _A Slackbot that searches genius.com_. 4 | 5 | ![](https://cdn-images-1.medium.com/max/1200/1*dK--YUKXxeLgSuddFUlRWQ.png) 6 | 7 | This Slackbot that receives input through a [slash command](https://api.slack.com/slash-commands) and sends a request to an API. 8 | 9 | A detailed tutorial about how I built this is on Medium: [https://goo.gl/wDyayI](https://goo.gl/wDyayI) 10 | 11 | ## Installation 12 | 13 | Clone the repo and then install dependencies: 14 | 15 | git clone git@github.com:mager/slack-genius.git 16 | cd slack-genius 17 | npm i 18 | 19 | 20 | Setup the server (I used Heroku): 21 | 22 | heroku create my-slackbot 23 | 24 | 25 | Set Heroku environment variable for [Genius access token](https://genius.com/api-clients): 26 | 27 | heroku config:add GENIUS_ACCESS=[your Genius access token] 28 | git push heroku master 29 | 30 | 31 | Setup Slack slash command: 32 | 33 | * Goto `http://[your-slack-team].slack.com/apps/manage/custom-integrations` and add a slash command. 34 | * Fill in the fields: 35 | * _Command_: the name of your slash command (ex: `/genius`) 36 | * _URL_: The URL to request when the slash command is run (ex: `https://my-slackbot.herokuapp.com/post`) 37 | * _Method_: POST 38 | * _Customize Name_: The name of your Slackbot 39 | * _Customize Icon_: A custom icon or an emoji 40 | * _Autocomplete help text_: Helps users when they start typing `/` 41 | * _Descriptive Label_: Provides extra context 42 | 43 | 44 | ## Usage 45 | 46 | In Slack, send slash commands to /genius: 47 | 48 | /genius Kendrick Lamar Poetic Justice 49 | 50 | 51 | ## License 52 | 53 | The MIT License (MIT) 54 | Copyright (c) 2016 Andrew Mager 55 | 56 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 57 | 58 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 59 | 60 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 61 | --------------------------------------------------------------------------------