feat: Add support for Whatsapp template messages in the UI (#4711)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Fayaz Ahmed
2022-06-07 17:33:33 +05:30
committed by GitHub
parent 56f668db6b
commit bad24f97ab
22 changed files with 733 additions and 54 deletions

View File

@@ -61,9 +61,13 @@ class Channel::Whatsapp < ApplicationRecord
true
end
def message_templates
sync_templates
super
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
private
@@ -79,7 +83,7 @@ class Channel::Whatsapp < ApplicationRecord
}.to_json
)
response.success? ? response['messages'].first['id'] : nil
process_response(response)
end
def send_attachment_message(phone_number, message)
@@ -99,7 +103,7 @@ class Channel::Whatsapp < ApplicationRecord
}.to_json
)
response.success? ? response['messages'].first['id'] : nil
process_response(response)
end
def send_template_message(phone_number, template_info)
@@ -113,7 +117,16 @@ class Channel::Whatsapp < ApplicationRecord
}.to_json
)
response.success? ? response['messages'].first['id'] : nil
process_response(response)
end
def process_response(response)
if response.success?
response['messages'].first['id']
else
Rails.logger.error response.body
nil
end
end
def template_body_parameters(template_info)
@@ -131,15 +144,6 @@ class Channel::Whatsapp < ApplicationRecord
}
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
def validate_provider_config
response = HTTParty.post(