Reputation:
I'm using RoR to create a web app that sends a text message to a phone number, using the Twilio API. Because I don't know what I'm doing, I'm following the instructions here: https://www.sitepoint.com/adding-sms-capabilities-to-your-rails-app/ which are super great, but also written 4 years ago. I noticed that Twilio uses a different resource URI (Messages vs SMS/Messages), and I've tried to adjust the code to reflect this update, but I think I'm still missing something, because I'm not receiving any text messages. And yes, the phone number I'm using is 100% verified. Even more perplexing, neither my console nor the Twilio SMS logs are giving me any error messages. The console output looks something like this:
(42.6ms) commit transaction Rendered text template (0.0ms) Completed 200 OK in 710ms (Views: 0.4ms | ActiveRecord: 42.9ms)
which looks pretty chill to me. And there's not even an entry in the log. Here's what my controller looks like:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
render text: "Thank you! You will receive an SMS shortly with verification instructions."
# Instantiate a Twilio client
client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])
# Create and send an SMS message
client.account.messages.create(
from: TWILIO_CONFIG['from'],
to: @user.phone,
body: "Thanks for signing up. To verify your account, please reply HELLO to this message."
)
else
render :new
end
end
end
Specifically, I tried changing client.account.messages.create()
from client.account.sms.messages.create()
, to no avail. I also tried changing it to client.account.sms.messages.sendMessage()
, but I got an error telling me the method was undefined.
Anywayyyy, can anyone give me a hint as to how to troubleshoot? This is the only piece of code I've changed, so maybe I have to alter something else?
Thanks in advance. :)
P.S. Why do the end
s at the end of my code look so screwy? They don't look like that in Sublime.
Upvotes: 1
Views: 326
Reputation:
I just wanted to follow up on this - I authored the question. It turns out that you can't test in this manner with Twilio's test credentials. As outlined in the Twilio documentation, https://support.twilio.com/hc/en-us/articles/223183808-Why-aren-t-my-test-credentials-working-:
Although Twilio will provide test credentials for users to exercise parts of the Twilio REST API, these cannot be used to send messages or make a phone call. This is why my app worked when I used production credentials. I wasn't charged for using these production credentials (I think you must get a couple of freebies).
For the record, the code I followed from that tutorial is correct, with the exception of the deprecated .sms endpoint mentioned in the original question. That did have to be altered.
Upvotes: 0
Reputation: 3811
Kira,
Take a look at Why aren't my test credentials working? regarding your update.
With the proper credentials, I'd suggest trying an updated tutorial SMS notifications in Ruby and Rails where the send_message
function looks like this:
def send_message(phone_number, alert_message, image_url)
@twilio_number = ENV['TWILIO_NUMBER']
@client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
message = @client.account.messages.create(
:from => @twilio_number,
:to => phone_number,
:body => alert_message,
# US phone numbers can make use of an image as well.
# :media_url => image_url
)
puts message.to
end
Take a look at the full code in the tutorial and let me know if it helps at all.
Upvotes: 1
Reputation: 2510
try this:
paste it into your controller
def twilio_client
Twilio::REST::Client.new(ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN'])
end
def send_otp
twilio_client.messages.create(to: 'phone_number', from: 'twilio_phone_no', body: "temporary identification code when prompted." )
end
set from field as per your twilio account and set to fild to client phone no "+4563217892" and call controller send_otp method.
for more information check this link:
http://rubyonrailsdocs.blogspot.com/2016/05/twilio-application-step-by-step-process.html
Upvotes: 1
Reputation: 5633
what happens if you do something like:
#config/initializers/twilio.rb
#move your TWILIO_CONFIG initialization here
TwilioClient = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])
#models/user.rb
class User
after_create :send_verification_message #best done with a background worker
def send_verification_message
TwilioClient.account.messages.create(
from: TWILIO_CONFIG['from'],
to: phone,
body: "Thanks for signing up. To verify your account, please reply HELLO to this message."
)
end
end
end
You can then remove the one defined in the controller. I believe with this, you should either be able to send the messages or see the errors returned
Upvotes: 1