Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

This tutorial shows how to build a Slack bot using the Botkit toolkit and run it on Google Kubernetes Engine.

You will build a "Hello World" Slack bot that responds with a greeting in response to messages.


  1. Create a bot internal integration in Slack.
  2. Build a Node.js image in Docker.
  3. Upload a Docker image to a private Google Container Registry.
  4. Run a Slack bot on Google Kubernetes Engine.


This tutorial uses billable components of Google Cloud Platform, including the following:

  • Google Kubernetes Engine
  • Compute Engine (with Google Kubernetes Engine)
  • Cloud Storage (with Google Container Registry)

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

Before you begin

  1. Set up your development environment.

    1. Select or create a Google Cloud Platform project. Go to the projects page.
    2. Enable billing for your project. Enable billing.
    3. Install the Google Cloud SDK.
    4. Authenticate gcloud with Google Cloud Platform.

      gcloud init
    5. Install Node.js. See how to prepare a Node.js development environment for more detailed instructions.

    6. Install Docker.

  2. Create a new Slack team, or use one you already have if you have permissions to add new integrations to it.

Getting the sample code

The code for this tutorial is hosted on GitHub.

  1. Clone the repository with Git or download and extract the code as a ZIP file.

    git clone https://github.com/googlecodelabs/cloud-slack-bot.git
  2. Change to the sample code directory cloud-slack-bot/start.

    cd cloud-slack-bot/start
  3. Install the dependencies, including Botkit. Run this command in the cloud-slack-bot/start directory.

    npm install

Creating a Slack bot

A bot user can listen to messages on Slack, post messages, and upload files. In this tutorial, you will create a bot post a simple greeting message.

  1. Create a new Slack app.
    1. Give the app a name, such as "Kittenbot".
    2. Choose the Slack team where you want it installed.
  2. Add a new bot user to the app.
    1. Select Bot users under the features heading on the left-hand side navigation of the app configuration page.
    2. Click the Add a bot user button.
    3. Give it a nice username, like @kittenbot.
    4. This tutorial uses the Realtime Messaging (RTM) API, so keep the Always show the bot as online option selected as Off. The bot user will show as online only when there is a connection from the bot.
    5. Click the Add bot user button.
  3. Get the OAuth access token to allow the bot to connect to your team.

    1. Select OAuth & Permissions under the features heading on the left-hand side navigation of the app configuration page.
    2. Click the Reinstall app button. This will reinstall the app to your team and add the bot user you just created.
    3. Click the Copy button to copy the Bot user OAuth access token text into your clipboard.

      You'll use the token in the next step. Don't worry. You can come back this configuration page from the apps management page if you need to get this token again.

Be careful with your bot user OAuth access token. Treat it like you would any other secret token. Do not store tokens in version control or share them publicly.

Running the bot locally

To run the bot locally on your development machine,

  1. Edit the kittenbot.js file and enter your Slack bot token.

  2. Run your bot using Node.js.

    node kittenbot.js

    In your Slack team, you should now see that Kitten Bot is online. If you do not see Kitten Bot in your direct messages list, open a direct message to the bot with the + icon.

    open direct message

    add kittenbot

  3. Say hello to @kittenbot in a direct message (or if you added Kitten Bot to a channel, the bot will respond to mentions there, too). It should meow back at you.


  4. In your terminal, press Control-C to stop the bot server.

Loading the API token from a file

Hard-coding an API token in the source code makes it likely to accidentally expose your token by publishing it to version control. Instead, you can load the token from a file, which is in the .gitignore file to prevent accidentally checking it into version control.

  1. Write your API token to a plain text file called slack-token (no file extension).

  2. Modify kittenbot.js to load the API token from the path specified by the slack_token_path environment variable.

    cp ../step-1-token-file/kittenbot.js kittenbot.js
  3. Run the bot locally to test it out.

    export slack_token_path=./slack-token
    node kittenbot.js

    You should see the bot online again in Slack and be able to chat with it.

  4. Press Ctrl-C to shut down the bot.

Containerizing your bot

Use Docker to containerize your bot. A Docker image bundles all of your dependencies (even the compiled ones) so that it can run in a lightweight sandbox.

Building a Docker container image

  1. Create the Dockerfile.

    cp ../step-2-docker/Dockerfile Dockerfile
  2. Save your Google Cloud Project ID to the PROJECT_ID environment variable. To get your project ID with the Cloud SDK, run

    export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
  3. Build the Docker image.

    docker build -t gcr.io/${PROJECT_ID}/slack-codelab:v1 .

    This step takes some time to download and extract everything.

Running a Docker container locally

  1. Run the newly-created container image as a daemon.

    docker run -d \
        -v $(pwd)/:/config \
        -e slack_token_path=/config/slack-token \

    The -v argument mounts the current directory as a volume inside the container to give it access to the slack-token file. The -e argument sets the slack_token_path environment variable within the running container.

    See the full documentation for the docker run command for details.

  2. You should see that Kitten Bot is online again.

Stopping a Docker container

  1. Get the ID of the running container.

    docker ps
  2. Stop the container. Replace the docker container ID (fab8b7a0d6ee in the example) with the ID of your container.

    docker stop fab8b7a0d6ee

Pushing a Docker image to Google Container Registry

Now that the image works as intended, push it to the [Google Container Registry])(https://cloud.google.com/container-registry/), a private repository for your Docker images.

gcloud docker -- push gcr.io/${PROJECT_ID}/slack-codelab:v1

If all goes well, you should be able to see the container image listed in the Google Cloud console.

Your Docker image is now published to your private repository, which Kubernetes can access and orchestrate.

Deploying a bot to Kubernetes Engine

Now that the Docker image is in Google Container Registry, you can run the gcloud docker -- pull command to save this image on any machine and run it with the Docker command-line tool.

If you want to make sure your bot keeps running after it is started, you'll have to run another service to monitor your Docker container and restarts it if it stops. This gets even harder if you want to make sure the bot keeps running even if the machine it is running on fails.

Kubernetes solves this. You tell it that you want there to always be a replica of your bot running, and the Kubernetes master will keep that target state. It starts the bot up when there aren't enough running, and shuts bot replicas down when there are too many.

Creating a Kubernetes cluster with Kubernetes Engine

A Kubernetes Engine cluster is a managed Kubernetes cluster. It consists of a Kubernetes master API server hosted by Google and a set of worker nodes. The worker nodes are Compute Engine virtual machines.

Create a cluster with two n1-standard-1 nodes (this will take a few minutes to complete):

gcloud container clusters create my-cluster \
      --num-nodes=2 \
      --zone=us-central1-f \
      --machine-type n1-standard-1

Alternatively, you could create this cluster via the Cloud Console.

Deploying to Kubernetes Engine

Kubernetes has a Secrets API for storing secret information such as passwords that containers need at runtime. Create a secret to store your Slack API token.

kubectl create secret generic slack-token --from-file=./slack-token

Next, create a Kubernetes Deployment for your bot. A deployment describes how to configure the container and configures a replication controller to keep the bot running.

  1. Copy the deployment.yaml file.

    cp ../step-3-kubernetes/slack-codelab-deployment.yaml slack-codelab-deployment.yaml
  2. Modify the image name in slack-codelab-deployment.yaml with your project ID.

    # Replace PROJECT_ID with your project ID.
    image: gcr.io/PROJECT_ID/slack-codelab:v1
  3. Deploy your bot to Kubernetes.

    kubectl create -f slack-codelab-deployment.yaml --record
  4. Check that the bot is running.

    kubectl get pods

    When the bot is running, the output will be similar to

    NAME                             READY     STATUS    RESTARTS   AGE
    slack-codelab-3871250905-dknvp   1/1       Running   0          3m

    You should also see that the bot is online in Slack.

Troubleshooting See the Kubernetes documentation for help with debugging a pod that is stuck pending.

Cleaning up

Congratulations, you now have a Slack bot running on Google Kubernetes Engine.

You can follow these steps to clean up resources and save on costs.

  1. Delete the Deployment (which also deletes the running pods).

    kubectl delete deployment slack-codelab
  2. Delete your cluster.

    gcloud container clusters delete my-cluster

    This deletes all of the Compute Engine instances that are running the cluster.

  3. Delete the Docker registry storage bucket hosting your image(s).

    1. List the Cloud Storage buckets to get the bucket path.

      gsutil ls

      Command output

    2. Delete the bucket and all the images it contains.

      gsutil rm -r gs://artifacts.${PROJECT_ID}.appspot.com/

Of course, you can also delete the entire project but you would lose any billing setup you have done (disabling project billing first is required). Additionally, deleting a project will only happen after the current billing cycle ends.

Next steps

See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides


Request a Tutorial

Ask for community help


GCP Tutorials

Tutorials published by GCP


Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.