Create a Slack Bot for random selection using AWS CDK #aws #slack #cdk

Manabu Uchida
7 min readSep 28, 2020

--

Summary

Built an AWS resource using the AWS Cloud Development Kit (AWS CDK).

I built a Slack bot using Amazon API Gateway and AWS Lambda.

The main function of the bot is to randomly select from the participating members of the channel.

I’ve described a brief summary of what we did in these. I hope you find it helpful.

For the original, please refer to the following Japanese blog.

Sources and Documents

Check out GitHub below.
The brief documentation is in the redme file.

About the hit command

The hit command allows you to randomly select a specified number of members participating in a Slack channel.

The intended use case would be to select a review member or a meeting facilitator.

In fact, there are many times when you need to shift to remote work and then select someone at random, and it feels good to have a default Slack app.

Creating a dedicated channel for the lottery seems like a good way to operate, but we’ve also implemented a mechanism that allows you to specify members who are not eligible for the lottery when you want to use an existing channel.

About the translate command

The translate command can translate Japanese into English and non-Japanese into Japanese using Amazon Translate.

The intended use case is to make it easy to translate RSS feeds in Slack.

In fact, if you’re checking RSS feeds in Slack, you may see a lot of status updates, so it would be a little more convenient if you could easily translate them.

You can use the Slack flag emoji method I created earlier to translate, but if you have a lot of participants in your workspace and use a lot of emoji reactions, the majority of the events will be unrelated to translation, and the Amazon API Gateway and AWS Lambda will run more often, because the Slack events fire over the entire workspace and cannot be focused on a specific channel or condition.

Sorry, the following is in Japanese only.

About the link command.

The link command uploads a file attached to Slack to S3 on mentioning a bot and issues a pre-signed URL to get the file.

The intended use cases include the delivery of a file that cannot be attached to an email, or the bringing of a file from the local environment to an EC2 instance.

In fact, when bringing a file to an EC2 instance, we often take the method of uploading the target file once to S3 and then retrieving the file from S3 on the EC2 instance, but it’s a nice touch that eliminates the need to upload the target file.

You can specify an expiration date for the signed URL, but in this case, the maximum value depends on this limit, as I am using the IAM role to run Lambda.

Also, multiple file attachments are possible, but signed URLs are limited to one file and one URL per file. This is because the signed URL of S3 is issued to the object. If you want to use 1 URL for multiple files, please compress the files themselves.

About the short command

The short command can issue a shortened URL.

The intended use case would be to convert a signed URL issued by the aforementioned link command into a short URL with a shortened URL.

In practice, signed URLs are quite long, and sometimes they can be copied incorrectly and not displayed. Shortening the URLs is a good way to reduce the number of simple mistakes.

Signed URLs have an expiration date, but you can also specify a shortened URL with an expiration date.

We refer to the following workshop to explain the mechanism for issuing these shortened URLs. Since this is an AWS CDK workshop, the code on the CDK side is also very helpful.

Part of what you need to be aware of when using it.

This time, we have deployed to the AWS account managed by the team on the assumption that it will be used in your team. Therefore, it is assumed that the parts to be considered in the specification can be shared.

If you would like to use it in your own environment, please note the following

Depends on AWS Lambda specification

Depends on memory capacity.

Depends on the execution time.

  • Lambda’s current maximum run time is 15 minutes
  • The current run time setting is also 15 minutes.
  • If there are many members participating in a Slack channel, the run time may be exceeded
  • If the attachment is extremely large, the run time may be exceeded
  • The execution time cannot be extended any longer, so we need to improve our internal mechanisms to deal with it.

Depends on the Amazon DynamoDB spec

Capacity Unit dependent.

Depends on the TTL specification.

Other.

About the build.

  • AWS resources are built on AWS CDKs
  • The Route 53 domain and the zone to be used must be created in advance
  • You need to set the zone name and zone ID as environment variables when you run the build with CDK

About the deletion.

  • AWS resources are deleted by AWS CDK
  • Zones in pre-created Route 53 domains will not be deleted.
  • Logs output to CloudWatch Logs will not be deleted
  • S3 does not delete the bucket because it cannot be deleted unless it is empty.
  • All other related resources are automatically deleted.
  • If you need to keep your data, take a backup beforehand.
  • To remove it completely. You need to manually delete the above undeleted resources

Finally

  • I wanted to use the AWS Cloud Development Kit (AWS CDK)
  • I made a bot for Slack that I usually use for this purpose.
  • The goal of this bot is to make it useful and useful in everyday life.
  • Random selection seems to be on the cusp there.
  • CDK is very useful and fun!
  • I think a lot of CDK implementation examples are TypeScript.
  • Personally, I’d like to write in Python, so I’d like to see more implementation examples.
  • I was wondering how to test the code I wrote in CDK.
  • By using CDK, we can develop Slack bots like this one without having to access the management console.
  • I would actively use CDK if I had the opportunity to do so in the future.

That’s all.

--

--

Manabu Uchida

The remarks are not affiliated with any company or organization. It is a personal opinion. Interest and concern Tags: AWS/golang/Python/Docker/GCP/Azure ...more