Building a Serverless Acronym Bot on GCP using Cloud Functions

rav3n
4 min readMay 26, 2019

--

I was originally making this tutorial using DynamoDB, API Gateway and Lambda but I decided that I needed more practice with Google Cloud Platform.

Setup

The acronyms I’m using for this tutorial are from here.

You’ll need to log into the Google Cloud Platform and create a new project if you don’t have one already. You’ll also need to enable the Cloud Functions API here (if you are signed in).

I’m currently using the GCP Free tier which has two parts as mentioned on their site:

  • A 12-month free trial with $300 credit to use with any GCP services.
  • Always Free, which provides limited access to many common GCP resources, free of charge.

Installing the bot

After signing into Slack, you’ll need to create a new app and give it a name and workspace. This is assuming you have the proper admin permissions and a slack workspace to do this.

On the next page, under Add features and functionality, select Bots

Next, you’ll Add a Bot User. I left the default on the next page except turning on Always Show My Bot as Online.

Then click Add Bot User.

Jumping back to the Basic Information Tab, click Install App to Workspace

Lastly, you’ll need to authorize the bot to be installed. Once that is done, you should see your bot online in your workspace.

If you scroll down, under the Basic Information tab, you’ll see your verification token. Keep this handy as it will goes into the config.json file we’ll create later.

Creating Function

After jumping over to Google Cloud Platform, you’ll need to navigate to Cloud Functions by clicking the navigation (top-left corner) and clicking Cloud Functions.

Now you could create a function here through the GUI but I just want to navigate to this page while I built the function via Google Cloud Shell. I’ve learned that this resets every time you disconnect. So if you build something in the /tmp/ folder, it won’t be there tomorrow. It save your command history though (woot) if you using the history command.

First command I ran to ensure all components were up-to-date:

gcloud components update

Then, I navigated over to the /tmp/ directory in the Google Cloud Shell and created 3 files.

  • config.json — where Slack verification token is stored
  • data.json — where list of acronym dictionaries are stored
  • main.py — the file that contains the cloud functions logic
cd /tmp/
mkdir slackbot
cd slackbot
# source code here
# https://github.com/pyraven/gcp-serverless-slack-acronymbot/
vi config.json
# copy/paste the json from the github
# You'll need to edit this file and add the verification token from # earlier
vi data.json
# copy/paste as is from the github
vi main.oy
# copy/paste as is from the github
# create the cloud function
gcloud functions deploy acronym_bot --runtime python37 --trigger-http

After you create the function (it’ll take a minute or so to deploy), it will output a url which we will use for Slack.

httpsTrigger:
url: https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/acronym_bot

Keep this URL handy and head back over to Slack. We’ll create a new Slash Command here.

NOTE: You can click into this function to find the source code that you deployed if you need it.

Jumping back to Slack:

In the Request URL, you’ll post the Cloud Function URL here and save.

Testing

I created a new channel and invited the bot into the channel. Next I took a sec to… well you know…

The results of looking up a few acronyms (DD, AAA, and DDD). The last one of the three doesn’t exist in the data.json file.

And it’s working!

I hope helps someone, somewhere. Have a good rest of your day and thanks for reading. Cheers.

--

--