A tutorial on creating a Node.js Slack bot using AWS Lambda with AWS API Gateway.
I am choosing Asia Pacific (Singapore) region for AWS Lambda and Asia Pacific (Singapore) region for AWS API Gateway.
$ git clone https://github.com/lesterchan/slack-bot.git
$ cd slack-bot
$ npm install --production
$ zip -r slack-bot.zip *.js node_modules/*
- Go to AWS Lambda.
- Click "Get Started Now".
- Under the "Select blueprint" screen, search for "hello-world"and you will see the hello-world blueprint which says "A starter AWS Lambda function.".
- Click on "hello-world" (NOT "hello-world-python").
- You will be brought to the "Configure Function" page.
- Under "Name", you can choose any name for your function. I called it "slack-bot".
- Under "Runtime", ensure it is "Node.js".
- Under "Code entry type", choose "Upload a .ZIP file" and click the "Upload" button" to browse for the file "slack-bot.zip" which you have zipped previously.
- Under "Handler", we leave it as "index.handler".
- Under "Role", we choose "Basic Execution Role".
- You will be brought to a "Role Summary" page.
- Under "IAM Role", choose "lambda_basic_execution".
- Under "Role Name", choose "oneClick_lambda_basic_execution_.....".
- Click "Allow".
- You will be brought back to the "Configure Function" page.
- Leave "Memory (MB)" as "128MB".
- You might want to increase "Timeout" to "15" seconds.
- Under VPC, choose "No VPC".
- Click "Next".
- Click "Create function".
- Go to AWS API Gateway.
- Click "Get Started Now".
- Under "API name", enter the name of your API. I will just name it "Slack Bot".
- Click "Create API".
- You will be redirected to the "Resources" page.
- Click "Create Method" and on the dropdown menu on the left, choose "POST" and click on the "tick" icon.
- Now, you will see the "/ - POST - Setup" page on the right.
- Under "Integration Type", choose "Lambda Function".
- Under "Lambda Region", choose "ap-southeast-1".
- Under "Lambda Function", type "slack" and it should auto-complete it to "slack-bot".
- Click "Save" and "Ok" when the popup appears.
- You will be brought to the "/ - POST - Method Execution" Page.
- Click "Integration Request".
- Click "Mapping Templates" and the section should expand.
- Click "Add Mapping Template" and type in "application/x-www-form-urlencoded" and click on the "tick" icon.
- Under "Input Passthrough" on the right, click on the "pencil" icon.
- Choose "Mapping Template" on the dropdown that appears.
- Copy and paste this GitHub Gist to the template box.
- Click on the "tick" icon beside the dropdown once you are done.
- This GitHub Gist will covert the your API Gateway data from application/x-www-form-urlencoded to application/json.
- Click on "Deploy API" button on the top left.
- Under "Deployment Stage", click "New Stage".
- Under "Stage Name", I will type in "production".
- Click "Deploy".
- Note the "Invoke URL" at the top and your API is now live.
- Go to Slack Apps.
- Search for "Outgoing WebHooks".
- Click "Install" besides the team you wanted.
- Click "Add Outgoing WebHook Integration".
- Scroll down to "Integration Settings" section.
- Under "Channel", choose "Any".
- Under "Trigger Word(s)", type in "bus,haze,weather,ipinfo,socialstats" (without the quotes).
- Under "URL(s)", type in your "Invoke URL" as noted above.
- Customize "Descriptive Label", "Name" and "Icon" to your liking and click "Save Settings".
- You are all set.
Do a CURL to see if your Lambda and API integration works.
$ curl --data "text=haze&trigger_word=haze" <Invoke URL>
You should see the response something like that:
{
"attachments":[
{
"pretext":":cloud: *Haze*",
"title":"PM2.5 Hourly Update",
"text":"Last updated at _14:00, 7th March 2016_",
"fallback":"Average: 11, Central: 10, North: 9, South: 16, East: 10, West: 9",
"mrkdwn_in":[
"pretext",
"text"
],
"color":"#479b02",
"fields":[
{
"title":"Average",
"value":"11",
"short":true
},
{
"title":"Central",
"value":"10",
"short":true
},
{
"title":"North",
"value":"9",
"short":true
},
{
"title":"South",
"value":"16",
"short":true
},
{
"title":"East",
"value":"10",
"short":true
},
{
"title":"West",
"value":"9",
"short":true
}
]
}
]
}
- Type in "haze" (without the quotes) in any Slack channel.
- You should get back a nicely formatting response as shown in the first screenshot.
Usage: bus <busStopNo> <busNo>
Example: bus 30111 991
Usage: bus <busStopNo>
Example: bus 44591
Usage: haze
Example: haze
Usage: weather
Example: weather
Usage: ipinfo <ip>
Example: ipinfo 8.8.8.8
Usage: socialstats <url>
Example: socialstats https://lesterchan.net