1

我正在构建一个简单的 Web 应用程序,它使用 Twilio 将 SMS 消息发送到手机。我想确保用户在允许尝试发送消息之前输入了完整的 10 位电话号码。

当我用小于或大于 10 位的数字对其进行测试时,在 中heroku logs,我看到了Twilio::REST::RequestError (The 'To' number 1234567890 is not a valid phone number.)

我尝试使用begin/rescue包装器并告诉它render text: "Try again with a valid number."并尝试了各种if语句来避免错误。

我对 Ruby、Rails 和 Twilio 还很陌生,但我保证我已经阅读了我找到的所有指南。任何帮助是极大的赞赏。我的完整代码UserController如下:

    require 'twilio-ruby'
    class UsersController < ApplicationController

    def new
      @user = User.new
    end

    def create
      @user = User.new(params[:user])

      account_sid = '...'
      auth_token = '...'

      if @user.save
         render text: "Wasn't that fun? Hit the back button in your browser to give it another go!"
    begin
       client = Twilio::REST::Client.new account_sid, auth_token
           client.account.sms.messages.create(
           from: '+16035093259',
           to: @user.phone,
           body: @user.message
           )
        rescue Twilio::REST::RequestError
           render text: "Try again with a valid number."        
        end
     else
       render :new
     end
  end

end
4

1 回答 1

3

我会将 SMS 发送逻辑提取到单独的模型/控制器中,并使用后台作业来处理提交。UserController 应该只处理用户创建/修改。

脚手架:

$ rails g model sms_job user:references message:text phone submitted_at:datetime
$ rake db:migrate

模型:

class SmsJob < AR::Base
  attr_accessible :user_id, :message, :phone

  belongs_to :user
  validates_presence_of :message, :phone, :user_id
  validates :phone,
      length: { min: 10 },
      format: { with: /\+?\d+/ }

  scope :unsubmitted, where(submitted_at: nil)

  TWILIO = {
    from_no:      '...',
    account_sid:  '...',
    auth_token:   '...'
  }

  # find a way to call this method separately from user request
  def self.process!
    unsubmitted.find_each do |job|
      begin
        client = Twilio::REST::Client.new TWILIO[:account_sid], TWILIO[:auth_token]
        client.account.sms.messages.create(
          from: TWILIO[:from_no],
          to:   job.phone,
          body: job.message
        )
        job.submitted_at = Time.zone.now
        job.save
      rescue Twilio::REST::RequestError
        # maybe set update a tries counter
        # or delete job record
        # or just ignore this error
      end
    end
  end
end

然后控制器应该只提供将要发送 SMS 的信息:

# don't forget the 'resources :sms_jobs' in your routes.rb
class SmsJobsController < ApplicationController
  # index, update, destroy only for only admin?

  def new
    @sms_job = SmsJobs.new
  end

  def create
    @sms_job = current_user.sms_jobs.build params[:sms_job]
    if @sms_job.save
      redirect_to root_url, notice: "Your message is being send!"
    else
      render :new
    end
  end
end

对于后台处理,看看这些优秀的Railscasts :-) 如果您必须处理许多消息和/或 Twilio 的响应时间很长(尚未使用该服务),您可能需要解决一些并发问题。

于 2012-10-12T13:24:12.373 回答