Pavan
Pavan

Reputation: 33542

Can't receive SMS using Twilio trial account

Everything was working great few hours back before this error pops up and now I can't receive SMS. Below is the log

Started POST "/phone_numbers" for 127.0.0.1 at 2015-08-28 20:24:38 +0530
Processing by PhoneNumbersController#create as JS
  Parameters: {"utf8"=>"✓", "phone_number"=>{"phone_number"=>"+919738437250", "user_id"=>"9"}, "commit"=>"Send PIN"}
  PhoneNumber Load (0.4ms)  SELECT  "phone_numbers".* FROM "phone_numbers" WHERE "phone_numbers"."phone_number" = $1 AND "phone_numbers"."user_id" = $2 LIMIT 1  [["phone_number", "+919738437250"], ["user_id", 9]]
   (0.1ms)  BEGIN
  SQL (0.2ms)  UPDATE "phone_numbers" SET "pin" = $1, "updated_at" = $2 WHERE "phone_numbers"."id" = $3  [["pin", "4143"], ["updated_at", "2015-08-28 14:54:38.800401"], ["id", 51]]
   (99.4ms)  COMMIT
Completed 500 Internal Server Error in 4519ms (ActiveRecord: 100.1ms)

Twilio::REST::RequestError - The requested resource /2010-04-01/Accounts//Messages.json was not found:
  twilio-ruby (4.2.1) lib/twilio-ruby/rest/base_client.rb:124:in `connect_and_send'
  twilio-ruby (4.2.1) lib/twilio-ruby/rest/base_client.rb:54:in `block (2 levels) in <class:BaseClient>'
  twilio-ruby (4.2.1) lib/twilio-ruby/rest/list_resource.rb:95:in `create'
   () home/pavan/Roorah/app/controllers/phone_numbers_controller.rb:14:in `create'

Below is my code.

#config/initializers/twilio.rb
path = File.join(Rails.root, "config/twilio.yml")
TWILIO_CONFIG = YAML.load(File.read(path))[Rails.env] || {'sid' => '', 'from' => '', 'token' => ''}

#config/twilio.yml
development:
from: '+1xxxxxxxxxx'
sid: 'Axxxxxxxxxxxxxxxxxxxxxxxxxe0'
token: '5xxxxxxxxxxxxxxxxxxxxxxxxxa0'

#app/controllers/phone_numbers_controller.rb
class PhoneNumbersController < ApplicationController

  def new
    @phone_number = PhoneNumber.new
  end

  def create
    @phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number], user_id: params[:phone_number][:user_id])
    @phone_number.generate_pin
  # Instantiate a Twilio client
  client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])

    # Create and send an SMS message
    client.messages.create(
      from: TWILIO_CONFIG['from'],
      to: @phone_number.phone_number,
      body: "Enter #{@phone_number.pin} to verify your phone number"
      )
    respond_to do |format|
    format.js # render app/views/phone_numbers/create.js.erb
  end
end

def verify
  @phone_number = PhoneNumber.find_by(phone_number: params[:hidden_phone_number])
  @phone_number.verify(params[:pin])
  @user = User.find(params[:user_id])
  @user.update(primary_phone: params[:hidden_phone_number], verified: true) if @phone_number.verify(params[:pin])
  respond_to do |format|
    format.js
  end
end
end

What could be the issue?

Upvotes: 1

Views: 279

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

It looks like where you are loading your config with YAML the Account SID is dropping back to the defaults, empty strings. That's why the URL the stack trace is showing up without your Account SID in:

Twilio::REST::RequestError - The requested resource /2010-04-01/Accounts//Messages.json was not found:

Check your config and how you're loading the YAML and what the value of TWILIO_CONFIG['sid'] is.

Also, an easier way to load the config might be with Rails's built in config_for helper. Check out the documentation here: http://api.rubyonrails.org/classes/Rails/Application.html#method-i-config_for

Upvotes: 1

Related Questions