I am new to Node, AWS Lambdas, and Slackbots, so what better way to learn them all than by trying to figure them out at the same time? Just kidding.
I have created a slackbot slash command app. That is, when a user on Slack types '/anondm @username msg' Slack will send this command to my Lambda function, which then returns an answer directly to the sender.
That goal is, have the Lambda take the message, remove the username, and forward the message onto the recipient anonymously.
Now, first off, this exists. Or at least, I can find code repos on GitHub. But I wanted a branded version and I wanted to add a couple little tricks.
What works: — Creating the bot and slash command was not a problem. — Getting the Lambda to receive the message and respond to the sender was also not a problem.
If you're interested, you just need an API Gateway and a Lambda function, and this tutorial will suffice: https://api.slack.com/tutorials/aws-lambda
What is not working: — I need to make an HTTPS GET request to the Slack API so it will execute the command of sending the message to the user.
I spent about a day trying to figure this out and went back and forth between many solutions, but then this morning after some coffee landed on something I REALLY like.
I've seen people suggesting using an SNS Topic. So one Lambda receives web request, then publishes to an SNS Topic which triggers another Lambda to send. Well, that sounds nice for something high volume where I would have use a job queue, but... i mean it's a web handler and a GET request... this should be a couple lines of code.
So the question: What is the easiest way to execute a GET request during a Lambda that has received a web request?
Here is what didn't work (in simplest form) but was expected to be the solution:
'use strict';
var qs = require('querystring');
var https = require('https');
var target = "_snip_";
exports.handler = function(event, context) {
console.log("This will for sure execute.");
https.get(target, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
console.log("This will for sure NOT execute.");
return "Your quest is complete.";
};
And here is what would work:
'use strict';
var qs = require('querystring');
var https = require('https');
var target = "_snip_";
exports.handler = function(event, context) {
console.log("This will for sure execute.");
https.get(target, function(res) {
console.log("Got response: " + res.statusCode);
context.succeed("Your quest is complete");
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
};