Skip to content

Instantly share code, notes, and snippets.

@michael-pratt
Created August 1, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-pratt/db5b370f94f0247443a65852dd031ac3 to your computer and use it in GitHub Desktop.
Save michael-pratt/db5b370f94f0247443a65852dd031ac3 to your computer and use it in GitHub Desktop.
Custom slack command hosted on Heroku with Node.js
var unirest = require('unirest');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
// Replace these with your own tokens
var imgurClientId = 'XXX';
var mashapeKey = 'YYY';
var slackToken = 'ZZZ';
var helpText = "{ \"response_type\" : \"ephemeral\" , \"text\": \"*Meme Generator Help*```/meme [image name],[top text],[bottom text],[font size]```\nThe first step is type the command with no arguments. This will generate the list of known image names you can use to start building custom memes. Once you know an image name, you can add text and change font size as needed.\n\nExamples:\n```/meme Ron Burgundy,This goes on top,This goes on bottom```\n```/meme Steve Jobs,This goes on top,This goes on bottom,32```\n```/meme Hoody Cat,,Only bottom text```\n```/meme Scumbag Steve,Only top text```\n_Note: Our *imgur* account is limited to 500 requests per day, so please be courteous. Also, our *heroku* instance is free and sleeps after 30 minutes of inactivity, so if your request fails, just try again._\",\"mrkdwn\": true }";
//
// Make sure that this route, when combined with your full Heroku app domain URL, matches
// the URL you provided in the Slack custom command setup.
//
app.get('/commands/meme', function(request, response) {
console.log('ha-slack got meme request with parameters', request.query);
//
// Validate this is a request with the proper Slack token
//
if(request.query.token != slackToken)
{
response.send("Invalid token");
}
//
// Parse the command text:
//
// - empty line means they want the list of image names
// - the word "help" generates a help message with syntax
// - anything else we split by commas and call meme generator
//
if(request.query.text.trim().length == 0)
{
//
// Call the GET images endpoint and format the response as a Slack
// message.
//
unirest.get("https://ronreiter-meme-generator.p.mashape.com/images")
.header("X-Mashape-Key", mashapeKey)
.header("Accept", "text/plain")
.end(function (result) {
response.type("json");
return response.send("{ \"response_type\" : \"ephemeral\" , \"text\":\"" + result.body.join('\n') + "\"}");
});
}
else if(request.query.text.trim() == "help")
{
response.type("json");
return response.send(helpText);
}
else
{
//
// We're only sure of having the imageName here, all others need reasonable
// defaults since we don't require them.
//
var textParts = request.query.text.trim().split(',');
var imageName = textParts.shift();
var topText = textParts.shift() || "";
var botText = textParts.shift() || "";
var fontSize = textParts.shift() || 40;
//
// Generate the raw meme image. This gives us back JPEG data.
//
var imgData = unirest.get("https://ronreiter-meme-generator.p.mashape.com/meme?meme=" + imageName + "&bottom=" + botText + "&font=Impact&font_size=" + fontSize + "&top=" + topText)
.header("X-Mashape-Key", mashapeKey)
.end(function (result) {
return result.body;
});
//
// Send the raw JPEG data to imgur to save as new image. The response
// object will contain a URL for accessing the image.
//
console.log('Calling imgur');
unirest.post("https://api.imgur.com/3/image")
.header("Authorization", "Client-ID " + imgurClientId)
.header("Content-Type", "image/jpeg")
.send(imgData)
.end(function (result) {
console.log('Result from imgur', result.status, result.headers, result.body);
var link = result.body.data.link;
console.log(link);
response.type("json");
//
// Send response to slack, attaching imgur file URL as an attachment.
//
return response.send("{ \"response_type\" : \"in_channel\" , \"text\":\"Meme Generator\",\"attachments\": [{ \"title\": \"" + link + "\", \"image_url\":\"" + link + "\"}]}");
});
}
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment