Google Cloud Platform — Serverless Slack Bot
By: Wade Francis
Tags: Slack, Gcp, Cloud Functions, Google Cloud Platform, Slackbot

Background

Finding sample code for implementing a serverless Slack bot should be easy - serverless computing is almost a perfect match for the requirements for a Slack bot:

However being unable to find a complete easy-to-follow guide to create a Slack bot that could run serverless via a Google Cloud Function hello_bot was created to fill the gap. The bot is minimalistic and serves as a starting point for further development. It is sufficient to demonstrate what is needed to align all the moving parts to get a functional result.

Version 1 of the bot is python where the heart of the bot is a threaded Cloud Function which aims to deliver a response within the 3 second window required by Slack and subsequently allows more complicated request handling to be completed in the main thread. Additionally the function expects and verifies the presence of the Verification Token created as part of the application setup.

Version 2 is a more complicated implementation which more consistently handles the three second Slack acknowledgement time requirement and has two components, one implemented in go and one in python .

Code overview

hello_bot v1 is a minimal Slack app that will respond to a single command ( hello ) with a random greeting from a list of 50 languages. Any other commands will give a generic error message.

The repository related to this article can be found at https://github.com/servian/gcp-serverless-slackbot

The main code section of the bot is simple:

Perform basic checks - confirm it was a POST request and the appropriate Slack token was present

request and the appropriate Slack token was present Create a thread to handle the request

Return a 200 response

def hello_bot(request):

if request.method != 'POST':

return 'Only POST requests are accepted', 405 verify_web_hook(request.form)

response_url = request.form.get('response_url') # Create a thread to handle the request and respond immediately

# A response time of longer than 3 seconds causes a timeout

# error message in Slack

thr_response = Thread(target=handle_request,

args=[request.form['text'], response_url])

thr_response.start() # return empty string to reduce spam

return ''

Using a thread to handle the main processing of the bot should allow a quicker response to get back to Slack to acknowledge receipt of the request while handling the request over a longer execution time.

Deploying hello_bot

To deploy the steps are:

Create the Slack App

Enable Incoming Webhooks

Deploy the Cloud Function

Create the Slash command

This deployment order allows the interdependent components to be set up as simply as possible. The slack app creates a webhook and secret token which are then used as part of the configuration information of the cloud function. The endpoint URL of the cloud function is then made available to be called as the handler for the slash command in Slack.

Create the Slack App

Go to https://api.slack.com/apps and click Create New App

2. Fill in the app name and pick a workspace

Enable Incoming Webhooks

From Config Options, select Incoming Webhooks

2. Turn on Activate Incoming Webhooks and click Add New Webhook to workspace

3. Select the channel to which the bot is to be added and click Allow

4. Copy the provided Webhook URL and update the value in config.json

5. Copy the Verification Token from the Basic Information page and update the value in config.json

Deploy the Cloud Function

1. Initialise gcloud as required for the target project and then deploy the Cloud Function:

gcloud functions deploy hello_bot --runtime python37 —-trigger-http

2. Once deployed, copy the URL from the output or the web console:

3. Update the function permissions to grant allUsers the Cloud Functions Invoker role. This makes the function publicly available to be invoked from Slack:

Create the Slash Command

Click Slash Commands and then Create New Command

2. Populate the information and paste the URL created when deploying the Cloud Function

3. As the permissions have changed click reinstall the app to re-add it to the workspace

4. Try it out with /hello_bot hello or /hello_bot randomstring

So here we have a working serverless slack bot: