How to send SMS Messages in Rails with Twilio Discussion
Great intro! We use Twilio to send SMS notifications to our users.
Question - I set up the Twilio client in an initializer. Is there any advantage to this over setting it up in the Twilio service object? I know global variables are generally frowned upon but they seem ok for clients like this.
# config/initializers/twilio.rb require 'twilio-ruby' Twilio.configure do |config| config.account_sid = ENV['TWILIO_ACCOUNT_SID'] config.auth_token = ENV['TWILIO_AUTH_TOKEN'] end $twilio = Twilio::REST::Client.new
And thanks for the tip on the Google phone number gem. I've been using Twilio's Lookup REST API to validate phone numbers. It's likely more accurate but makes an API for every validation. Not so great. I think I'll be switching to
phonelib soon 👍
when i run this
- rails credentials:edit --environment=development No $EDITOR to open file in. Assign one like this:
EDITOR="mate --wait" bin/rails credentials:edit
For editors that fork and exit immediately, it's important to pass a wait flag,
otherwise the credentials will be saved immediately with no chance to edit.
Interesting curve on the road to sending a Twilio SMS. Running the command as per the
send_text method suggested, ran the first time from localhost.
But somehow, after adjusting the method to be able to handle the API response, Rails 5 complains with:
ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true) I am certain the method is firing (because when the conditions are not met, the console prompt returns to a ready state & testing without the added line generates the SMS).
status_callback: update_twilio_reminder_url(reminder.user_id),to the method is hanging because it needs a defaulturl option. But which? certainly not action_mailer's?
config.application.routes.default_url_options? On this point, the rails guide is as opaque as the error message...