2014-04-30 34 views
0

需要將Twillio整合到用於預約的導軌應用程序中。我正在嘗試整合twilio發送預約提醒。我有兩個型號用戶&預約。twillio與導軌的整合3.2

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的約會提醒,我想integerate:https://github.com/twilio/twilio-ruby

的文檔工作,只有控制器,它是模糊的,我是否需要創建一個新的模型,並有關係用戶或約會模型? 真的需要一些幫助,請

回答

0

我會建議在初始創建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你需要發送的文本。

+0

非常感謝史蒂文,我希望文本在預約時間前半小時發送。例如:預約時間爲下午4:00時,在下午3:00發送文本。 – magnetic

+0

有沒有辦法在rails中的特定日期和時間觸發該操作。 – magnetic

+0

使用類似[sidekiq](https://github.com/mperham/sidekiq/wiki/Scheduled-Jobs)的方式來調查後臺作業。 –

0

Ricky從Twilio在這裏。

我們放在一起使用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適配器。