需要帮助将Twillio集成到用于预约的rails应用程序中。我正在尝试整合twilio来发送约会提醒。 我有两个型号User&预约。
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
该文档仅适用于控制器且不明确,我是否需要创建新模型并与用户或约会模型建立关系? 真的需要一些帮助,请
答案 0 :(得分: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
中发送文字。
答案 1 :(得分:0)
来自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适配器一起使用。