How to create and deploy a Slackbot on Google Cloud Platform in less than 5 minutes
Tags: cloud, googlecloudplatform
Published: 2023-11-09 17:34:56

---
title: How to create and deploy a Slackbot on Google Cloud Platform in less than 5 minutes
published: true
date: 2023-03-19 09:39:00 UTC
tags: Cloud,GoogleCloudPlatform
canonical_url: https://xebia.com/blog/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes/
---

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](https://copier.readthedocs.io/) 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](https://copier.readthedocs.io/) 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](https://cloud.google.com/build) 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:

1. [generate the source directory](#generate-the-source-directory)
2. [create the application in Slack](#create-the-application-in-slack)
3. [install the application in the workspace](#install-the-application-in-the-workspace)
4. [obtain the application token](#obtain-the-application-token)
5. [obtain the signing secret](#obtain-the-application-signing-secret)
6. [obtain the bot user OAuth token](#obtain-the-bot-user-oauth-token)
7. [deploy the slackbot](#deploy-the-slackbot)
8. [deploy the tokens and secrets](#deploy-the-tokens-and-secrets)
9. [run the CI/CD pipeline](#run-the-cicd-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](https://openai.com/product/dall-e-2). 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?
   mark.vanholsteijn@xebia.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](https://api.slack.com/apps) and click on “create app from manifest”. ![slack create new app from manifest](https://xebia.com/wp-content/uploads/2023/03/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes-create-app.jpg)

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.  
  ![slack install application](https://xebia.com/wp-content/uploads/2023/03/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes-install-app.jpg)

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`. ![slack generate app-token](https://xebia.com/wp-content/uploads/2023/03/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes-generate-app-token.jpg) Copy the generated app token into the file `secrets/app-token`. ![app token generated](https://xebia.com/wp-content/uploads/2023/03/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes-app-token-generated.jpg) 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`. ![signing secret](https://xebia.com/wp-content/uploads/2023/03/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes-signing-secret.jpg) 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 `secrets/bot-token`. ![signing secret](https://xebia.com/wp-content/uploads/2023/03/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes-bot-token.png)

Now everything is done to deploy the Slackbot using [Terraform](https://terraform.io)

## 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](https://cloud.google.com/secret-manager), 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](https://console.cloud.google.com/cloud-build/builds) Once the build and deploy complete your bot is ready to run! You can now chat with your bot and start developing the functionality.

## Conclusion

This [copier](https://copier.readthedocs.io/) 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](https://github.com/binxio/slackbot-on-google-cloud-platform-template.git)

* * *

Photo by [Stephen Phillips – Hostreviews.co.uk](https://unsplash.com/@hostreviews?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/slack?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

The post [How to create and deploy a Slackbot on Google Cloud Platform in less than 5 minutes](https://xebia.com/blog/how-to-create-and-deploy-a-slack-bot-on-google-cloud-platform-in-less-than-5-minutes/) appeared first on [Xebia](https://xebia.com).