Create and distribute a slack bot with python and AWS in 1 hour

colin cazabet
Analytics Vidhya
Published in
8 min readJun 30, 2021

Learn how to create a slack bot from scratch in less than an hour with the help of AWS. You can then customize the bot with your needs. All the code in this article can be retrieved on my github.

In this article, I will show you how to create a simple bot that replies when someone posts a message in a channel.You will also learn how to distribute your bot so anybody can install it.

1. Setting up a slack application

For this step, we need to create a Slack app, fill basic informations, subscribe to events and setup permissions for our new bot.

  • Go to https://api.slack.com/apps and click on “create new app”
  • Enter the name of your application and select your workspace
  • You should be redirected to the “Basic information” section of your app
  • Update the “display information” part on this page
  • Go to the “Event Subscriptions” page on the sidebar:
  • Enable events and and click on “Subscribe to bot events”, add the following permission: message.channels . Leave the “Request url” field blank for now, we will fill it later.
  • Then, go to the “Oauth & Permissions tab” in the sidebar, go to the “Scopes” section and add the following scopes (some scopes are already prefilled and you can’t remove them.

You will have to customize your scopes depending on the problem you’re trying to solve.

2. Setting up AWS

In this step we will create all the components needed on AWS to create our bot. If you don’t have an AWS account yet, no worries, you can create on for free at https://aws.amazon.com/fr/free/ . Free tier is more than enough for most use cases.

  • Log into your AWS account and go to lambda in the services tab.
  • Click on “create function” then “author from scratch” and fill the name field
AWS lambda
  • Click on your new lambda function and then click on “Add trigger”, we will create an API gateway that will “expose” our lambda publicly so we can send it events.
  • Select “API Gateway” and then “Create an API” (HTTP API). Set the security as “Open” so slack can send you requests and click on “create
  • You can test your lambda by clicking on the Api endpoint link (see image below) and you should see “Hello from lambda!
  • Go back to slack to the “Event Subscriptions” page, paste the link of your API endpoint. You should see an error. It’s because slack is trying to verify your endpoint and you have to return the “challenge” value sent by slack.
  • On AWS, go to your lambda function to edit the code and put the code below to return the challenge and verify your URL.

def lambda_handler(event, context) is the entrypoint of every lambda function. The event parameter gives you info about the event that triggered the function (the slack API post request in our case)

  • Go back to Slack and click on “retry” to verify your request URL, it should work fine now.
  • You can go the “Basic information” tab on the sidebar and click on “Install your app” and then “install to workspace

3. Code the bot

Your bot is now installed on your workspace and your AWS lambda is ready to receive events from this workspace. It’s now time to add logic into the lambda function.

  • On your slack workspace, go to a channel and add your bot to the channel by typing @name-of-your-bot and press enter
add bot to channel

Your are now able to receive events on your lambda function ! We just have to add the logic in our lambda function, here is a little example that checks if there is a file attached to the message and reply if it is the case.

resume bot lambda code
  • For this example you need to add the BOT_TOKEN environment variable, go to “Oauth & Permissions” on slack to copy it and create an environment variable (see image below)
add environment variable

This is the result you should have if you use this code 👇

basic slack bot result

If you have a problem or it’s not working and you want to debug, you can go see the Cloudwatch service on AWS, it writes logs on each lambda call.

4. Add a slash command to your slack application (optional)

Slack allows you to create slash commands, like /setreply hello world ! , it’s very useful for a lot of cases

  • Go to the “Slash command” tab in the slack sidebar and click on “create new command”
  • You can create a second lambda function to process events related to slash commands, you will also have to create another API gateway, just repeat the steps I described before.
  • Once this is done, you can copy the link of your API endpoint into the “Request URL” field when creating the slash command.
  • Go to your new lambda function and extend this basic code depending on your needs:

Your lambda will be triggered when someone types your slash command into a channel. Adapt the logic to your needs.

5. Use dynamoDB to store data

Your bot may need to store information and do something more complicated than just replying a text message. I will show you how you can link DynamoDB to any of your lambdas so you can build any type of bot.

You also need a way to store tokens when you will want to distribute your slack application to install it on new workspaces (in next step)

  • Find the dynamoDB service in your AWS account and click on “Create table
  • Fill the table name and set the primary key, in my case the table name will be “token” and the primary key will be “token” too. We will store a slack token inside for each new workspace our bot is added to. We will also have a field named “teamId” which is an id unique to your workspace.

We now need to give our lambda the access to our dynamoDB service. Otherwise you will have a “permission denied error” when trying to access the database from the lambda.

  • Go to your IAM service on AWS and click on “Roles
  • Find the role corresponding to your lambda function, for me it’s “oauthLambda-role-et3lslpe” and click on it
  • Click on “Attach policies” in the permission tab and search for “dynamoDB”
  • Add the policy that corresponds to your needs, I added full access (AmazonDynamoDBFullAccess) in my case
  • If you are using python, you can use the great Boto3 library and query your table like that:

In this example I use the “reply” table and I update the “content” field of the row that has the name “default”. I invite you to read the Boto3 documentation to understand how to query your database.

5. Distribute your bot

Once you have finished coding your new bot, it’s time to distribute it. I will show you how to implement the oauth2 protocol so anybody can install your bot to its workspace.

  • Create a third lambda function (oauthLambda) with an API gateway (like in step 2)
  • On slack, go to “Oauth & permissions” in the sidebar, then click on “Add Oauth redirect URLs” and then “Set up redirect URLs
  • Add the URL of your new API gateway as a new redirect URL.
  • Go to the “manage distribution” tab on the sidebar, check your bot meet all the criterias and click on “activate distribution
  • Once this is done, you will get a sharable URL from slack (see image below), you have to do one last thing before testing it: code your lambda to do the oauth2 process.
  • Go back to AWS and edit the code of your last AWS lambda function (the oauth one). Put the code below:

This code will be called when someone tries to install your bot on their workspace, the only thing it does is saving the oauth token in dynamoDB next to the slack teamId.

  • You can now change the example code from step 3 to use the token that corresponds to the teamId in the event received from slack (here is an example on my github).

When an event happens on a workspace where your bot is installed, you will receive an event on your lambda with a payload that contains the “teamId”. Each teamId is stored with a bot token in your database, that’s how you know which token you have to use depending on the event received.

And voila ! You now have a bot that runs on AWS and anybody with your link can install it to his workspace.

Conclusion

I tried to show the main steps on how you can get started if you want to create a new slack bot in no time with AWS. You can extend the basic example with whatever your use case is. Feel free to use your imagination to contribute to the slack community with amazing bots.

If you have any questions, comment this post and I will answer as fast as possible.

--

--