How to Send SMS / Text Messages with Twilio and Ruby on Rails

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up guys this episode Brahimi's talking about sending SMS or text message notifications to your users so this is a great feature that you might need especially if you're building something like appointments you can send a text as a reminder that hey you need to make sure you don't forget your appointment coming up in 24 hours or something like that so Twilio is the service we'll be using for this there's a bunch of other options out there as well but Twilio is just a great one and it's cheap and easy to get set up especially with rubies so let's dive in and create our count so go ahead and create your account then you're gonna look for the programmable SMS inside this menu on the side and under the learn and build section they're going to have a place for you to set up your first phone number so you can create a phone number and for permanent use of Twilio you have to pay for a phone number because you need to be having that phone number reserved for your application and then once you have a trial phone number set up you can send yourself a text message and on the side here it will show you how to do that in code so they have a Twilio Ruby library that you can use you have an account s ID and your auth token and you pass that in along with your message to your whatever phone number that you were sending to and it will print that out and actually send the message to you so this is the code that we want to implement in our app to send out notifications so let's go ahead and dive into building our app so to get started I've already generated a rails application using the jumpstart template and we are going to be adding a phone number to our user model which is generated through device so your existing applications they already have you can just add the phone number to the user model or whatever model you want to send that to so let's generate a migration called add phone to users and we'll just have the phone number as a string and we'll use a couple gems and our rails Apple its first run rails DB migrate and then we can go to our gem file and this is the jumpstart gem file of course but we're going to add a gem called phone Lib and a gem called Twilio Ruby and film lab is gonna allow us to do validations for our phone numbers so the phone lib library is built using Google's Lib phone number library and it basically recreates that inside of Ruby and add some active record in rails integration so you have access to a bunch of different configuration options here but most importantly the active record integration is what we're interested in we can just simply say phone is true that will validate that that string is a phone number and then you have a bunch of other options here like lab link and types and so on so what we're gonna be doing is just adding this validation right here to our user model so let's go to our users and then let's say validates phone as a phone and we are good to go so now we need to run bundle to install those two gems and we want to also run rails credentials edit environment equals development and inside of here we're going to add our Twilio credentials so that we have access to those so Twilio and we'll say account s ID and auth token and you will just go into your Tullio account and paste in those keys here now also you could put in your phone number for your Twilio account in here alongside of your credentials this doesn't really need to be kept in the credentials because it's not private anytime you send a text message from your phone number that's going to be sent along with it so it's not private but it could just be nice to have all these configuration items inside your jet credentials together so let's go back to our application code now that we've added our credentials and let's go to registrations new let's go and div class equals forum group and close div and we're gonna add a field here for our phone number to do that we can say F dot phone field phone and we'll add our bootstrap class with form control on here and you can optionally add placeholders and things like that so let's do that so a placeholder phone number and we're going to need to go into application controller to permit that extra parameter so we already have this set up for device and so on sign up will ask for your name and the account update will also ask for your name and phone so you can edit your registration as well so if we go to registrations edits we could copy that same thing so let's go to registrations new copy the phone registrations edit paste it in under name so now on our signup page we have a space for the phone number which i've already filled out with a fake phone number you want to put in your actual phone number so you can test this out but of course i don't want to put my real phone number in a screencast so i'll change this and save my new user account and to make sure we have device setup correctly let's register for our account and go to settings and verify that our phone number was saved so we can dive into the actual Twilio stuff next alrighty let's go and create a wrapper around the Twilio library just to make it easier to interface in our code with the Twilio api so let's make a directory called app services and inside of there let's create a file called app services Twilio client Darby and we'll define a class in here called Twilio client we'll define an initialize method in here and that will set an instance variable which will be able to access through the adder reader for client and this is where we're going to instantiate the Twilio client and create or pass in those credentials for it so if we open up the Tullio ruby github repo or go look at the code examples for Rubi we can grab the Toledo restclient initializer right here and rather than doing our account s ID or auth token we can actually implement you know maybe a method for that or just pass these in directly so we have rails dot application dot credentials dot Twilio and this will give us a hash so we should be able to say count s ID here and do the same thing for our off token and our phone number so we'll say auth token auth token phone number and phone number now if you want to test this out before you make these private you can just go into the rails console and say Twilio client dot new dot account s ID and that should print out your account s ID from your credentials file now we don't generally need to access those methods anywhere outside of this class we'll go ahead and make those private methods because they're just kind of useful for internal stuff in here and we can add a method called send text and this will send to a user for example and we will send them a message and this is just gonna be pretty straightforward we will go and use this Twilio client dot messages that create we want to send this to user dot phone we want to send it from the phone number and we're going to add in that message and if you ever forget this stuff take a look at the Twilio documentation but you can set up the send them SMS stuff just like so and we probably need to do api account messages this looks to be a little bit different than some of their other documentation so just take a look at whatever's in the rubygem docs and that's probably the most accurate so the last thing we need to set body to that message that we received in the ARBs so let's say body is message and we should be good to go but let me go and change this to an API dot account messages screen and let's open the terminal and see if we can send that text message so here we can say Twilio client new dot send text to user dot first and we'll say hello world from SMS and if we send this we will look up that user and then send a request to the Twilio api and all of that works like we would expected here you can see the body of that was sent from your Twilio trial account so i'm on a Twilio trial and all of my messages will be prefaced with that at the beginning and the rest of this is going to include some information about the message s ID and the products and all of that stuff as well and here we can see that my Google Voice number received a text message from my Twilio account I made a few tests as I was recording and all of those came through on Google Voice just like I would expect it to same thing happens if you send this to a real phone number it'll show up on your phone you can also check the Twilio dashboard to see if your messages are going through just to verify that you'll see the incoming and outgoing listed here on the programmable SMS dashboard and you can also take a look at their debugger as well to help debug your API integration if you're having troubles but for the most part this is extremely easy to get connected and in the next episode we're gonna be talking about actually building out a conversation inside of Twilio so that users can reply to your messages because right now we can send them a text but they can't reply and it won't be processed by our application so we can set up inbound text messages and process those and send responses back in our rails app in the future we'll talk about that in a future episode but it works very similarly to the way that action mailbox works in rails six to process inbound emails into your rails app as well so we'll talk about all that and more in future episodes but until I will talk to you guys in the next one peace [Music]
Info
Channel: GoRails
Views: 5,375
Rating: undefined out of 5
Keywords: Ruby, Ruby on Rails, Rails, Javascript, HTML, CSS, Servers, Databases, Screencast, Tutorial, Rails 6, Action Text, Active Support, Action Mailbox, Webpacker, Active Storage, Active Record, Twilio, Rails SMS, Rails Twilio, rails twilio gem, rails text message
Id: LIKKO_r3MAc
Channel Id: undefined
Length: 11min 7sec (667 seconds)
Published: Wed Jul 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.