How to get your Ruby app up and running with Twilio fast — the Twilio Trial Version

Brendan McIlhenny
4 min readDec 7, 2017

Flatiron’s four month intensive web development program works just like a dojo: every two weeks there’s an assessment to test your skills. If you pass you move on to the next level, if not you stay behind and retake that module. Two failures and you’re out. So when my class was assigned to sit in on Module 2’s presentation and one group integrated not only clean, crisp Bootstrap styling but Twilio’s automatic routing features into their project, I felt both a sense of wonder and fright. How are these guys already using third party app integrations?

So I set off digging around Twilio’s API docs convinced that if they could do it with the same amount of knowledge that I currently have at the end of module 2 so could I.

Aww yEahh

For our project’s current scope (we just learned Rails), an integration with Twilio’s SMS feature makes sense and since I do not want to put in my credit card information to buy a Twilio phone number I took advantage of Twilio’s trial period. It gives each user $15.00 worth of credit to play around. Each successful SMS/Voice Message you send costs around $0.10.

Restrictions to using the Trial:

The biggest caveat of using the Trial version is that a) you can only send text messages to verified numbers and b) in order create a verified number you have to manually add it into the Verified Number section of your Twilio dashboard. This is quite laborious and unless you have a bunch of cell phones sitting around it’s nearly impossible to test multiple phone numbers out to receive your message. Why? Because in order to “verify” a number Twilio requires you to authenticate that number by typing in the authentication code sent to that number. That being said: the regular version of Twilio DOES NOT require you to authenticate each receiving number.

Before we dive into the code, let’s go over what is happening under the hood. Your app makes a POST request to Twilio’s Rest Client API, Twilio makes sure you are who you say you are through token verification and then sends the message of your request, which is stored in the body of the request to the phone number you specified, to the number you specified.

Step 1: Sign up with an account.

You must validate your phone number with Twilio in order to start working with a Trial account

Step 2: Create your project

In your Twilio dashboard create a new project. Choose the option to send SMS Messages then ead back to your dashboard.

Step 3: Grab your Auth and SID keys

Step 4: create a Rails application and require the Twilio-Ruby Gem in your Gemfile. Make sure to Bundle install in the Command Line.

Step 6: Add this code to your Ruby file:

account_sid = “your account sid”
auth_token = “your authorization number”
CLIENT = Twilio::REST::Client.new(account_sid, auth_token)CLIENT.messages.create(
to: “+number”,
from: “+your-twilio-number”,
body: “whatever you want!”
)

From the command line run:

bundle exec ruby <name of your app>.rb

And BOOM! You’re up and running on Twilio!

For our Module 2 project, Jake MacNaughton and I are in the process of building a League tracker app for those competitive people out there looking to see their performance across various lawn sports they play so I thought it would be cool to integrate a Welcome message feature to our on-boarding process.

When a user creates a profile and the profile is valid a function should send a request to this person’s phone number based on what they entered in the profile. Check it out below:

#app/models/player.rbaccount_sid = “your account sid”
auth_token = “your authorization number”
CLIENT = Twilio::REST::Client.new(account_sid, auth_token)def send_twilio_message(user_phone_number)
#byebug
CLIENT.messages.create(
to: "+#{user_phone_number}",
from: "+12677133348",
body: "Welcome to Lawnstar! Massacring your bros in cornhole as of late? Make sure you record those W's!"
)
end

Back in my players_controller.rb I call the send_twilio_message(user_phone_number) method right before the redirect to the user show page in the #create action:

def create
@player = Player.new(player_params)
if @player.valid?
@player.send_twilio_message(some ten digit number)
@player.save
session[:player_id] = @player.id
redirect_to player_path(@player)
else
render ‘new’
end
end

When a user logs in and the user passes the validations they will receive a text message via Twilio’s routing system from our app!

Looking to learn a bit more about Twilio? Check out their handy dandy starter docs here.

--

--

Brendan McIlhenny

Beijing儿, 冲浪er, Full Stack Developer, I’d rather be in New Orleans-er