Using Twilio and Slack to make childbirth less unpredictable
Or: “How I stopped worrying and learned to love the bot”
Apr 01, 2019

How can expecting parents use technology to facilitate better communication? Here, an Asymmetrik Software Engineer explains how Twilio and Slack can help.

My wife is nine months pregnant with our first child; the baby could come any day now. While it’s an exciting time, anyone who has been in this situation knows that the last couple of weeks are an unpredictable waiting game. On my end, I have to be prepared to drop everything immediately once my wife goes into labor. But, what if my phone’s battery dies, or I leave it in the car, or drop it in the toilet? How can she get in touch if she needs me right now?

To solve this problem, I decided to turn to technology and build a Baby Bot. And in case you find yourself in the same situation at some point, here’s how you can build one too.

A Twilio-powered Slack Baby Bot

Slack is great. Our team at Asymmetrik uses it every day to communicate quickly and easily. We’ve even set it up to push phone alerts for important conversations. But, it doesn’t let someone who isn’t an employee, such as my wife, push messages to it.

This is where Twilio comes in. First, I purchased a Twilio phone number that can accept incoming SMS messages.

Purchase a Twilio phone number than can accept SMS

Then I created a Twilio Studio workflow. This is a convenient drag-and-drop interface where you can configure components and behaviors to respond to incoming phone calls and text messages.

Since I’ll be sending the message to Slack, I’ll need to trigger an HTTP Request component when a text message arrives.

Add an HTTP Request component to an incoming SMS message in Twilio Studio

Now it’s time to set up a Slack webhook, in order to have somewhere to post to. Slack lets you configure the icon it will use, the default message, and which channel it will post to. But, you can also specify each of these things in the message itself. You can find Slack’s full documentation on webhooks here.

Create a Slack webhook

Now let’s go back to Twilio and fill in the details of that HTTP request. Give the widget a name, point it to the webhook URL from Slack, and then setup a message payload. This should be valid JSON object.

Configure the Twilio Studio flow to call the Slack webhook

Twilio Studio supports a robust way to pass variables through the workflow.  For instance, I could have decided to pass through whatever text message my wife had typed, by embedding {{trigger.message.body}} in the message payload.  However, in this case I opted for a simple fixed message, because perhaps what she will try to write to me will be rather personal…

Click the Publish button when you’re ready to some testing. Finally, go back and configure the phone number we bought to point to this Studio flow.

Configure the Twilio phone number to point to the Studio flow

Great, we’re ready to do some testing! I sent a text to the phone number, switched back over to Slack, and nothing happened.

Troubleshooting

Technology never works quite how you expect it to the first time. The first message I sent never made it to Slack and I needed to dig into Twilio’s logs to figure out why. Fortunately, Twilio has a great debugging console that shows all the errors it encountered, what the payloads were, and what errors were returned. Take a look at this request:

Inspect each error using the Twilio debugger

It turns out, I forgot to wrap the JSON keys in double quotes. Slack is expecting a valid JSON payload that is properly formatted. Here’s what the Application/JSON payload should look like:

{ "text": "@channel Andrew's wife is in labor.  Please help her by finding him now!" }

I went back and fixed the Twilio flow, published it again, sent a new text message, and bam!

The message appears in Slack, and Andrew's coworkers go find him.

Of course, then my colleagues came and found me at my desk and offered their congratulations. Oh yeah, I had just built my own emergency alert system. I sent a follow-up post to explain this was only a test…

What’s next?

After confirming that the Slack notifications worked, I set out to build a phone tree so that my colleagues would also receive texts on their phones. I had big plans to have it call each person in turn until someone responded, and then to hook up some IoT devices in the offices to flash some lights, play a recorded message on the Sonos speakers throughout the office, and post a message on the Chromecast TVs.

Then my wife texted to tell me that she had written my coworker’s phone numbers on the fridge, and that was all she needed. A good old-fashioned phone tree. Right. Sigh…