In this blog I will show you how to create and deploy a Slackbot on Google Cloud Platform in less than 5 minutes using copier template. To write a bot for Slack is not too difficult. With a few lines of code, you can create a program which will respond to slack commands and mentions. But that is the easy part: you also need to configure the application in Slack, deploy it so that is available 7×24, write unit tests and create CI/CD deployment pipelines and more. This copier template has it all!
- it creates the source code for your slackbot.
- it provides the Terraform templates for the infrastructure, the application and the CI/CD pipeline.
- it provides a CI/CD pipeline using Google CloudBuild to build and deploy the application and infrastructure changes.
This means you can start interacting with your slackbot and iteratively improve the implementation until you have the perfect bot.
deploy a slackbot to Google Cloud Platform
To create and deploy your slackbot, you need to:
- generate the source directory
- create the application in Slack
- install the application in the workspace
- obtain the application token
- obtain the signing secret
- obtain the bot user OAuth token
- deploy the slackbot
- deploy the tokens and secrets
- run the CI/CD pipeline
The following sections provide a detailed explanation of each step.
generate the source directory
Let’s say you want to create a slackbot which generates images using Dall-e. To generate the source directory, type:
$ pip install copier $ copier https://github.com/binxio/slackbot-on-google-cloud-platform-template.git /tmp/dali
> the human readable name of your slackbot Salvador Dali > a short description of the Slackbot generates images on request > the name of the package salvador_dali_slackbot > the slackbot command prefix /dali > Your full name? Mark van Holsteijn > Your email address? email@example.com > the google project to deploy to speeltuin-mvanholsteijn > primary region to deploy the slackbot to europe-west4 > the replica region for slackbot artifacts europe-west1 > the artifact repository location to deploy the image to europe > name of the terraform state bucket speeltuin-mvanholsteijn-terraform-state
Now the source code is ready in /tmp/dali. The contents of that directory looks like this:
. ├── src # bootstrap implementation │ └── salvador_dali_slackbot │ ├── __init__.py │ ├── __main__.py │ ├── bot.py │ ├── google_secrets.py │ └── manifest.yaml ├── tests # skaffold tests for bot functions │ ├── test_command_help.py │ └── test_mention_help.py ├── terraform # deployment definitions │ ├── pipeline.tf │ ├── slackbot.tf │ ├── user-data.yaml │ ├── variables.tf │ └── versions.tf │ ├── providers.tf ├── cloudbuild # CI/CD pipeline definitions │ ├── build.yaml │ └── deploy.yaml ├── secrets ├── Dockerfile ├── Makefile ├── Makefile.mk ├── Pipfile ├── pyproject.toml ├── setup.cfg ├── tox.ini
As you can see everything you need to build, maintain and deploy a slackbot is there! Next, you need to create the application in Slack.
Create the application in Slack
To create the slackbot in Slack, you can use the application manifest
manifest.yaml found in the generated source directory. Copy the contents and goto https://api.slack.com/apps and click on "create app from manifest".
your next step is to install the application in the slack workspace.
Install the application in the workspace
After you created the application, you can install it in your workspace. It is easy: just click the button and follow the flow.
Once the application is installed, you need to obtain the application token, signing secret and bot token.
Obtain the application token
After you install the application, scroll down the screen and generate an application token with the scope
connections:write. Copy the generated app token into the file
secrets/app-token. This token will put into a Google secret manager secret later. First you need to get the signing secret and bot token.
Obtain the application signing secret
To obtain the application signing secret, Navigate to Application Basic information, and copy the signing secret into the file
secrets/signing-secret. This secret will put into a Google secret manager secret later. The bot token is the last secret we need to get, before we can deploy the bot.
Obtain the bot user OAuth token
To obtain the bot user OAuth token, navigate to OAuth and permissions and copy the token into the file
Now everything is done to deploy the Slackbot using Terraform
deploy the slackbot
Before we can deploy the slackbot to Google Cloud Platform, you need to enable the used services and create the Terraform storage bucket. Type:
$ make enable-services $ make state-bucket
Now you are ready to let Terraform do it's magic. Type:
$ cd terraform $ terraform init $ terraform apply
This creates everything you need, but the bot will not yet run. The tokens and secrets needs to be made available as Google secrets.
deploy the tokens and secrets
To deploy the tokens and secrets of the slackbot into the respective Google secrets, type:
$ make configure-secrets
run the CI/CD pipeline
Now deploy your changes to the git repository. This will trigger a build of the container image and a deployment of the newly created image using Google CloudBuild.
$ git push
Check the Google CloudBuild logs Once the build and deploy complete your bot is ready to run! You can now chat with your bot and start developing the functionality.
This copier template provides everything you need to quickly build, deploy and maintain a Slackbot on Google Cloud Platform. If you have any questions, problems or feedback feel free to contact me. You can also directly add issues on Github