Use Twilio to Communicate from Your App 📞

We will use Twilio to send SMS Text Messages from our app using pure Ruby and explore the possibilities of Twilio using Rails.

Renée Cruz
Sep 12, 2019 · 7 min read

Twilio is an app that connects your computer to phone lines. This means that we can build apps that can call, text with our code! That’s incredible and frankly, I’m thrilled because this is freaking awesome.


Communication. So fun!

A Brief History & Some Big Picture Musings

Communication. Words, gestures, written, aural…the humans have been doing it by any means necessary, sending and receiving messages, using whatever medium 😉 is available, for a long time. Hence, the message and the medium are intrinsically linked. The medium influences the message and cultures form, ideas can reference themselves. Memes! But I digress. So whether the message is transmitted or received via a cave wall, the clouds, a letter, a carrier pigeon, a telegraph, a speech, a phone, a beeper, an email, a time-capsule, a satellite, a fossil, binary code, Morse Code, programming languages, poetry or a bunch of quartz crystals, bits & atoms, made into a silicon circuit board and sending messages at the speed of light…its all the same idea. Communication! It’s transmitting ideas and information, be it sounds or light, organized in many different ways. So basic and yet, so profound!


A link to a video entitled Admiral Grace Hopper Explains the Nanosecond.

Checkout out this great article on the topic of communication technology in her television appearance decades later, in which she illustrates the connection between time & space, bits & atoms. “In this 1986 interview with David Letterman, Grace Hopper displays her grace and wit, and explains the concept of a nanosecond, using Bell System telephone wire cut into 30cm lengths — 30cm is the maximum distance light can travel in a billionth of a second. Here’s a representative quote:”

“When an admiral asks you why it takes so damn long to send a message via satellite, you point out to him that between here and the satellite there are a very large number of nanoseconds. [Waves the wire at the sky.]”


And who knows what the future holds. Physicists at MIT, Massachusetts Institute of Technology, have recently discovered what they call a Time Crystal, which they describe as:

“new quantum electronic material with an atomic structure resembling a Japanese basketweaving pattern, “kagome metal” exhibits exotic, quantum behavior.”

And they say, (I paraphrase) that these Time Crystals will be kinda like cyberpunk animes basically, storing memories on the hard drives and preserving consciousness of humanity. Cool.

Remember The Matrix? The phone lines?! See where I’m going with this? Anyway, don’t be scared, we’re coming back to some

Gif of The Matrix movie, Morpheus presenting The Red Pill or The Blue Pill option.

Okay. If you’re thinking that sounds kinda weird, you’re right. Don’t worry, we won’t go too deep into the matrix on this one. We’re gonna get to the code.


Enter Twilio! Let’s get practical.

So how can we do this from within and then beyond the matrix of our apps? Communicate in real-time or close to it?


Twilio is an app that connects your computer to phone lines. This means that we can build apps that can call, text with our code! That’s incredible and frankly, I’m thrilled because this is freaking awesome. Obviously.


We can use Twilio to do so many fun things using telecommunications and the internet! Today we’re going to explore using Twilio with pure Ruby to send SMS messages from our code! There’s also incredible documentation and tutorials about how to incorporate SMS into your apps using different programming languages and frameworks, for example, scheduling appointments using Ruby on Rails.

Also of note, Twilio acquired Authey, the Two-Factor Authentication service and has tutorials on implementing the Authy API in your Rails apps and so many fun resources using Ruby, Rails & Active Record with Twilio!

Programmable SMS

Twilio makes sending and receiving SMS easy. Find the documentation, sample code, and developer tools you need to build exactly what you want, fast. We’ll handle the complexity of mobile carrier and global regulations. Let’s get building.



What’s Twilio?

According to Wikipedia,

“Twilio is a cloud communications platform as a service (CPaaS) company based in San Francisco, California. Twilio allows software developers programmatically to make and receive phone calls, send and receive text messages, and perform other communication functions using its web service APIs.


Twilio uses Amazon Web Services to host telephony infrastructure and provide connectivity between HTTP and the public switched telephone network (PSTN) through its APIs.[26]


Twilio follows a set of architectural design principles to protect against unexpected outages and received praise for staying online during the widespread Amazon Web Services outage in April 2011.[27]


