Setup - mrcunninghamz/hi-command GitHub Wiki
Welcome to the hi-command wiki!
This is a aws lambda function that is called upon by a slash command from slack.
Overview
- Create a custom slack integration: slash command
- Create an AWS Lambda function
- Create an AWS API Gateway
Technology Prereq:
- Aws User Account
- Slack team
- Visual Studio with the latest .netcore: click here
- Visual Studio AWS Toolkit: click here
Getting Started
Setting up Slack
- Go to your integrations page: http://[your slack team name].slack.com/apps/build/custom-integration
- Click on "Slash Commands".
- Create a command, in this case "/hi".
- Click "Add Slash Command Integration".
- In the next page take note of the "URL" field.
- This will be the AWS API Gateway endpoint we create.
- Make sure "Method" is set to "POST".
- The "Token" field is for added security, we will not be secure... (for fun!).
- Set "Customize Name" to "hi-command".
- The other fields are optional at this point.
- Click "Save Integration".
Publish the hi-command project to an AWS Lambda instance
- Pull the code down and checkout the "develop" branch.
- Open the solution
- Notice these projects
- Core
- This has the slack related DTOs
- HiCommand.Master
- This is the master project that delegates orders to the other lambda functions
- HiCommand.Master.Tests
- This is the test project
- HiCommand.Quote
- This is the lambda funciton that makes the Genius API call, crawls the page with the lyrics and pulls out a phrase that was annotated.
- HiCommand.Quote.Tests
- This is the test project
- Core
- Right click on "HiCommand.Master" project
- Select "Publish to AWS Lambda..."
- If you do not see this please get the latest AWS Toolkit here.
- In the next screen click the little icon to add an aws account,
.
- You will need your "Access Key ID" and "Secret Access Key" from your AWS account.
- You can get this by logging into your AWS account and click the drop down below to view "Your Security Credentials"
- Click "Access Keys (Access Key ID and Secret Access Key)" on the following page.
- This opens an accordion which you can then click "Create New Access Key".
- A modal opens, click "Show Access Key". Copy and Paste these values to the dialog in Visual Studio.
- You will need your "Access Key ID" and "Secret Access Key" from your AWS account.
- Your account will show up in the drop down.
- Enter a "Function Name", in this case "HiCommand".
- Click "Next".
- In the "Role Name" field select "PowerUserAccess" Under the "New role based on AWS manged policy" section.
- Click "Upload".
- Do the same thing for the "HiCommand.Quote" Project.
- Make sure to name your function "HiCommand_Quote"
- This project requires a Genius API token that you can get here
- Add an environmental variable on this page called "Genius_Token"
- Click "Upload".
Create the AWS API Gateway
- Log into your AWS Account
- Click the top left drop down under "Services"
- Under "Application Services", click "API Gateway"
- Click "Create API"
- Select "New API"
- Name the API, "Hi Command API"
- Click "Create API"
- You will notice that you are in the resources section and the words "No methods defined for the resource" is in the middle of the screen.
- Click the drop down under "Actions" and select "Create Method"
- A drop down appears, select "POST" and click the circle with a check inside it that appears next to the dropdown to save.
- Now you have to configure the POST
- Select "Lambda Function"
- Select the "Use Lambda Proxy integration"
- The reason for this is to save you from having to create mapping templates. AWS Lambda functions by default expect a JSON object to be passed in. A Slack Slash command post sends the content-type application/x-www-form-urlencoded which mucks up everything and without this checked you will have to create a mapping template which uses Apache Velocity which I'm not too comfortable with when AWS makes it hard to debug the script you are writing. So instead we will use the proxy integration and you will notice that there is a nuget reference added to the project to "Amazon.Lambda.APIGatewayEvents" which will handle the proxy input request where we can handle the body of the slash command post in the code. I created unit tests that you can reference.
- Select the region you published your lambda in.
- Start typing the name of the lambda function you published and there will be a selection.
- Click "Save".
- Click on the "Actions" drop down and select "Deploy API"
- Give the deployment stage a name
- I still need to read up on deployment stages, I called my "prod", but something like "dev_hi-command" for lower environments might work, but it seems like whenever you upload your lambda there isn't a "staging" for it because the updated lambda is exposed to all "prod" deployment stage. So Idk, i'll have to figure that piece out.
- Click "Deploy"
- We're almost there, we need to update our Slack Slash Command.
- Copy the URL next to "Invoke URL" in the top of the Stage Editor.
- Notice that we didn't do this for the "HiCommand.Quote" project, this is because we are calling this lambda directly from the "HiCommand.Master" lambda.
Update Your slack integration
- Navigate to https://[your team name].slack.com/apps/manage
- Click "Custom Integrations"
- Click "Slash Commands"
- Click the "/hi" integration to edit.
- Paste the "Invoke URL" from the API Gateway stage into the "URL" field.
Test in slack
- Open your slack application. If you are using the desktop application "CTRL + R" and in any chat type "/hi".
- You should get a message that says "hello there, [your username]!"
How the solution with an AWS Lambda function using Visual Studio was created
- Open Visual Studio
- Create a new project
-
Under installed templates select "AWS Lambda", if you don't see these templates please get the latest AWS Toolkit here.
-
Select "Empty Function" on the next screen and click "Finish"
-
- This creates the project
- You will notice that there are more references added to the Hi-Command project because we want to utilize the Lambda Proxy option with our AWS API Gateway.
References
- Your first slash command: click here
- Using the AWS Lambda Project in Visual Studio: click here