2021-10-05 18:05:06 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: channel_whatsapp
|
|
|
|
#
|
2021-11-30 15:20:35 +00:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# message_templates :jsonb
|
|
|
|
# message_templates_last_updated :datetime
|
|
|
|
# phone_number :string not null
|
|
|
|
# provider :string default("default")
|
|
|
|
# provider_config :jsonb
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
2021-10-05 18:05:06 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_channel_whatsapp_on_phone_number (phone_number) UNIQUE
|
|
|
|
#
|
|
|
|
|
|
|
|
class Channel::Whatsapp < ApplicationRecord
|
|
|
|
include Channelable
|
2022-11-24 11:50:32 +00:00
|
|
|
include Reauthorizable
|
2021-10-05 18:05:06 +00:00
|
|
|
|
|
|
|
self.table_name = 'channel_whatsapp'
|
2022-07-06 19:45:03 +00:00
|
|
|
EDITABLE_ATTRS = [:phone_number, :provider, { provider_config: {} }].freeze
|
|
|
|
|
|
|
|
# default at the moment is 360dialog lets change later.
|
|
|
|
PROVIDERS = %w[default whatsapp_cloud].freeze
|
2022-10-12 00:32:31 +00:00
|
|
|
before_validation :ensure_webhook_verify_token
|
2022-07-06 19:45:03 +00:00
|
|
|
|
|
|
|
validates :provider, inclusion: { in: PROVIDERS }
|
2021-10-05 18:05:06 +00:00
|
|
|
validates :phone_number, presence: true, uniqueness: true
|
2022-07-06 19:45:03 +00:00
|
|
|
validate :validate_provider_config
|
2022-10-12 00:32:31 +00:00
|
|
|
|
2022-06-14 18:16:36 +00:00
|
|
|
after_create :sync_templates
|
2021-10-05 18:05:06 +00:00
|
|
|
|
|
|
|
def name
|
|
|
|
'Whatsapp'
|
|
|
|
end
|
|
|
|
|
2022-07-06 19:45:03 +00:00
|
|
|
def provider_service
|
|
|
|
if provider == 'whatsapp_cloud'
|
|
|
|
Whatsapp::Providers::WhatsappCloudService.new(whatsapp_channel: self)
|
2021-11-11 07:33:48 +00:00
|
|
|
else
|
2022-07-06 19:45:03 +00:00
|
|
|
Whatsapp::Providers::Whatsapp360DialogService.new(whatsapp_channel: self)
|
2021-11-11 07:33:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
def messaging_window_enabled?
|
2021-11-11 07:33:48 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2022-07-06 19:45:03 +00:00
|
|
|
delegate :send_message, to: :provider_service
|
|
|
|
delegate :send_template, to: :provider_service
|
|
|
|
delegate :sync_templates, to: :provider_service
|
|
|
|
delegate :media_url, to: :provider_service
|
|
|
|
delegate :api_headers, to: :provider_service
|
2021-11-30 15:20:35 +00:00
|
|
|
|
2021-11-11 07:33:48 +00:00
|
|
|
private
|
|
|
|
|
2022-10-12 00:32:31 +00:00
|
|
|
def ensure_webhook_verify_token
|
|
|
|
provider_config['webhook_verify_token'] ||= SecureRandom.hex(16) if provider == 'whatsapp_cloud'
|
|
|
|
end
|
|
|
|
|
2021-10-05 18:05:06 +00:00
|
|
|
def validate_provider_config
|
2022-07-06 19:45:03 +00:00
|
|
|
errors.add(:provider_config, 'Invalid Credentials') unless provider_service.validate_provider_config?
|
2021-10-05 18:05:06 +00:00
|
|
|
end
|
|
|
|
end
|