Skip to content

Instantly share code, notes, and snippets.

@pathawks
Last active April 9, 2016 04:28
Show Gist options
  • Save pathawks/d2023c99a9813f6907b5f0f0ccead8d4 to your computer and use it in GitHub Desktop.
Save pathawks/d2023c99a9813f6907b5f0f0ccead8d4 to your computer and use it in GitHub Desktop.

Revisions

  1. pathawks revised this gist Apr 9, 2016. 1 changed file with 39 additions and 5 deletions.
    44 changes: 39 additions & 5 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    const qs = require('querystring');
    const qs = require('querystring');
    const https = require('https');
    const url = require('url');

    const googleLink = function (text) {
    const query = encodeURIComponent(text).replace(/%20/g,'+');
    @@ -7,7 +9,7 @@ const googleLink = function (text) {

    const qrCode = function (url) {
    return {
    "response_type": "in_channel",
    "response_type": "ephemeral",
    "attachments": [{
    "fallback": "https://chart.googleapis.com/chart?chs=512x512&cht=qr&chl=" + url,
    "text": "QR code linking to " + url,
    @@ -25,19 +27,51 @@ const in_channel = function (text) {
    }
    }

    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;

    switch(command) {
    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, in_channel(googleLink(text)));
    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, JSON.stringify(event));
    callback(null, not_in_channel("I have no idea what you are asking of me"));
    }
    };
  2. pathawks revised this gist Apr 8, 2016. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,23 @@ const googleLink = function (text) {
    return "<https://www.google.com/search?q=" + query + "|google.com/search?q=" + query + ">";
    }

    const qrCode = function (url) {
    return {
    "response_type": "in_channel",
    "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
    "text": text,
    "unfurl_links": true
    }
    }

    @@ -21,6 +34,9 @@ exports.handler = function (event, context, callback) {
    case "/google":
    callback(null, in_channel(googleLink(text)));
    break;
    case "/qr":
    callback(null, qrCode(text));
    break;
    default:
    callback(null, JSON.stringify(event));
    }
  3. pathawks revised this gist Apr 8, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ const qs = require('querystring');

    const googleLink = function (text) {
    const query = encodeURIComponent(text).replace(/%20/g,'+');
    return "<https://www.google.com/search?q=" + query + ">";
    return "<https://www.google.com/search?q=" + query + "|google.com/search?q=" + query + ">";
    }

    const in_channel = function (text) {
  4. pathawks revised this gist Apr 8, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    var qs = require('querystring');
    const qs = require('querystring');

    const googleLink = function (text) {
    return "<https://www.google.com/search?q=" + encodeURIComponent(text) + ">";
    const query = encodeURIComponent(text).replace(/%20/g,'+');
    return "<https://www.google.com/search?q=" + query + ">";
    }

    const in_channel = function (text) {
  5. pathawks revised this gist Apr 8, 2016. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,13 @@
    var qs = require('querystring');

    const response = function(text) {
    const url = "https://www.google.com/search?q=" + encodeURIComponent(text);
    const googleLink = function (text) {
    return "<https://www.google.com/search?q=" + encodeURIComponent(text) + ">";
    }

    const in_channel = function (text) {
    return {
    "response_type": "in_channel",
    "text": "<" + url + ">"
    "text": text
    }
    }

    @@ -15,9 +18,9 @@ exports.handler = function (event, context, callback) {

    switch(command) {
    case "/google":
    callback(null, response(text));
    callback(null, in_channel(googleLink(text)));
    break;
    default:
    callback(null, JSON.stringify(event));
    }
    };
    };
  6. pathawks created this gist Apr 8, 2016.
    23 changes: 23 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    var qs = require('querystring');

    const response = function(text) {
    const url = "https://www.google.com/search?q=" + encodeURIComponent(text);
    return {
    "response_type": "in_channel",
    "text": "<" + url + ">"
    }
    }

    exports.handler = function (event, context, callback) {
    const params = qs.parse(event.body);
    const command = params.command;
    const text = params.text;

    switch(command) {
    case "/google":
    callback(null, response(text));
    break;
    default:
    callback(null, JSON.stringify(event));
    }
    };