const qs = require('querystring'); const https = require('https'); const url = require('url'); const googleLink = function (text) { const query = encodeURIComponent(text).replace(/%20/g,'+'); return ""; } 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")); } };