Build an Emergency Notification Slack Bot in 10 Minutes with MessageBird, StdLib, and Node.js
Slack bots can be a fun way for you to add functionality into your Slack workspace. Using StdLib you can easily connect APIs to your Slack Bot to create some amazing functionality — like using the MessageBird API to text messages.
In just 10 minutes we will teach you how to build a simple Slack bot that can send a text message to a number using a simple Slack command.
If you’re not familiar with StdLib yet, you’re in for a treat! We’re a serverless API development and publishing platform that can help you build and ship custom logic in record time.
What You’ll Need Beforehand
- 1x Slack Team
- 1x Command Line Terminal
- 10x Minutes (or 600x Seconds)
- US Cell Phone Number
But first…Let’s talk about Slash Commands.
What Is a Slash Command?
In Slack, slash commands make your users feel powerful. Simply type a slash command in the message field to perform your task in one easy step.
Slack already has a bunch of in built commands. Check them out here.
Two of my favorites are
/me (display italicized action text, e.g.
/me does a dance will display as “does a dance”) and
/shrugs (Appends ¯\_(ツ)_/¯ to the end of your message). Another very useful one is
/dnd which will (start or end a do not disturb session)
With the do not disturb session Slack will automatically disable notifications and it will be harder to reach you. But what happens when you want to contact someone who has it enabled? This is where an emergency notification bot comes in handy!
In just a few minutes we will teach you how to build an emergency notification bot.
Minute 1: Preparing Your New Slack App
After making sure you are signed in to Slack, visit your Slack Apps page at https://api.slack.com/apps. You’ll see a screen that looks like the following, depending on whether you have an existing apps.
Simply click Create New App to create your app. You’ll be presented with a modal to enter in your App Name. I suggest DisturbBot since this application will be used to “disturb” members who may not want to be disturbed.
From here, click Create App.
After that find the Bot Users option on the left sidebar, under the Features heading.
Click Add a Bot User to create your bot. You’ll be given an option to enter in the bot username. Click Add Bot User to complete the process, and that’s it! Your bot user is now added and ready to be used.
Minute 2: Send Messages Using the MessageBird API on StdLib
With MessageBird API on StdLib, you can enjoy seamlessly adding SMS functionality (for customer communication, two-factor authentication and more) into your app using the world’s fastest global communication platform, MessageBird, with a simple, functional API on StdLib. But first you’ll need to do a few steps to initialize your first number.
Visit the MessageBird’s Profile on StdLib
Visit https://stdlib.com/@messagebird in your browser. You should see a profile page that looks like the following:
In order to get started, the first step is to initialize your first phone number, but first we’ll have to agree to the MessageBird terms of service. To do this, find the messagebird.numbers API on the right hand side of the screen and click on the name.
Accept the Terms of Service for MessageBird APIs
Great, you found your way around StdLib profiles! (You even have your own generated when you create an account.) Now it’s time to initialize your first phone number, but first we’ll need to accept MessageBird’s Terms of Service (ToS). You should be on a page that looks like the following:
This is the messagebird.numbers API reference page. If you can’t find this page, simply click here https://stdlib.com/@messagebird/lib/numbers/# to visit it.
From here, we can see a list of available API methods. (These are just things you can do with phone numbers!) Before we get started, you’ll notice a big orange notification (see above) indicating you need to accept a Terms of Service in order to use this API.
Follow the on-screen instructions to accept. You should see a screen that asks you to claim a StdLib namespace (or if you already have one you can click “Already Registered”) and login there.
When complete, you should see the following on the API page:
Great! Now we can use all of MessageBird’s APIs. Let’s initialize that first number!
Minute 3: Initialize Your First Phone Number
Awesome. We’ve now accepted the MessageBird API’s ToS and we have a StdLib account to boot! Please make sure you’re on the numbers API Reference page before continuing: https://stdlib.com/@messagebird/lib/numbers/#available
What you’re looking for is the available method. You can find it by scrolling down the page, or clicking available from the sidebar on the left.
From here, we can see a code example and a big green button on the right that says Run Function. See the dropdown to the right of the “Run Function” button? There first thing we’ll want to do is select a Library Token. If you’re logged in (you should be!), clicking on the dropdown should yield something like this:
Select a Library Token, and hit the Run Function button. You’ll be greeted with an output like the following:
Don’t lose this! Pick a number that you like (out of the ones shown), and copy it to your clipboard. Now you’ll want to go back to the numbers.initialize API and paste your number the the
number field in the documentation.
If you can’t find the numbers.initialize API, simply click here: https://stdlib.com/@messagebird/lib/numbers/#initialize.
Enter in your chosen number from the result of the numbers.available API above. Click Run Function, ensuring that the correct Library Token is selected from the Dropdown on the right (unauthenticated won’t work!).
Congratulations! You have now received your first FREE number with the MessageBird API on StdLib. Additional numbers can be claimed for $0.99 from the numbers.claim API.
If you’d like — you can try sending a message using the messagebird.sms API. Click here: https://stdlib.com/@messagebird/lib/sms/ and fill in the recipient (the number you want to send the message to) and the body (the text message you want to send) choose your Library Token and then run the function.
If everything is set up correctly the specified recipient should receive a text message. Congratulations! You have just sent your first text message using StdLib and MessageBird.
Minute 4: Create a StdLib Workspace
Open up your Terminal or Command Line and install the StdLib Command Line Tools with the following:
$ npm install lib.cli -g
This gives you access to the
lib command for service management and execution. Next, create a
stdlib directory for your StdLib services.
$ mkdir stdlib
$ cd stdlib
$ lib init
You’ll be asked to log in using the credentials you created your account with when signing up for the MessageBird TOS. That’s it!
You’ll now want to create a StdLib service for your Slack App. You can use the Slack source code to get a bot up and running with very little effort.
stdlib directory you just created, type:
$ lib create -s @slack/app
You’ll be asked to enter a
Service Name, we recommend
Minute 5: Add Your Slack App Credentials
env.json (environment variables) under
<username>/disturbBotService in a text editor of your choice.
We’ll be making modifications to the “local” and “dev” environment variables — make sure you’re modifying the right set!
First, fetch your StdLib Token from your StdLib Dashboard.
This is your StdLib library token. Click Show token to see its value and copy and paste it to your
env.json file under
STDLIB_TOKEN in both the “dev” and “local” environment variables. Dev values are for your dev environment and release values should only be populated when releasing your application.
Next, fill out
SLACK_APP_NAME, we recommend “Disturb Bot Test App”.
Finally, go back to your Slack App, and scroll down on the Basic Information panel:
Copy each of these values to
dev section of
As a last step, modify
<username> is your StdLib username.
Minute 6: Create Your Command File
To add or modify Slash commands, you’ll want to look in your StdLib Slack Source Code directory under
<username>/disturbBotService/ functions/commands and create files with the name
NAME is your intended command.
Since I suggested
/disturb as your command name, I also suggest you name your file:
Fire up your favorite editor and copy the code below into that file.
In your project directory where your
package.json file is run:
$ npm i -S libphonenumber-js
This will install the npm module
libphonenumber-js which is a really useful npm package that helps you parse phone numbers.
To make sure your slack-app is ready, let’s run a test disturb command locally. Run this command below in your terminal (of course substituting the +11234567890 with the number of your choice).
$ lib .commands.disturb test general "+11234567890 Wake up"
The command emulates a @ test user executing
/disturb +11234567890 Wake up in the channel #general.
If everything is setup correctly it should send the message “Wake up!” to the number specified.
Minute 7: Enable OAuth
Next, we’ll enable OAuth. On the sidebar menu, click OAuth & Permissions. Once there, you’ll want to enter in a Redirect URL as follows:
https://<username>.lib.id/disturbBotService@dev/auth/ where <username> is your StdLib username.
Minute 8: Create a New Slash Command
The first thing we will need to do is create our new
Make sure you’re in your Slack App settings before continuing and then click Slash Commands on the sidebar.
After clicking Create New Command, you’ll be asked to enter some command details, here’s what I recommend.
- Request URL:
- Short Description:
Sends a text to someone even if they are in dnd mode
- Usage Hint:
[phone] [message] e.g. 415-123-4567 Wake up!
Slack has a great style guide you can use as a reference when naming commands.
Minute 9: Deploy Your Function to the Cloud
Deploying your function to the cloud can be done in one command
$ lib up dev
And just like that you have a simple Slack Bot that allows you to send text messages to people who may not want to be disturbed.
Once complete, visit
https://<username>.lib.id/disturbBotService@dev/ in your web browser — it should be available to be copy-and-pasted from your command line output. You’ll see a very simple “Add to Slack” button.
Click this button, and accept the requested permissions (we set them up previously), you’ll have to scroll down and click Authorize.
You’ll be returned to your specified auth callback, which should give a success message:
Minute 10: Playing With Your New Slack App
You’re all done. Try it out!
You should be able to send commands in the following format
/disturb [phone-number] [message] This should send a emergency notification to the number specified.
Check our website StdLib.com to join our Slack channel where you can let us know if you need any help and share with us what you are building next! Follow us on Twitter for more content and updates, @StdLibHQ.
StdLib is your new API development solution, built atop serverless architecture for fast deployments and infinite scalability. To stay informed with the latest platform updates, please follow us on Twitter, @StdLibHQ, or if you’re interested in joining the team — we’re hiring! Shoot us an e-mail or resume to email@example.com.