2020-04-05 16:41:27 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: channel_twilio_sms
|
|
|
|
#
|
2022-07-08 12:50:07 +00:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# account_sid :string not null
|
|
|
|
# auth_token :string not null
|
|
|
|
# medium :integer default("sms")
|
|
|
|
# messaging_service_sid :string
|
|
|
|
# phone_number :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
2020-04-05 16:41:27 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2022-07-11 18:12:48 +00:00
|
|
|
# index_channel_twilio_sms_on_account_sid_and_phone_number (account_sid,phone_number) UNIQUE
|
|
|
|
# index_channel_twilio_sms_on_messaging_service_sid (messaging_service_sid) UNIQUE
|
|
|
|
# index_channel_twilio_sms_on_phone_number (phone_number) UNIQUE
|
2020-04-05 16:41:27 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
class Channel::TwilioSms < ApplicationRecord
|
2021-09-10 20:01:17 +00:00
|
|
|
include Channelable
|
|
|
|
|
2020-04-05 16:41:27 +00:00
|
|
|
self.table_name = 'channel_twilio_sms'
|
|
|
|
|
|
|
|
validates :account_sid, presence: true
|
|
|
|
validates :auth_token, presence: true
|
2022-07-08 12:50:07 +00:00
|
|
|
|
|
|
|
# Must have _one_ of messaging_service_sid _or_ phone_number, and messaging_service_sid is preferred
|
2022-07-18 14:32:37 +00:00
|
|
|
validates :messaging_service_sid, uniqueness: true, presence: true, unless: :phone_number.presence
|
|
|
|
validates :phone_number, absence: true, if: :messaging_service_sid.presence
|
2022-05-07 12:27:16 +00:00
|
|
|
validates :phone_number, uniqueness: true, allow_nil: true
|
2020-04-05 16:41:27 +00:00
|
|
|
|
2020-04-29 20:11:13 +00:00
|
|
|
enum medium: { sms: 0, whatsapp: 1 }
|
|
|
|
|
2020-09-04 13:43:47 +00:00
|
|
|
def name
|
2020-09-17 18:02:19 +00:00
|
|
|
medium == 'sms' ? 'Twilio SMS' : 'Whatsapp'
|
2020-07-25 17:24:45 +00:00
|
|
|
end
|
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
def messaging_window_enabled?
|
2020-09-17 18:02:19 +00:00
|
|
|
medium == 'whatsapp'
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
2022-07-08 12:50:07 +00:00
|
|
|
|
|
|
|
def send_message(to:, body:, media_url: nil)
|
|
|
|
params = send_message_from.merge(to: to, body: body)
|
|
|
|
params[:media_url] = media_url if media_url.present?
|
|
|
|
client.messages.create(**params)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
|
|
|
::Twilio::REST::Client.new(account_sid, auth_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_message_from
|
|
|
|
if messaging_service_sid?
|
|
|
|
{ messaging_service_sid: messaging_service_sid }
|
|
|
|
else
|
|
|
|
{ from: phone_number }
|
|
|
|
end
|
|
|
|
end
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|