Skip to content

Instantly share code, notes, and snippets.

@pathawks
Last active April 9, 2016 04:28
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 pathawks/d2023c99a9813f6907b5f0f0ccead8d4 to your computer and use it in GitHub Desktop.
Save pathawks/d2023c99a9813f6907b5f0f0ccead8d4 to your computer and use it in GitHub Desktop.
Slack Google slash command
const qs = require('querystring');
const https = require('https');
const url = require('url');
const googleLink = function (text) {
const query = encodeURIComponent(text).replace(/%20/g,'+');
return "<https://www.google.com/search?q=" + query + "|google.com/search?q=" + query + ">";
}
const qrCode = function (url) {
return {
"response_type": "ephemeral",
"attachments": [{
"fallback": "https://chart.googleapis.com/chart?chs=512x512&cht=qr&chl=" + url,
"text": "QR code linking to " + url,
"image_url": "https://chart.googleapis.com/chart?chs=512x512&cht=qr&chl=" + url,
"thumb_url": "https://chart.googleapis.com/chart?chs=256x256&cht=qr&chl=" + url
}]
}
}
const in_channel = function (text) {
return {
"response_type": "in_channel",
"text": text,
"unfurl_links": true
}
}
const not_in_channel = function (text) {
return {
"response_type": "ephemeral",
"text": text,
"unfurl_links": true
}
}
const post_message = function (url, message) {
const body = JSON.stringify(message);
var options = {
hostname: url.hostname,
port: url.port,
path: url.path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length
}
};
var req = https.request(options);
req.write(body);
req.end();
return;
}
exports.handler = function (event, context, callback) {
const params = qs.parse(event.body);
const command = params.command;
const text = params.text;
const response_url = url.parse(params.response_url);
const token = params.token;
if (text === "debug") {
post_message(response_url, in_channel("Message Recieved"));
callback(null, not_in_channel(response_url.path));
} else switch(command) {
case "/google":
callback(null, not_in_channel("Let me Google that for you..."));
post_message(response_url, in_channel(googleLink(text)));
break;
case "/qr":
callback(null, qrCode(text));
break;
default:
callback(null, not_in_channel("I have no idea what you are asking of me"));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment