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

@@ -106,10 +106,7 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS
policy: 'deterministic',
code: template_info[:lang_code]
},
components: [{
type: 'body',
parameters: template_info[:parameters]
}]
components: template_info[:parameters]
}
end

View File

@@ -12,15 +12,20 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
end
def send_template(phone_number, template_info)
template_body = template_body_parameters(template_info)
request_body = {
messaging_product: 'whatsapp',
recipient_type: 'individual', # Only individual messages supported (not group messages)
to: phone_number,
type: 'template',
template: template_body
}
response = HTTParty.post(
"#{phone_id_path}/messages",
headers: api_headers,
body: {
messaging_product: 'whatsapp',
to: phone_number,
template: template_body_parameters(template_info),
type: 'template'
}.to_json
body: request_body.to_json
)
process_response(response)
@@ -119,17 +124,36 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
end
def template_body_parameters(template_info)
{
template_body = {
name: template_info[:name],
language: {
policy: 'deterministic',
code: template_info[:lang_code]
},
components: [{
type: 'body',
parameters: template_info[:parameters]
}]
}
}
# Enhanced template parameters structure
# Note: Legacy format support (simple parameter arrays) has been removed
# in favor of the enhanced component-based structure that supports
# headers, buttons, and authentication templates.
#
# Expected payload format from frontend:
# {
# processed_params: {
# body: { '1': 'John', '2': '123 Main St' },
# header: { media_url: 'https://...', media_type: 'image' },
# buttons: [{ type: 'url', parameter: 'otp123456' }]
# }
# }
# This gets transformed into WhatsApp API component format:
# [
# { type: 'body', parameters: [...] },
# { type: 'header', parameters: [...] },
# { type: 'button', sub_type: 'url', parameters: [...] }
# ]
template_body[:components] = template_info[:parameters] || []
template_body
end
def whatsapp_reply_context(message)