Create and integrate Slack App and AWS Lambda (for ruby) in 30 minutes

This entry is the 19th day article of iCARE Advent Calendar 2020.

Introduction

The function to be created this time is · Send a message with mentions to Slack App ・ Slack App returns a message with the same content with a mention to yourself is. This time, the main method is to link Slack App and AWS Lambda (for ruby), so the process itself to be implemented in Lambda is simple. I think that various functions can be created by modifying the program in Lambda, so I hope it will be helpful.

Procedure to create

--Creating a Slack App --Create AWS Lambda (for ruby) function --Create API Gateway --API authentication and Event settings in Slack App

Let's make it.

Creating a Slack App

Log in to Slack from the URL below. Please log in with the account of the workspace where you want to create the app.

https://slack.com/signin

Then access the following URL.

https://api.slack.com/apps

A screen like the one below will be displayed. Click [Create New App]. 001.jpg

Enter the name of the Slack app you want to create in [App Name], select the target workspace, and click [Create App].

We will add permissions to the created application. Under Basic Information-Add features and functionality, click Permissions. 003.jpg

Click [Add an OAuth Scope] in the [Scopes] section. 004.jpg

This time, it's just "read the message with mention to Slack app and return it as it is" Grant two permissions, [app_mentions: read] and [chat: write]. 005.jpg

You can also change Permission from the [OAuth & Permissions] item, and you can change it later. 006.jpg

We will install it in the Slack workspace. Click OAuth & Permissions-Install to Workspace. 007.jpg

A confirmation screen for installing to the Slack workspace will be displayed. Click [Allow]. 008.jpg

You can now use the Slack App in your workspace. Next, the screen will change to the [OAuth & Permissions] screen, so copy and leave the displayed [Bot User OAuth Access Token]. Used to send messages from Lambda via Slack App. 009のコピー.jpg

Creating an AWS Lambda (for ruby) function

Next, let's create an AWS Lambda function. From the AWS Console screen, move to Lambda. Click Create Function. 001.jpg

This time, we will create it with [Create from scratch]. 002.jpg

Set the Function Name, Runtime, and Access Privileges. Specify Ruby2.5 for [Runtime]. (I have only confirmed the operation with 2.5, so I do not know if it will work with 2.7) Permission is set to Create new role with basic Lambda permissions. If you want to use an existing role, select from [Use existing role]. 003.jpg

Since the [Detailed Settings] part has not been set in particular, click [Create Function] as it is. 004.jpg

A lambda_function.rb similar to the one below will be created. 005.jpg

The lambda_handler function in this lambda_function.rb is the function that will be executed when Lambda is actually called. This time, since the event is notified from slack in this process, we will implement the process of analyzing the parameter and returning it to the corresponding channel. The processing is published at ↓. https://github.com/Nobuo-Hirai/parrot-for-Lambda

Follow the steps below to deploy to Lambda.

git clone [email protected]:Nobuo-Hirai/parrot-for-Lambda.git

## bundle install
##Since gem also needs to be included in the Lambda application, vendor/Please insall under bundle.
bundle config set --local path 'vendor/bundle'
bundle install

##Create zip file
zip -r parrot-for-Lambda.zip vendor Lambda_function.rb

After creating the zip file, upload it from [Upload zip file] of Lambda. (If the size of the zip file exceeds 10MB, it will be via S3) 006.jpg

Next, set the environment variables used by the Lambda function. The setting method is set from [Edit] of the environment variable below. 007.jpg

Set the following three environment variables to use.

Key value
api_app_id Slack api_app_id (App ID displayed on the Slack App page)
channel_id Caller's Slack channel id. Do not allow execution except for the target channel.(To check the channel id, select the relevant channel in the app and right-click to copy the link. Paste it into your browser,/archives/After that is id)
slack_token Slack access token(Bot User OAuth Access Token)

Since it is difficult to understand how to check the channel_id, I will attach a screenshot. This is the last part after archives /. 008.jpg

After setting, it should look like the following. 009.jpg

Creating an API Gateway

Next, create an API Gateway that connects Slack and Lambda. Go to AWS API Gateway and click Create API. 001.jpg

Select [New API], enter an arbitrary name in [API Name], and click [Create API]. 002.jpg

Click Actions-Create Method. 003.jpg

Then click the [/ (slash)] part and select the [POST] method.

Set the following as the method settings and click [Save].

Integration type: Lambda function
Using Lambda proxy integration: checked
Lambda Region: Specify the region of the corresponding Lambda function
Lambda function: Enter the name of the created Lambda function
Use default timeout: checked

005.jpg

A screen confirming the addition of permissions to the Lambda function will be displayed. Click [OK]. 006.jpg

The following screen will be displayed. Select [Deploy API] from [Action]. 008.jpg

Enter the stage name and click Deploy. (This will be the last string on the endpoint) 009.jpg This completes the link between the API Gateway and the Lambda function, and the API endpoint is created. The URL displayed in the [Call URL of screen] part will be the endpoint, so copy and save it. 010のコピー.jpg

API authentication and Event settings in Slack App

Next, we will authenticate the API endpoint created with Slack App and call the API if there is a mention to Slack App, so we will proceed with that.

Return to the Slack App screen and select Event Subscriptions. 001.jpg

Change Enable Events, which is initially [OFF], to [ON]. Next, enter the URL of the API endpoint in Request URL and it will be Verified. Here, we are checking communication from Slack to the API endpoint. Specifically, the parameter {challenge: xxxxx} is sent, so that challenge parameter must be returned as is. This is a lamba process

#For authentication of slack's Event API
  if event["body"].present? && JSON.parse(event["body"])["challenge"].present?
    return { statusCode: 200, body: JSON.parse(event["body"])["challenge"] }
  end

It is implemented in the place called. 002.jpg

Next, add [app_mention] from [Add Bot User Event] of [Subscribe to bot events] and click [Save Changes] to add the function to call the API if there is a mention to Slack App. 003.jpg

Now that all the implementation is complete, I would like to actually call the Slack App! !! 004.jpg

@parrot [message] I sent the same message back! You succeeded! 30 minutes, may be a little overstated, but Slack App and Lambda can easily work together, so give it a try! This time the process itself was simple, but it seems to be fun to do various things by changing the implementation in the lambda function!

Finally

iCARE also posts Tech Blog every month, so please check it out! We also hold Meet Up, so if you are interested, please feel free to join us!

Recommended Posts

Create and integrate Slack App and AWS Lambda (for ruby) in 30 minutes
Notify Slack of AWS bills daily with Lambda for Ruby
Install gem in Serverless Framework and AWS Lambda with Ruby environment
[Java] for Each and sorted in Lambda
I tried to integrate AWS I oT button and Slack
[AWS Lambda] Resize the image saved in S3 and save it in another S3 (using Ruby)
[Programming complete] §5 Create a review management app in Ruby
Create a SlackBot with AWS lambda & API Gateway in Java
Easily create virtual S3 and test the integration between AWS Lambda and S3 services in your local environment
Create a JVM for app distribution with JDK9 modules and jlink
Create a periodical program with Ruby x AWS Lambda x CloudWatch Events
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
AWS SDK for Java 1.11.x and 2.x
Create a new app in Rails
[Ruby] then keyword and case in
Write keys and values in Ruby