Exploring new user notifications in Slack

I have an army of Slackbots behind me, just try and stop me!

Griffin Solot-Kehl · Jul 22, 2019

The inspiration

In the past weeks, I have been going through the Slack App Blueprints made by @girlie_mac in an attempt to get super familiar with Slack API capabilities on our platform. We have been making a push to make Transposit the best place to build Slack apps, so I challenged myself to see how many of the blueprints I could port to Transposit. In this blog post, I’m going to talk about new user notifications, which are super useful for scenarios like onboarding new employees, or having community members understand the rules of a workspace. I plan on treating this as a first in a series of the Slack Blueprints, so stay tuned for more posts about the others!

Understanding the Slack method

To quickly summarize what the new user notifications blueprint does, every time a new user joins your Slack workspace, our friendly little bot will send them a welcome message, followed by a ToS for them to accept. If a user doesn’t accept, it will remind them constantly by spamming their DMs until they accept them.

Taking a quick look at the readme on GitHub, it guides you to: fork their server code, pass in your credentials, and let it work. When looking into the source code, I was able to see their API calls made to Slack that I could run, as well as some of the other hidden functions, such as the reminder. What was extremely useful here was that the content in the API calls are in the same format we need to be able to call them in Transposit. Nice.

Conversion to Transposit

Forking a new Slack app on Transposit starts us out with a good start to begin creating our port. After following the instructions in the readme to create a Slack App, we need to give a webhook url in the Enable Events section, which conveniently, is already prepared for use in Transposit’s New Slack App starting point under deploy/endpoints.

However, the Events functionality in Slack Apps does require you to pass a challenge in order to verify ownership. Thankfully, we can put in a snippet of code into our webhook function to pass this test.

let body = JSON.parse(http_event.body);
if (body.challenge) {
  return {
    status_code: 200,
    headers: { "Content-Type": "text/plain" },
    body: body.challenge
  };
}

The only other Slack authentication we need to do now is pass in the custom credentials for our Slack bot into Transposit. This can be done on the data connections tab, where we use a Slack connector to store the credentials the Slack app gives us.

You can view the code for the app right here to see how it all comes together. We can parse in the information given from the webhook and send it into the according Slack API calls in Transposit to replicate the functionality from the express solution.

Tracking user responses

The original way that this Slack app tracks who has or hasn’t accepted the ToS was using node-json-db, which stores all data as a key-value pair. I decided to use Transposit’s Application stash for this purpose, as it works very similarly by allowing us to store key-value pairs, where we set the user ID as the key, and the boolean if they accepted or not as the value. However, if you wanted to use something better suited for hands-on administrative purposes, you are also free to use a connector. Airtable, Google Sheets, and Salesforce are all examples that could serve this purpose.

Reminding lollygaggers

Our stash allows us to write a quick reminder function as a Scheduled Task that will iterate over the list of keys we have stored and check to see if they have been accepted or not. This can run every week (or day. or month. your call.) to badger them enough to accept the ToS.

let users = stash.get("keys");
for (let i = 0; i < users.length; i++) {
if (!stash.get(users[i])) {
api.run("this.post_tos", { userid: users[i], message: "REMINDER" });
}
}

Extensions and conclusions

The best part of making this port though, was how simple it was to build. I didn’t have to set up and host my own server. The code being much easier to digest as sections of SQL and JavaScript that can be individually tested and experimented with interactively makes developing more streamlined compared to being lumped into one big file of server code. And having all of the API keys and tokens stored into the platform for you so that you could avoid needing to call them yourself is a huge headache averted.

Here we can see a great starting point (or blueprint 😉) for making a large number of Slack apps. One that I decided to make for my company’s Slack was one that would send all new users to the workspace a direct message containing a custom Slack theme I made. It’s a quick and easy way to make new users feel more welcome in your workspace, that ended up being a fun thing for many of my coworkers to explore. Most people didn't know you could even theme the Slack workspace to begin with, much less with just eight hex codes!

To learn more, check out our Quickstart which has been updated to help you build your own Slack app in Transposit from scratch. Or go fork this app or one of our sample apps to start building the Slackbot that will start your own army.