Skip to content

Instantly share code, notes, and snippets.

@nalindak
Forked from vgeshel/function.js
Created February 12, 2016 04:45
Show Gist options
  • Select an option

  • Save nalindak/2d4b7453911a8de7960b to your computer and use it in GitHub Desktop.

Select an option

Save nalindak/2d4b7453911a8de7960b to your computer and use it in GitHub Desktop.

Revisions

  1. Vadim Geshel revised this gist Apr 13, 2015. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions function.js
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,6 @@ exports.handler = function(event, context) {
    (event.Records || []).forEach(function (rec) {
    if (rec.Sns) {
    var req = https.request(slack_req_opts, function (res) {
    console.log(JSON.stringify(res.headers));

    if (res.statusCode === 200) {
    context.succeed('posted to slack');
    } else {
    @@ -26,7 +24,7 @@ exports.handler = function(event, context) {
    context.fail(e.message);
    });

    req.write(JSON.stringify({text: rec.Sns.Message})); // for testing: , channel: '@vadim'
    req.write(JSON.stringify({text: JSON.stringify(rec.Sns.Message, null, ' ')})); // for testing: , channel: '@vadim'

    req.end();
    }
  2. Vadim Geshel created this gist Apr 13, 2015.
    34 changes: 34 additions & 0 deletions function.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    console.log('Loading function');

    const https = require('https');
    const url = require('url');
    // to get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration
    const slack_url = 'https://hooks.slack.com/services/...';
    const slack_req_opts = url.parse(slack_url);
    slack_req_opts.method = 'POST';
    slack_req_opts.headers = {'Content-Type': 'application/json'};

    exports.handler = function(event, context) {
    (event.Records || []).forEach(function (rec) {
    if (rec.Sns) {
    var req = https.request(slack_req_opts, function (res) {
    console.log(JSON.stringify(res.headers));

    if (res.statusCode === 200) {
    context.succeed('posted to slack');
    } else {
    context.fail('status code: ' + res.statusCode);
    }
    });

    req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
    context.fail(e.message);
    });

    req.write(JSON.stringify({text: rec.Sns.Message})); // for testing: , channel: '@vadim'

    req.end();
    }
    });
    };