TayyabZahid
TayyabZahid

Reputation: 187

Receiving RequestError when using the twilio-ruby gem

I am try to use the twilio-ruby gem, but got a Twilio::REST::RequestError. What does this mean? Here's the code I'm using:

Controller

Class UserController < ApplicationController

    def new
        @user = User.new
    end

    def createUser
        @user = User.new(user_params)
        if @user.save
            render text: "Thank you! You will receive sms notification"
            
            account_sid = '*****' 
            auth_token = '*****'

            @client = Twilio::REST::Client.new account_sid, auth_token

            #@client = Twilio::REST::Client.new account_sid, auth_token
            #client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])
            # Create and send an SMS message
            
            @client.account.messages.create
            ({
                :from => '+127*****',
                :to => @user.phone,
                :body => "Hello"    
            })
            

        else
            render 'new'
        end
    end


    private

    def user_params
        params.require(:user).permit(:name, :email, :phone)
    end

end

Why is this generating an error?

Upvotes: 1

Views: 750

Answers (1)

Kevin Burke
Kevin Burke

Reputation: 64844

A RequestError means that we couldn't send the SMS message. It may mean you don't have international permissions to send to the number in question, or you are trying to use a Caller ID for a phone number that you don't own, or you're trying to send to a landline, or any number of problems.

Here is an example of how to catch a RequestError and view the attached error message.

require 'twilio-ruby'

begin
    client = Twilio::REST::Client.new account_sid, auth_token
    client.account.sms.messages.create(
      from => from_number,
      to =>   to_number,
      body => "Hello World"
    )
rescue Twilio::REST::RequestError => e
    puts e.message
end

Upvotes: 3

Related Questions