Automating Bagel Bliss with the Twilio API

Rekha Tenjarla
Building The Atlantic
4 min readJul 20, 2018

--

Emojipedia/Youtube

Here at The Atlantic, we take our bagels very seriously. Every Wednesday, members of the product team update a spreadsheet with their bagel choices and every Thursday, our coworker places and picks up the group order from the best no-frills bagel spot in DC. Unfortunately, this process has only become more complicated as our team has grown larger.

The immediate issue is that we have been ordering more jalapeño bagels than the store prepares in a typical day. The bagel shop gave us a simple solution. If we could fax our order before Thursday morning, they would be able to accommodate our spicy sensibilities. As programmers, we could only hear one thing: automation.

The original bagel bot planning session

We put our heads together at the first planning session to think through the component pieces of our bagel bot. First, we would need to read in the bagel order spreadsheet using the Google Sheets API and convert it to a faxable format. Next, using the Twilio API, we would send our fax to the bagel shop. After the fax is successfully sent, we would send out a slack notification to our team using the Slack API. Finally, we’d automate this process using a cron job; a native Unix task scheduling utility that derives its name from the Greek word for time, chronos.

Setting up fax

First, the bot reads in our spreadsheet, using Google’s Sheets API. Then, using pdfkit , a pdf generating Node library, it pipes our document up to a destination on our CDN using a writable stream. After adding content and formatting the document, it sends a fax to the bagel shop using Twilio’s Programmable Fax API.

Integration with Slack

Our async sendFax() function sends out a slack notification to the team’s bagel channel after the fax has been sent . The message contains a link to the location on our CDN where the document lives so that we can verify the order details.

Automating the bot with a cron job

The last piece of the puzzle was automating this task to run every Wednesday night at 10pm. Setting up the cron job was simple enough.

# Run the bagel bot every Wednesday at 10pm
0 22 * * 3 NODE_CONFIG_DIR=/path/to/user/bagel_bot/config /usr/local/bin/node /path/to/user/bagel_bot/index.js

We remembered to include an absolute path to Node, but we ran into trouble on our first attempt because the crontab didn’t know where to look for configuration details. We solved this issue by setting the environment variable NODE_CONFIG_DIR to our config directory path and adding it to the command.

However, like all automated processes, our robot was susceptible to the external world behaving unpredictably. The second week into our new routine, the bagel shop didn’t remember to check their fax and the process broke down. We responded to this by updating the cron job to run at 3pm on Wednesdays, giving them plenty of time to process the order for the following day.

Next Steps

Going forward, we are interested in implementing user-centric features that would improve the experience for both our coworkers and the bagel shop. For example, we might consider getting rid of the spreadsheet altogether and turning it into a fully integrated slack bot that prompts individuals for their bagel choices, so that they don’t have to remember to update the spreadsheet on Wednesdays. We may also consider adding in new notification systems for the bagel shop, so they know when our weekly Wednesday fax has been sent.

As developers on the product team, we are accustomed to employing automated processes to make our lives easier and more productive. We have Slack integrations that send off notifications when pull requests are opened or closed in Github, when our continuous integration pipeline throws an error in Jenkins, or when a developer needs to QA changes on the staging server before a deploy. In the past year, we’ve even implemented a social Slack bot, that pairs together random employees at The Atlantic for lunch. In short, this army of automatons keeps our workplace exciting and organized by allowing us to turn over certain repetitive processes to code. The more we can automate without creating new issues, the more we can control for the unpredictable nature of the world. And that frees us up to think about bigger problems.

--

--