- Read Tutorial
- Watch Guide Video
In this video, we are going to integrate SMS functionality.
Open sms_tool.rb
, and start by including your Twilio authentication. Here, you're calling your ENV
variables, so make sure it matches what you have in .env
file. Then, you're instantiating the Twilio REST client and passing this information to it. You're storing it all in a local variable called client
.
Next, in the send_sms
method, we are creating a message that includes the number from which the message has to be sent (which is the number associated with Twilio), the number to which it has to be sent and the message that has to be sent.
The code for this functionality is:
# lib/sms_tool.rb module SmsTool account_sid = ENV['TWILIO_ACCOUNT_SID'] auth_token = ENV['TWILIO_AUTH_TOKEN'] @client = Twilio::REST::Client.new account_sid, auth_token def self.send_sms(number:, message:) @client.messages.create( from: ENV['TWILIO_PHONE_NUMBER'], to: "+1#{number}", body: "#{message}" ) end end
A nice part about our Twilio integration is that all the code related to sending a text message is present inside a single file and this means you'll only have to make changes here when something needs to be modified.
Now, let's test it on rails console. Remember, you'll have to use a number that is verified on Twilio. You can test like this:
I configured the system to work with my personal cell phone number, so after running the script the text message is actually sent to my phone, as shown here:
If you're wondering about how we're going to integrate our rake notifications, we're not going to worry about them until the application is live.