使用twilio-ruby gem时接收RequestError

时间:2014-02-12 06:39:24

标签: ruby ruby-on-rails-4 twilio

我尝试使用twilio-ruby gem,但得到了Twilio :: REST :: RequestError。这是什么意思?这是我正在使用的代码:

控制器

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

为什么会产生错误?

1 个答案:

答案 0 :(得分:3)

RequestError意味着我们无法发送短信。这可能意味着您没有国际权限发送到相关号码,或者您尝试使用来电显示您不拥有的电话号码,或者您尝试发送到固定电话,或者任何问题。

以下是如何捕获RequestError并查看附加的错误消息的示例。

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