feat: Enhanced WhatsApp template support with media headers (#11997)

This commit is contained in:
Muhsin Keloth
2025-08-11 18:49:05 +05:30
committed by GitHub
parent 1baf5cbe19
commit 6784eb9b3d
10 changed files with 826 additions and 104 deletions

View File

@@ -2,11 +2,9 @@ class Whatsapp::TemplateProcessorService
pattr_initialize [:channel!, :template_params, :message]
def call
if template_params.present?
process_template_with_params
else
process_template_from_message
end
return [nil, nil, nil, nil] if template_params.blank?
process_template_with_params
end
private
@@ -20,51 +18,6 @@ class Whatsapp::TemplateProcessorService
]
end
def process_template_from_message
return [nil, nil, nil, nil] if message.blank?
# 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
# Then we use regex to parse the template varibles and convert them into the proper payload
channel.message_templates&.each do |template|
match_obj = template_match_object(template)
next if match_obj.blank?
# we have a match, now we need to parse the template variables and convert them into the wa recommended format
processed_parameters = match_obj.captures.map { |x| { type: 'text', text: x } }
# no need to look up further end the search
return [template['name'], template['namespace'], template['language'], processed_parameters]
end
[nil, nil, nil, nil]
end
def template_match_object(template)
body_object = validated_body_object(template)
return if body_object.blank?
template_match_regex = build_template_match_regex(body_object['text'])
message.outgoing_content.match(template_match_regex)
end
def build_template_match_regex(template_text)
# Converts the whatsapp template to a comparable regex string to check against the message content
# the variables are of the format {{num}} ex:{{1}}
# transform the template text into a regex string
# we need to replace the {{num}} with matchers that can be used to capture the variables
template_text = template_text.gsub(/{{\d}}/, '(.*)')
# escape if there are regex characters in the template text
template_text = Regexp.escape(template_text)
# ensuring only the variables remain as capture groups
template_text = template_text.gsub(Regexp.escape('(.*)'), '(.*)')
template_match_string = "^#{template_text}$"
Regexp.new template_match_string
end
def find_template
channel.message_templates.find do |t|
t['name'] == template_params['name'] && t['language'] == template_params['language'] && t['status']&.downcase == 'approved'
@@ -75,21 +28,100 @@ class Whatsapp::TemplateProcessorService
template = find_template
return if template.blank?
parameter_format = template['parameter_format']
# Convert legacy format to enhanced format before processing
converter = Whatsapp::TemplateParameterConverterService.new(template_params, template)
normalized_params = converter.normalize_to_enhanced
if parameter_format == 'NAMED'
template_params['processed_params']&.map { |key, value| { type: 'text', parameter_name: key, text: value } }
else
template_params['processed_params']&.map { |_, value| { type: 'text', text: value } }
end
process_enhanced_template_params(template, normalized_params['processed_params'])
end
def validated_body_object(template)
# we don't care if its not approved template
return if template['status'] != 'approved'
def process_enhanced_template_params(template, processed_params = nil)
processed_params ||= template_params['processed_params']
components = []
# we only care about text body object in template. if not present we discard the template
# we don't support other forms of templates
template['components'].find { |obj| obj['type'] == 'BODY' && obj.key?('text') }
components.concat(process_header_components(processed_params))
components.concat(process_body_components(processed_params, template))
components.concat(process_footer_components(processed_params))
components.concat(process_button_components(processed_params))
@template_params = components
end
def process_header_components(processed_params)
return [] if processed_params['header'].blank?
header_params = build_header_params(processed_params['header'])
header_params.present? ? [{ type: 'header', parameters: header_params }] : []
end
def build_header_params(header_data)
header_params = []
header_data.each do |key, value|
next if value.blank?
if media_url_with_type?(key, header_data)
media_param = parameter_builder.build_media_parameter(value, header_data['media_type'])
header_params << media_param if media_param
elsif key != 'media_type'
header_params << parameter_builder.build_parameter(value)
end
end
header_params
end
def media_url_with_type?(key, header_data)
key == 'media_url' && header_data['media_type'].present?
end
def process_body_components(processed_params, template)
return [] if processed_params['body'].blank?
body_params = processed_params['body'].filter_map do |key, value|
next if value.blank?
parameter_format = template['parameter_format']
if parameter_format == 'NAMED'
parameter_builder.build_named_parameter(key, value)
else
parameter_builder.build_parameter(value)
end
end
body_params.present? ? [{ type: 'body', parameters: body_params }] : []
end
def process_footer_components(processed_params)
return [] if processed_params['footer'].blank?
footer_params = processed_params['footer'].filter_map do |_, value|
next if value.blank?
parameter_builder.build_parameter(value)
end
footer_params.present? ? [{ type: 'footer', parameters: footer_params }] : []
end
def process_button_components(processed_params)
return [] if processed_params['buttons'].blank?
button_params = processed_params['buttons'].filter_map.with_index do |button, index|
next if button.blank?
if button['type'] == 'url' || button['parameter'].present?
{
type: 'button',
sub_type: button['type'] || 'url',
index: index,
parameters: [parameter_builder.build_button_parameter(button)]
}
end
end
button_params.compact
end
def parameter_builder
@parameter_builder ||= Whatsapp::PopulateTemplateParametersService.new
end
end