chore: Whatsapp templates fix for 360 dialog (#3479)

Fixes: #3426
This commit is contained in:
Sojan Jose
2021-11-30 20:50:35 +05:30
committed by GitHub
parent 0899f62912
commit d5c30760a7
7 changed files with 215 additions and 17 deletions

View File

@@ -2,13 +2,15 @@
#
# Table name: channel_whatsapp
#
# id :bigint not null, primary key
# 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
# 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
#
# Indexes
#
@@ -43,6 +45,10 @@ class Channel::Whatsapp < ApplicationRecord
end
end
def send_template(phone_number, template_info)
send_template_message(phone_number, template_info)
end
def media_url(media_id)
"#{api_base_path}/media/#{media_id}"
end
@@ -55,27 +61,34 @@ class Channel::Whatsapp < ApplicationRecord
true
end
def message_templates
sync_templates
super
end
private
def send_text_message(phone_number, message)
HTTParty.post(
response = HTTParty.post(
"#{api_base_path}/messages",
headers: { 'D360-API-KEY': provider_config['api_key'], 'Content-Type': 'application/json' },
headers: api_headers,
body: {
to: phone_number,
text: { body: message.content },
type: 'text'
}.to_json
)
response.success? ? response['messages'].first['id'] : nil
end
def send_attachment_message(phone_number, message)
attachment = message.attachments.first
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
attachment_url = attachment.file_url
HTTParty.post(
response = HTTParty.post(
"#{api_base_path}/messages",
headers: { 'D360-API-KEY': provider_config['api_key'], 'Content-Type': 'application/json' },
headers: api_headers,
body: {
'to' => phone_number,
'type' => type,
@@ -85,6 +98,46 @@ class Channel::Whatsapp < ApplicationRecord
}
}.to_json
)
response.success? ? response['messages'].first['id'] : nil
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
)
response.success? ? response['messages'].first['id'] : nil
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
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?
end
# Extract later into provider Service