This entry is the 19th day article of iCARE Advent Calendar 2020.
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.
--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.
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].
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.
Click [Add an OAuth Scope] in the [Scopes] section.
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].
You can also change Permission from the [OAuth & Permissions] item, and you can change it later.
We will install it in the Slack workspace. Click OAuth & Permissions-Install to Workspace.
A confirmation screen for installing to the Slack workspace will be displayed. Click [Allow].
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.
Next, let's create an AWS Lambda function. From the AWS Console screen, move to Lambda. Click Create Function.
This time, we will create it with [Create from scratch].
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].
Since the [Detailed Settings] part has not been set in particular, click [Create Function] as it is.
A lambda_function.rb
similar to the one below will be created.
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)
Next, set the environment variables used by the Lambda function. The setting method is set from [Edit] of the environment variable below.
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 /.
After setting, it should look like the following.
Next, create an API Gateway that connects Slack and Lambda. Go to AWS API Gateway and click Create API.
Select [New API], enter an arbitrary name in [API Name], and click [Create API].
Click Actions-Create Method.
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
A screen confirming the addition of permissions to the Lambda function will be displayed. Click [OK].
The following screen will be displayed. Select [Deploy API] from [Action].
Enter the stage name and click Deploy. (This will be the last string on the endpoint) 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.
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.
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.
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.
Now that all the implementation is complete, I would like to actually call the Slack App! !!
@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!
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