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:
@@ -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(
|
||||
|
||||
@@ -92,6 +92,10 @@ class Inbox < ApplicationRecord
|
||||
channel_type == 'Channel::TwitterProfile'
|
||||
end
|
||||
|
||||
def whatsapp?
|
||||
channel_type == 'Channel::Whatsapp'
|
||||
end
|
||||
|
||||
def inbox_type
|
||||
channel.name
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user