skip to Main Content

Need help with integrating Twillio in a rails app made for making appointments. I am trying to integrate twilio to send appointment reminders.
I have two models User & Appointment.

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

and now there is the appointment reminder from Twillio which i want to integerate: https://github.com/twilio/twilio-ruby

The documentation works with controller only and it is ambiguous, Do i need to create a new model and have relationship with User or Appointment model?
Really need some help, please

2

Answers


  1. I would recommend creating the Twillio client in an initializer and setting it to a global variable:

    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
    

    Now you can interact with the twillio client using $twillio wherever you need to. For example if you what to send a text message when an appointment is created you could do something like this:

    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
    

    If you want to schedule an appointment reminder text message consider using something like Sidekiq to queue a worker that will perform the action at the time you want. Using sidekiq your create appointment action would then look something like this:

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

    Then in your TextReminderWorker you would send the text.

    Login or Signup to reply.
  2. Ricky from Twilio here.

    We put together a tutorial for appointment reminders using Ruby that could be helpful here:

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

    Here’s the code block in this tutorial where we’re sending the reminder:

      # 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
    

    For scheduling the reminder, we’re using Delayed::Job with the ActiveRecord adapter.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search