Build a “Serverless” Slack Bot in 7 Minutes with Node.js and Autocode

Keith Horwood
Mar 21, 2017 · 7 min read

Slack bots — they’re useful for sending notifications to your team, creating welcome messages when new users join a channel, and more.

Today we’ll be going over how to build a Serverless Slack Bot in 7 Minutes from a Basic Slack App template. This bot will (1) respond to a /hello command and (2) welcome members when they join a channel. To build it we’ll be using Autocode, the Node.js webhook, script and API automation platform. It offers a bunch of great features; a code editor that has baked-in API autocomplete, serverless hosting, API authentication management, webhook signing, revision history, API version control and more — all for free.

Without further ado, let’s begin!

What You’ll Need for this Tutorial

  • 1x Autocode Account
  • 7x Minutes (or 420x Seconds)

Minute 1: Prepare your Slack Workspace

You can create a new Slack Workspace by clicking here.

Having a development workspace where you can play around is helpful!

We recommend having a #general and #random channel set up as easy message targets when playing with a new Slack app.

Minute 2: Autocode Account Setup

Create an Autocode Account, Enter your E-mail First

From here, you’ll be brought through a simple onboarding flow to introduce you to the platform.

Minute 3: Visit the “Slack Example” Source Code

See a summary of project endpoints and file structure

From here, you can view the two individual endpoints;

(1) The /hello command endpoint at /events/slack/command/hello/:
https://autocode.com/src/slack/basic-slack-example/functions/events/slack/command/hello.js

(2) The member_joined_channel event endpoint:
https://autocode.com/src/slack/basic-slack-example/functions/events/slack/member_joined_channel.js

On each endpoint we’ll see a summary of how they work and which APIs they talk to:

Minute 4: Fork the “Basic Slack App” Source Code

Once clicked, the Fork button will open this prompt

Once you’ve clicked Create Fork, wait a moment for your project to be created and open in the Autocode editor:

The Autocode Editor. Your README.md will be open first.

Voila! You now have a project ready. Only a few steps left!

Minute 5: Link Your Slack Workspace

Click the red 1 Account Required button (Or the Slack logo)

You’ll be prompted with the following screen:

An explanation as to how Linked Accounts / Resources work

Click Link beside the Slack row to prompt you to link a Slack account…

You’ll want to click Link New Resource if this is your first time

Click Link New Resource. You’ll be prompted with the option to Install Autocode App or build a Custom Internal Application (Advanced). For the purpose of this tutorial, choose Install Autocode App and click Finish.

You’ll be prompted by Slack to install the app. Make sure you select the correct workspace!

Click Allow to proceed.

Once you click Allow, the last step is to customize your Slack app. You can set a custom name and profile image. If you don’t mind using the defaults, you can just click Finish.

Enter your Slack App details

You’ll now see that your app is linked! You can click Finished Linking to get back to your project.

Your Slack Workspace is linked! Way to go.

Minute 6: Test and Deploy your Slack Bot

You’ll see a summary of the code for the command /hello

Now click Run Test Event in the bottom right. A Running... dialog will appear that should complete in a few seconds. Afterwards it’ll look like this:

Run your code right in your browser.

Awesome! If we check our Slack workspace in the #random channel we should see this:

Your Slack Bot is live!

If we want to change the payload (sample data) being sent to our bot, we can click Edit Test Event Payload in the top of the editor.

Edit your Slack Payload…
… and configure the JSON data.

Note: Your bot is not yet deployed! Once you’re happy with functionality, to deploy your bot, simply click the Deploy button in the bottom left of the editor:

Click Deploy in the bottom left…
… and your bot will be deployed!

Your API (webhooks) for your Slack app are now live. To test your newly deployed bot, use /cmd hello from Slack. Note that all commands build via the Autocode app require you to prefix command executions with /cmd command-name instead of just using /command-name. The latter is available via custom apps which require a little more setup.

You can now run Slack commands!

Congratulations! You now have a working Slack bot with a hello command and a member_joined_channel event.

Minute 7: Shipping to Production, Modifying

Click Ship Release

This will open up a Ship Release dialog with three tabs: Secrets, Linked Resources, and API Version. The most important tab here is Linked Resources which is where you can link your production Slack workspace credentials.

Link a Production Slack Workspace

Once you’ve filled out your environment secrets, linked your accounts, and entered an API version you’ll be able to ship. Just click Create Release when ready!

Your release will be created.

We’ve now released your API, which will handle all of your Slack webhooks. Modifying your Slack app from here is simple, just visit autocode.com/manage and edit your Sandbox. You’ll need to deploy to a dev environment and then ship a new release to push new changes to production.

That’s it! Your Slack App is live!

We would love for you to comment here or e-mail me at keith [at] autocode [dot] com, or follow us on Twitter, @AutocodeHQ. Please let us know if you’ve built anything exciting that you would like us to feature or share!

Slack Platform Blog

Where you make work happen