【问题标题】:twillio integeration with rails 3.2twilio 与 rails 3.2 的集成
【发布时间】:2014-04-30 21:12:48
【问题描述】:

需要帮助将 Twillio 集成到用于预约的 rails 应用程序中。我正在尝试集成 twilio 以发送约会提醒。 我有两个模型用户和约会。

class User < ActiveRecord::Base 
  attr_accessible :email, :password, :password_confirmation, :remember_me, :provider, :uid, :name, 
  has_many :appointments, dependent: :destroy
end


class Appointment < ActiveRecord::Base      
    attr_accessible :discription, :appointment_time, :reserve_time, :reserve_date
    belongs_to :usermodel
end

现在有来自 Twillio 的约会提醒,我想整合:https://github.com/twilio/twilio-ruby

该文档仅适用于控制器并且不明确,我是否需要创建一个新模型并与用户或约会模型有关系? 真的需要帮助,拜托了

【问题讨论】:

    标签: ruby-on-rails api rubygems twilio


    【解决方案1】:

    我建议在初始化程序中创建 Twillio 客户端并将其设置为全局变量:

    config/initializers/twillio

    require 'twillio-ruby'
    
    # put your own credentials here
    account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
    
    $twillio = Twilio::REST::Client.new account_sid, auth_token
    

    现在,您可以在任何需要的地方使用 $twillio 与 twillio 客户端进行交互。例如,如果您在创建约会时发送短信,您可以执行以下操作:

    class AppointmentsController
      ...
      def create
        @appointment = Appointment.new(appointment_params)
        if @appointment.save
          appointment_message = "Your appointment has been booked for #{@appointment.date}"
          $twillio.account.messages.create(:from => '+14159341234', 
                                           :to => @appointment.user.phone_number,
                                           :body => 'Your appointment has been booked')
        end
      end
    end
    

    如果您想安排约会提醒短信,请考虑使用 Sidekiq 之类的东西来排队一个将在您想要的时间执行操作的工作人员。使用 sidekiq,您的创建约会操作将如下所示:

    class AppointmentsController
      ...
      def create
        @appointment = Appointment.new(appointment_params)
        if @appointment.save
          TextReminderWorker.perform_in(@appointment.datetime - 30.minutes, @appointment.id)
        end
      end
    end
    

    然后在您的TextReminderWorker 中发送文本。

    【讨论】:

    • 非常感谢 steven,我希望在预约时间前半小时发送短信例如:预约时间为下午 4:00 时在下午 3:00 发送短信。
    • 有没有办法在 Rails 中的特定日期和时间触发该操作。
    • 考虑使用类似sidekiq 的方式为此安排后台作业。
    【解决方案2】:

    来自 Twilio 的 Ricky 在这里。

    我们整理了一个使用 Ruby 的约会提醒教程,在这里可能会有所帮助:

    https://www.twilio.com/docs/tutorials/walkthrough/appointment-reminders/ruby/rails

    这是本教程中我们发送提醒的代码块:

      # Notify our appointment attendee X minutes before the appointment time
      def reminder
        @twilio_number = ENV['TWILIO_NUMBER']
        @client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
        time_str = ((self.time).localtime).strftime("%I:%M%p on %b. %d, %Y")
        reminder = "Hi #{self.name}. Just a reminder that you have an appointment coming up at #{time_str}."
        message = @client.account.messages.create(
          :from => @twilio_number,
          :to => self.phone_number,
          :body => reminder,
        )
        puts message.to
      end
    

    为了安排提醒,我们将 Delayed::Job 与 ActiveRecord 适配器一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2017-11-29
      相关资源
      最近更新 更多