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

@@ -6,16 +6,18 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
end
def perform_reply
# can reply checks if 24 hour limit has passed.
if message.conversation.can_reply?
send_on_whatsapp
else
should_send_template_message = template_params.present? || !message.conversation.can_reply?
if should_send_template_message
send_template_message
else
send_session_message
end
end
def send_template_message
channel.sync_templates
name, namespace, lang_code, processed_parameters = processable_channel_message_template
return if name.blank?
message_id = channel.send_template(message.conversation.contact_inbox.source_id, {
@@ -28,6 +30,16 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
end
def processable_channel_message_template
if template_params.present?
return [
template_params['name'],
template_params['namespace'],
template_params['language'],
template_params['processed_params'].map { |_, value| { type: 'text', text: value } }
]
end
# Delete the following logic once the update for template_params is stable
# see if we can match the message content to a template
# An example template may look like "Your package has been shipped. It will be delivered in {{1}} business days.
# We want to iterate over these templates with our message body and see if we can fit it to any of the templates
@@ -78,8 +90,12 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
template['components'].find { |obj| obj['type'] == 'BODY' && obj.key?('text') }
end
def send_on_whatsapp
def send_session_message
message_id = channel.send_message(message.conversation.contact_inbox.source_id, message)
message.update!(source_id: message_id) if message_id.present?
end
def template_params
message.additional_attributes && message.additional_attributes['template_params']
end
end