The Awe Inspiring Magic of Twilio

Gabriel Chazanov
3 min readJun 7, 2021

As a budding software engineer, all the apps that I’ve built have been largely self contained. Front end systems interacting with backend. Models speaking to each other. Essentially a closed circuit. There is a way though, for an app you create, to reach its digital tentacles beyond the confines of the virtual kingdom you’ve created. Namely, automated messaging. Through, you’ve guessed it, Twilio. Twilio is a service that lets you send SMS text messages and voice memos. What’s so great about Twilio is that users can programmatically send out these messages based on whatever parameters you code into your app. Let’s say you are building a board game app, where users can play asynchronously. With Twilio, you’d be able to send a message to a user automatically when it was their turn. The possibilities are endless, but what’s most exciting about Twilio to me is the fact that when you integrate aspects of your app into a users every day life, the app takes on a life of its own that grows larger than the screen on which you programmed it.

Today I’ll be going over how to get started with Twilio inside of a Ruby app, and a couple of its use cases. To get started, you need a Twilio account. A trial account is free, and it comes with fifteen dollars worth of messaging to play around with. If you want to upgrade to a full Twilio account, you need to connect you account to a credit card. This can be a dangerous game to play as your account will be charged for every message your app sends, and it can build up very quickly depending on how you’ve programmed your app.

Once you’ve set up your account, you will also be prompted to pick a phone number. This is the number that people will see when you app messages them. Now that you have your account set up, you’ll want to download the Twilio code library onto your app. Very simply, all you have to do is enter this command into the CLI.

gem install twilio-ruby

Once that’s done, there’s just one more step to give you access to all of Twilio’s code. At the top of the file that you’ll be building the Twilio functionality into, put in your require line, like so:

require 'twilio-ruby'

From here you’re going to want to define some variables that have your Twilio credentials. If you’re still in development of your app, and have yet to deploy, you can put these values in like any old variable. Once you’re ready to deploy though, it’s advisable to hide these values in ENVs, lest people download your killer app and use your Twilio credentials as their own.

Once you’ve defined an auth token and an account ID, you’ll instantiate an instance of the Twilio client using the aforementioned credential variables as paarameters, like so:

client = Twilio::REST::Client.new((your account ID, (your auth token))

The final two ingredients to setting up your first SMS message is to define variables that will contain the telephone numbers that will be sending and receiving the messages. The sender will be the phone number that Twilio provided, but the receiving number is where you have the opportunity to create a programmatically defined recipient based on whatever is going in your app.

The actual code block that will send the message will take the variable that you defined for the Twilio client and pass in the receiving and sending number, and a string as the parameters. Like so:

client.messages.create(
from: (Your number),
to: (The number you are sending the message to,
body: "This is a message!"
)

The body of the message can also be programmatically defined using string interpolation to send a specific message based on whatever is going in your program at the time. And there you have it! Define your credential variables, create a client variable, and pass in the proscribed code block into a method that will be able to dynamically accept arguments for recipient and body, and before you know it, you’ve got an app that’s able to touch the world.

--

--

Gabriel Chazanov

He/Him; Full Stack software developer who’s always striving to learn