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
|
|
|
|
|
|
|
|
self.table_name = 'channel_whatsapp'
|
|
|
|
EDITABLE_ATTRS = [:phone_number, { provider_config: {} }].freeze
|
|
|
|
|
|
|
|
validates :phone_number, presence: true, uniqueness: true
|
|
|
|
before_save :validate_provider_config
|
|
|
|
|
|
|
|
def name
|
|
|
|
'Whatsapp'
|
|
|
|
end
|
|
|
|
|
|
|
|
# all this should happen in provider service . but hack mode on
|
|
|
|
def api_base_path
|
|
|
|
# provide the environment variable when testing against sandbox : 'https://waba-sandbox.360dialog.io/v1'
|
|
|
|
ENV.fetch('360DIALOG_BASE_URL', 'https://waba.360dialog.io/v1')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Extract later into provider Service
|
|
|
|
def send_message(phone_number, message)
|
2021-11-11 07:33:48 +00:00
|
|
|
if message.attachments.present?
|
|
|
|
send_attachment_message(phone_number, message)
|
|
|
|
else
|
|
|
|
send_text_message(phone_number, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-30 15:20:35 +00:00
|
|
|
def send_template(phone_number, template_info)
|
|
|
|
send_template_message(phone_number, template_info)
|
|
|
|
end
|
|
|
|
|
2021-11-11 07:33:48 +00:00
|
|
|
def media_url(media_id)
|
|
|
|
"#{api_base_path}/media/#{media_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_headers
|
|
|
|
{ 'D360-API-KEY' => provider_config['api_key'], 'Content-Type' => 'application/json' }
|
|
|
|
end
|
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
def messaging_window_enabled?
|
2021-11-11 07:33:48 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2022-06-07 12:03:33 +00:00
|
|
|
def sync_templates
|
|
|
|
# to prevent too many api calls
|
|
|
|
last_updated = message_templates_last_updated || 1.day.ago
|
|
|
|
return if Time.current < (last_updated + 12.hours)
|
|
|
|
|
|
|
|
response = HTTParty.get("#{api_base_path}/configs/templates", headers: api_headers)
|
|
|
|
update(message_templates: response['waba_templates'], message_templates_last_updated: Time.now.utc) if response.success?
|
2021-11-30 15:20:35 +00:00
|
|
|
end
|
|
|
|
|
2021-11-11 07:33:48 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def send_text_message(phone_number, message)
|
2021-11-30 15:20:35 +00:00
|
|
|
response = HTTParty.post(
|
2021-10-05 18:05:06 +00:00
|
|
|
"#{api_base_path}/messages",
|
2021-11-30 15:20:35 +00:00
|
|
|
headers: api_headers,
|
2021-10-05 18:05:06 +00:00
|
|
|
body: {
|
|
|
|
to: phone_number,
|
2021-11-11 07:33:48 +00:00
|
|
|
text: { body: message.content },
|
2021-10-05 18:05:06 +00:00
|
|
|
type: 'text'
|
|
|
|
}.to_json
|
|
|
|
)
|
2021-11-30 15:20:35 +00:00
|
|
|
|
2022-06-07 12:03:33 +00:00
|
|
|
process_response(response)
|
2021-10-05 18:05:06 +00:00
|
|
|
end
|
|
|
|
|
2021-11-11 07:33:48 +00:00
|
|
|
def send_attachment_message(phone_number, message)
|
|
|
|
attachment = message.attachments.first
|
|
|
|
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
|
2022-04-20 17:12:13 +00:00
|
|
|
attachment_url = attachment.download_url
|
2021-11-30 15:20:35 +00:00
|
|
|
response = HTTParty.post(
|
2021-11-11 07:33:48 +00:00
|
|
|
"#{api_base_path}/messages",
|
2021-11-30 15:20:35 +00:00
|
|
|
headers: api_headers,
|
2021-11-11 07:33:48 +00:00
|
|
|
body: {
|
|
|
|
'to' => phone_number,
|
|
|
|
'type' => type,
|
|
|
|
type.to_s => {
|
|
|
|
'link': attachment_url,
|
|
|
|
'caption': message.content
|
|
|
|
}
|
|
|
|
}.to_json
|
|
|
|
)
|
2021-11-30 15:20:35 +00:00
|
|
|
|
2022-06-07 12:03:33 +00:00
|
|
|
process_response(response)
|
2021-11-30 15:20:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_template_message(phone_number, template_info)
|
|
|
|
response = HTTParty.post(
|
|
|
|
"#{api_base_path}/messages",
|
|
|
|
headers: api_headers,
|
|
|
|
body: {
|
|
|
|
to: phone_number,
|
|
|
|
template: template_body_parameters(template_info),
|
|
|
|
type: 'template'
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
|
2022-06-07 12:03:33 +00:00
|
|
|
process_response(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_response(response)
|
|
|
|
if response.success?
|
|
|
|
response['messages'].first['id']
|
|
|
|
else
|
|
|
|
Rails.logger.error response.body
|
|
|
|
nil
|
|
|
|
end
|
2021-11-30 15:20:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def template_body_parameters(template_info)
|
|
|
|
{
|
|
|
|
name: template_info[:name],
|
|
|
|
namespace: template_info[:namespace],
|
|
|
|
language: {
|
|
|
|
policy: 'deterministic',
|
|
|
|
code: template_info[:lang_code]
|
|
|
|
},
|
|
|
|
components: [{
|
|
|
|
type: 'body',
|
|
|
|
parameters: template_info[:parameters]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-10-05 18:05:06 +00:00
|
|
|
# Extract later into provider Service
|
|
|
|
def validate_provider_config
|
|
|
|
response = HTTParty.post(
|
|
|
|
"#{api_base_path}/configs/webhook",
|
|
|
|
headers: { 'D360-API-KEY': provider_config['api_key'], 'Content-Type': 'application/json' },
|
|
|
|
body: {
|
|
|
|
url: "#{ENV['FRONTEND_URL']}/webhooks/whatsapp/#{phone_number}"
|
|
|
|
}.to_json
|
|
|
|
)
|
2022-02-03 23:22:13 +00:00
|
|
|
errors.add(:provider_config, 'error setting up the webook') unless response.success?
|
2021-10-05 18:05:06 +00:00
|
|
|
end
|
|
|
|
end
|