DEV Community

Standard Library
Standard Library

Posted on • Originally published at Medium

How to Build a Slack App to Keep a Slack Channel Topic Locked with Airtable and Standard Library

Have you ever wanted to lock your Slack channel topics? A friend of ours, Ben Tossell, recently posed this question on Twitter:

A fine question, indeed.

If you’re reading this, then odds are that you use Slack for internal communication when at work. It is likely that you also know that Slack Channel topics can set the tone for how users interact with one another in a given channel. They are a constant reminder that a channel is meant for serious business, reserved for horseplay, or falls somewhere in between. And while they are undeniably important, they are also extremely fragile, subjected to the will of the masses who make up the channel. At any moment your topic can be replaced, willy-nilly, by any member of the channel, and you are powerless to prevent it. That is, until now.

There is always one.

Today we are going to see how you can retake control of your Slack channel topics by deploying a Slack bot on Standard Library that uses Airtable to lock a channel’s topic to a stored value. So let’s dive in!

For those who just want to deploy a finished project, navigate to this Github repo. You will still need to set up Airtable and have a Standard Library account, but no other setup will be required on your part.

What You’ll Need Beforehand

1x Airtable Account — https://www.airtable.com
1x Slack Account — https://www.slack.com
1x Standard Library Account — https://www.stdlib.com

Step 1: Setting Up Airtable

First thing’s first. We want to set up some way to persist our desired channel topics. Airtable, while it looks like a spreadsheet, works a lot like a database, and is simple to use and easy to set up. Get a free account by clicking here, and then click here to add our template to your workspace. You should end up with something like this, except yours will only have 1 row:

Highlighted: Table Name

Feel free to add as many channels as you would like here! For this tutorial, I would recommend adding the #random channel and a topic of your choosing to the Base (this will be used in our test event later on). Just note that you must include the # before all channel names in the first columns, and that topics are limited to 250 characters. You are now ready to move on to setting up your workflow.

Step 2: Set up Your Workflow & Link Airtable

Head over to Build on Standard Library and begin setting up your project. Choose the following options for your project:

Event:

Slack → messages.channels

Actions:

Airtable → Select Rows by querying a Base

Slack → Set a Channel’s Topic

It should look like this:

You are ready to Create Workflow!

The next screen that you see will ask you to link your Airtable and Slack resources. We already set up an Airtable account in the previous step, so let’s start there. Click on the blue Link Resource button next to Airtable. On the following dialog page, click Link New Resource to select your new table. If you have used Airtable with Standard Library before, click Finish to link your previous account. If you are new to Airtable, you will see a different dialog box asking you to Enter a Name for Your Account (I recommend your account email) and to Retrieve the API Key from Your Account Page (found here). The screen should look like this:

Click on Finish to proceed!

Click on Finish, and on the next screen find and select the Base that we created earlier, Channel Topics, and click Finish here as well. That’s it for Airtable. Let’s move on to Slack!

Step 3: Set up Your Slack Bot

To begin, click on the Link Resource button next to Slack. If you already have a bot set up in your desired workspace, find it and select it on this first dialog screen. That’s it! You’re ready to move on to the next step. If you do not have a bot currently set up in the desired workspace, then continue along in this section.
Once you click Link New Resource, you will arrive at our new Slack workspace linking process. Your screen should look like this:

Choose Install Standard Library App to proceed.

Select the Standard Library Bot option and a pop-up will ask you to install Standard Library into your Slack workspace.

Slack workspace OAuth page.

On the following page, you will have the option to customize your bot. Feel free to be as creative as you would like here, but keep in mind that if you choose to add additional functionality to your bot down the road, other users in your channel may be exposed to the name of your bot and the thumbnail image.

Our Topic Bot.

Click on Finish and you should now see a green circle with a message Identity Generated underneath your two resources. If you do, click on the blue Next at the bottom of the dialog box and move on to Step 4.

Step 4: Set up Your Workflow

For this step, we will be using Build on Standard Library’s intuitive interface to generate Node.js code that will drive the functionality of our bot. The first order of business is setting up our Airtable query. Fill in the following details:

Table → Topics

Where → Channel → is equal to → #${result.step1.channel.name}

We can now give this a test run. Click the pause button next to the Set a Channel’s Topic row in our workflow, as pictured below, and click on the green Run with Test Event button:

Finding our channel in Airtable.

Scroll down to the very bottom of the test result and find the portion that includes “step3”. It should look something like this:

Your topic may vary.

If you see this, then we are ready to move on to the next step! Unpause the row that we paused earlier, and click on the six dots next to the Set a Channel’s Topic step in the workflow. Fill in the two fields as follows:

channel → ${result.step3.selectQueryResult.rows[0].fields.Channel}

topic → ${result.step3.selectQueryResult.rows[0].fields.Topic}

It should look something like this:

We are ready to change some topics!

Click on the Run with Test Event button and watch your bot go! Check your Slack workspace’s #random channel and you should see a message informing you that the channel’s topic has been changed to the value you input in Airtable. Pretty neat!

Step 5: The Finishing Touch

As it currently stands, our bot will attempt to change the topic in any channel that it receives any message in. But we really only want to perform this action when a user changes the channel topic in a channel that we are monitoring, not whenever Janice in accounting asks everyone to submit their expense reports. To do this, click on the Developer Mode button under your workflow to get a behind the scenes look at the code that we have been generating.
Developer Mode: ON.

Here you will want to replace all of the code with the following snippet to make sure that things are running smoothly.

This looks like a lot, so let’s take a closer look at what it is that we are doing.

-We are setting up some conditional logic to tell our project that if the message that is received is not an alert that the channel topic has been changed, then return a message to our logs (‘Status unchanged.’)

-If the message is an alert that the status has been changed, then we preform our query as written and perform one more conditional check before updating the channel topic

-Our conditional check looks to see if the channel is part of our Airtable Base, and then validates the topic length. If the channel is being monitored and the topic is valid, then we perform the set topic action using the value stored in Airtable for the respective channel

If you Run with Test Event now, the project should simply return a message informing you that the status was unchanged.

You should see a “Status unchanged” message.

You will also notice that you received no message in your #random Slack channel regarding a topic change. This means that our checks are working as intended. Click on the blue Next button, give your app a name, and click Alright, Ship it! That’s all you have to do! Feel free to test it out. Happy patrolling, Topic Bot!

*Kevin Brimmerman is a Software Engineer at Standard Library. Outside of work he is an avid runner and a die-hard Chicago sports fan. Go Cubs! *

Top comments (0)