Twilio supports the development of open-source software and regularly makes contributions to the open-source community. In June 2010 Twilio launched OpenVBX, an open-source product that lets business users configure phone numbers to receive and route phone calls.[28] One month later, Twilio engineer Kyle Conroy released Stashboard, an open-source status dashboard written in the Python programming language that any API or software service can use to display whether their service is functioning properly.[29] Twilio also sponsors Localtunnel, created by now ex-Twilio engineer Jeff Lindsay, which enables software developers to expose their local development environment to the public internet from behind a NAT.[30]”




I don’t know about you, but I”m so grateful for the “fact that basically everything we do every day on the Internet and take for granted is possible because someone did a bunch of really hard work for us.”, as Adda Birner, CEO, and founder of Skillcrush, points out in her educational courses on the topic. We stand on the shoulders of giants! I love it. Yay 💖

This is so evident in Twilio.

Quoting Adda Birnir again, “Twilio is a company that makes it possible to call phones via the Internet.”

She goes on to point out:

“I know this seems really straightforward, but let’s think about this for a second…when I call my mom on my cell phone…am I calling her over the Internet?”

“Nope! I am using the cell phone network!”

“Now, granted, we are now able to use cell networks to access the Internet, but that wasn’t always the case! And what about landlines?”

“Where do I type in the URL?”

“The point here is that it takes a bunch of cool technology to allow me to use my COMPUTER to call you and Twilio makes that technology available to you!

Twilio Use Cases in the Wild

  • Airbnb uses Twilio to allow people to call one another about their rooms, without revealing either person’s phone number!
  • Intuit uses it Twilio to send users confirmation codes via text message
  • John Keefe at WNYC used Twilio to allow Brooklyn bus riders to find out how soon there bus would be at their stop
  • Turbo Vote let users set up an text message reminder to go out and vote!
  • GroupMe used the Twilio API to build their group messaging service!

How to Implement Twilio in pure Ruby

  1. First, head over to the Twilio site and create an account. You’ll need to verify that your humanity by having Twilio text you or call you with a verification number.
  2. Either use the number Twilio generates for you, or select a different one.
  3. On your Twilio dashboard, there are two important numbers you need in order to connect to Twilio’s API: the account SID and the auth token (you can see the auth token by clicking the lock icon). You’ll be using the API to do all your texting in your script.
  4. Check out the Twilio API docs to get lots of ideas and inspiration.
  5. Next, install the Twilio Ruby gem. In your terminal, type the following command and hit enter:
gem install twilio-ruby

gem install twilio-ruby

Wait for it. Not too long.

6. Read up on how to use the gem on the repository’s Wiki page.

7. Start your script by using Twilio to text yourself messages so you can get a feel for how it works. The first thing you need to do in your script is require the gem:

require ‘twilio-ruby’ require ‘rubygems’

require ‘twilio-ruby’

require ‘rubygems’

8. Next, connect to the Twilio API using the account SID and auth token on your Twilio dashboard:

account_sid = “your account sid” auth_token = “your auth token” @client = Twilio::REST::Client.new(account_sid, auth_token)

account_sid = “your account sid”

auth_token = “your auth token”

@client = Twilio::REST::Client.new(account_sid, auth_token)

9. Now, text yourself a message. You’ll need to use the .create and .to methods to create the message and then send it:

message = @client.api.account.messages.create(

message = @client.api.account.messages.create(
:from => “+your Twilio number”,
:to =>”+your own number”,
:body => “Haaaii!”
)

puts message.to


And there you have it! 📞 ✨

So. Much. Fun!


Next up, explore the docs to use Ruby on Rails with Twilio to integrate appointment scheduling SMS reminders! The docs are truly excellent; well-written, clear and concise.


In addition, there are a plethora of beautifully designed tutorials to help programmers of all languages and levels implement cloud communications in their apps and projects!


While you’re exploring the Twilio docs, the following Active Record and Ruby on Rails docs and Ruby on Rails topics by my lovely cohort mates at The Flatiron School might come in handy!

Explicit & Custom Routes in Rails

Files and Naming Conventions in Rails

Using Ruby for Game Development

Sorting Algorithms with Bubble Sort


I hope you’ve enjoyed and feel free to hit me up 📞 with any fun things use build with Twilio! ☕️

Renée Cruz

Written by

Full-Stack Web Developer | Software Engineer && Musician https://reneecruz.github.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade