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

@@ -19,7 +19,7 @@ describe Whatsapp::OneoffCampaignService do
'namespace' => '23423423_2342423_324234234_2343224',
'category' => 'UTILITY',
'language' => 'en',
'processed_params' => { 'name' => 'John', 'ticket_id' => '2332' }
'processed_params' => { 'body' => { 'name' => 'John', 'ticket_id' => '2332' } }
}
end
@@ -125,8 +125,13 @@ describe Whatsapp::OneoffCampaignService do
namespace: '23423423_2342423_324234234_2343224',
lang_code: 'en',
parameters: array_including(
hash_including(type: 'text', parameter_name: 'name', text: 'John'),
hash_including(type: 'text', parameter_name: 'ticket_id', text: '2332')
hash_including(
type: 'body',
parameters: array_including(
hash_including(type: 'text', parameter_name: 'name', text: 'John'),
hash_including(type: 'text', parameter_name: 'ticket_id', text: '2332')
)
)
)
)
)

View File

@@ -165,19 +165,17 @@ describe Whatsapp::Providers::WhatsappCloudService do
let(:template_body) do
{
messaging_product: 'whatsapp',
recipient_type: 'individual', # Added recipient_type field
to: '+123456789',
type: 'template',
template: {
name: template_info[:name],
language: {
policy: 'deterministic',
code: template_info[:lang_code]
},
components: [
{ type: 'body',
parameters: template_info[:parameters] }
]
},
type: 'template'
components: template_info[:parameters] # Changed to use parameters directly (enhanced format)
}
}
end

View File

@@ -6,7 +6,7 @@ describe Whatsapp::SendOnWhatsappService do
namespace: '23423423_2342423_324234234_2343224',
language: 'en_US',
category: 'Marketing',
processed_params: { '1' => '3' }
processed_params: { 'body' => { '1' => '3' } }
}
describe '#perform' do
@@ -39,15 +39,16 @@ describe Whatsapp::SendOnWhatsappService do
let(:named_template_body) do
{
messaging_product: 'whatsapp',
recipient_type: 'individual',
to: '123456789',
type: 'template',
template: {
name: 'ticket_status_updated',
language: { 'policy': 'deterministic', 'code': 'en_US' },
components: [{ 'type': 'body',
'parameters': [{ 'type': 'text', parameter_name: 'last_name', 'text': 'Dale' },
{ 'type': 'text', parameter_name: 'ticket_id', 'text': '2332' }] }]
},
type: 'template'
}
}
end
@@ -73,7 +74,7 @@ describe Whatsapp::SendOnWhatsappService do
it 'calls channel.send_template when after 24 hour limit' do
message = create(:message, message_type: :outgoing, content: 'Your package has been shipped. It will be delivered in 3 business days.',
conversation: conversation)
conversation: conversation, additional_attributes: { template_params: template_params })
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
.with(
@@ -107,12 +108,18 @@ describe Whatsapp::SendOnWhatsappService do
name: 'ticket_status_updated',
language: 'en_US',
category: 'UTILITY',
processed_params: { 'last_name' => 'Dale', 'ticket_id' => '2332' }
processed_params: { 'body' => { 'last_name' => 'Dale', 'ticket_id' => '2332' } }
}
stub_request(:post, "https://graph.facebook.com/v13.0/#{whatsapp_cloud_channel.provider_config['phone_number_id']}/messages")
.with(
:headers => { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{whatsapp_cloud_channel.provider_config['api_key']}" },
:headers => {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{whatsapp_cloud_channel.provider_config['api_key']}",
'User-Agent' => 'Ruby'
},
:body => named_template_body.to_json
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
message = create(:message,
@@ -124,12 +131,44 @@ describe Whatsapp::SendOnWhatsappService do
end
it 'calls channel.send_template when template has regexp characters' do
message = create(
:message,
message_type: :outgoing,
content: 'عميلنا العزيز الرجاء الرد على هذه الرسالة بكلمة *نعم* للرد على إستفساركم من قبل خدمة العملاء.',
conversation: conversation
)
regexp_template_params = build_template_params('customer_yes_no', '2342384942_32423423_23423fdsdaf23', 'ar', {})
arabic_content = 'عميلنا العزيز الرجاء الرد على هذه الرسالة بكلمة *نعم* للرد على إستفساركم من قبل خدمة العملاء.'
message = create_message_with_template(arabic_content, regexp_template_params)
stub_template_request(regexp_template_params, [])
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
it 'handles template with header parameters' do
processed_params = {
'body' => { '1' => '3' },
'header' => { 'media_url' => 'https://example.com/image.jpg', 'media_type' => 'image' }
}
header_template_params = build_sample_template_params(processed_params)
message = create_message_with_template('', header_template_params)
components = [
{ type: 'header', parameters: [{ type: 'image', image: { link: 'https://example.com/image.jpg' } }] },
{ type: 'body', parameters: [{ type: 'text', text: '3' }] }
]
stub_sample_template_request(components)
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
it 'handles empty processed_params gracefully' do
empty_template_params = {
name: 'sample_shipping_confirmation',
namespace: '23423423_2342423_324234234_2343224',
language: 'en_US',
category: 'SHIPPING_UPDATE',
processed_params: {}
}
message = create(:message, additional_attributes: { template_params: empty_template_params },
conversation: conversation, message_type: :outgoing)
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
.with(
@@ -137,10 +176,10 @@ describe Whatsapp::SendOnWhatsappService do
body: {
to: '123456789',
template: {
name: 'customer_yes_no',
namespace: '2342384942_32423423_23423fdsdaf23',
language: { 'policy': 'deterministic', 'code': 'ar' },
components: [{ 'type': 'body', 'parameters': [] }]
name: 'sample_shipping_confirmation',
namespace: '23423423_2342423_324234234_2343224',
language: { 'policy': 'deterministic', 'code': 'en_US' },
components: []
},
type: 'template'
}.to_json
@@ -149,6 +188,169 @@ describe Whatsapp::SendOnWhatsappService do
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
it 'handles template with button parameters' do
processed_params = {
'body' => { '1' => '3' },
'buttons' => [{ 'type' => 'url', 'parameter' => 'https://track.example.com/123' }]
}
button_template_params = build_sample_template_params(processed_params)
message = create_message_with_template('', button_template_params)
components = [
{ type: 'body', parameters: [{ type: 'text', text: '3' }] },
{ type: 'button', sub_type: 'url', index: 0, parameters: [{ type: 'text', text: 'https://track.example.com/123' }] }
]
stub_sample_template_request(components)
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
it 'processes template parameters correctly via integration' do
processed_params = {
'body' => { '1' => '5' },
'footer' => { 'text' => 'Thank you' }
}
complex_template_params = build_sample_template_params(processed_params)
message = create_message_with_template('', complex_template_params)
components = [
{ type: 'body', parameters: [{ type: 'text', text: '5' }] },
{ type: 'footer', parameters: [{ type: 'text', text: 'Thank you' }] }
]
stub_sample_template_request(components)
expect { described_class.new(message: message).perform }.not_to raise_error
expect(message.reload.source_id).to eq('123456789')
end
it 'handles edge case with missing template gracefully' do
# Test the service behavior when template is not found
missing_template_params = {
'name' => 'non_existent_template',
'namespace' => 'missing_namespace',
'language' => 'en_US',
'category' => 'UTILITY',
'processed_params' => { 'body' => { '1' => 'test' } }
}
service = Whatsapp::TemplateProcessorService.new(
channel: whatsapp_channel,
template_params: missing_template_params
)
expect { service.call }.not_to raise_error
name, namespace, language, processed_params = service.call
expect(name).to eq('non_existent_template')
expect(namespace).to eq('missing_namespace')
expect(language).to eq('en_US')
expect(processed_params).to be_nil
end
it 'handles template with blank parameter values correctly' do
processed_params = {
'body' => { '1' => '', '2' => 'valid_value', '3' => nil },
'header' => { 'media_url' => '', 'media_type' => 'image' }
}
blank_values_template_params = build_sample_template_params(processed_params)
message = create_message_with_template('', blank_values_template_params)
components = [{ type: 'body', parameters: [{ type: 'text', text: 'valid_value' }] }]
stub_sample_template_request(components)
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
it 'handles nil template_params gracefully' do
# Test service behavior when template_params is completely nil
message = create(:message, additional_attributes: {},
conversation: conversation, message_type: :outgoing)
# Should send regular message, not template
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
.with(
headers: headers,
body: {
to: '123456789',
text: { body: message.content },
type: 'text'
}.to_json
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
expect { described_class.new(message: message).perform }.not_to raise_error
end
it 'processes template with rich text formatting' do
processed_params = { 'body' => { '1' => '*Bold text* and _italic text_' } }
rich_text_template_params = build_sample_template_params(processed_params)
message = create_message_with_template('', rich_text_template_params)
components = [{ type: 'body', parameters: [{ type: 'text', text: '*Bold text* and _italic text_' }] }]
stub_sample_template_request(components)
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
private
def build_template_params(name, namespace, language, processed_params)
{
name: name,
namespace: namespace,
language: language,
category: 'SHIPPING_UPDATE',
processed_params: processed_params
}
end
def create_message_with_template(content, template_params)
create(:message,
message_type: :outgoing,
content: content,
conversation: conversation,
additional_attributes: { template_params: template_params })
end
def stub_template_request(template_params, components)
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
.with(
headers: headers,
body: {
to: '123456789',
template: {
name: template_params[:name],
namespace: template_params[:namespace],
language: { 'policy': 'deterministic', 'code': template_params[:language] },
components: components
},
type: 'template'
}.to_json
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
end
def build_sample_template_params(processed_params)
build_template_params('sample_shipping_confirmation', '23423423_2342423_324234234_2343224', 'en_US', processed_params)
end
def stub_sample_template_request(components)
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
.with(
headers: headers,
body: {
to: '123456789',
template: {
name: 'sample_shipping_confirmation',
namespace: '23423423_2342423_324234234_2343224',
language: { 'policy': 'deterministic', 'code': 'en_US' },
components: components
},
type: 'template'
}.to_json
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
end
end
end
end

View File

@@ -0,0 +1,199 @@
require 'rails_helper'
describe Whatsapp::TemplateParameterConverterService do
let(:template) do
{
'name' => 'test_template',
'language' => 'en',
'components' => [
{
'type' => 'BODY',
'text' => 'Hello {{1}}, your order {{2}} is ready!'
}
]
}
end
let(:media_template) do
{
'name' => 'media_template',
'language' => 'en',
'components' => [
{
'type' => 'HEADER',
'format' => 'IMAGE'
},
{
'type' => 'BODY',
'text' => 'Check out {{1}}!'
}
]
}
end
let(:button_template) do
{
'name' => 'button_template',
'language' => 'en',
'components' => [
{
'type' => 'BODY',
'text' => 'Visit our website!'
},
{
'type' => 'BUTTONS',
'buttons' => [
{
'type' => 'URL',
'url' => 'https://example.com/{{1}}'
},
{
'type' => 'COPY_CODE'
}
]
}
]
}
end
describe '#normalize_to_enhanced' do
context 'when already enhanced format' do
let(:enhanced_params) do
{
'processed_params' => {
'body' => { '1' => 'John', '2' => 'Order123' }
}
}
end
it 'returns unchanged' do
converter = described_class.new(enhanced_params, template)
result = converter.normalize_to_enhanced
expect(result).to eq(enhanced_params)
end
end
context 'when legacy array format' do
let(:legacy_array_params) do
{
'processed_params' => %w[John Order123]
}
end
it 'converts to enhanced format' do
converter = described_class.new(legacy_array_params, template)
result = converter.normalize_to_enhanced
expect(result['processed_params']).to eq({
'body' => { '1' => 'John', '2' => 'Order123' }
})
expect(result['format_version']).to eq('legacy')
end
end
context 'when legacy flat hash format' do
let(:legacy_hash_params) do
{
'processed_params' => { '1' => 'John', '2' => 'Order123' }
}
end
it 'converts to enhanced format' do
converter = described_class.new(legacy_hash_params, template)
result = converter.normalize_to_enhanced
expect(result['processed_params']).to eq({
'body' => { '1' => 'John', '2' => 'Order123' }
})
expect(result['format_version']).to eq('legacy')
end
end
context 'when legacy hash with all body parameters' do
let(:legacy_hash_params) do
{
'processed_params' => {
'1' => 'Product',
'customer_name' => 'John'
}
}
end
it 'converts to enhanced format with body only' do
converter = described_class.new(legacy_hash_params, media_template)
result = converter.normalize_to_enhanced
expect(result['processed_params']).to eq({
'body' => {
'1' => 'Product',
'customer_name' => 'John'
}
})
expect(result['format_version']).to eq('legacy')
end
end
context 'when invalid format' do
let(:invalid_params) do
{
'processed_params' => 'invalid_string'
}
end
it 'raises ArgumentError' do
expect do
converter = described_class.new(invalid_params, template)
converter.normalize_to_enhanced
end.to raise_error(ArgumentError, /Unknown legacy format/)
end
end
end
describe '#enhanced_format?' do
it 'returns true for valid enhanced format' do
enhanced = { 'body' => { '1' => 'test' } }
converter = described_class.new({}, template)
expect(converter.send(:enhanced_format?, enhanced)).to be true
end
it 'returns false for array' do
converter = described_class.new({}, template)
expect(converter.send(:enhanced_format?, ['test'])).to be false
end
it 'returns false for flat hash' do
converter = described_class.new({}, template)
expect(converter.send(:enhanced_format?, { '1' => 'test' })).to be false
end
it 'returns false for invalid structure' do
invalid = { 'body' => 'not_a_hash' }
converter = described_class.new({}, template)
expect(converter.send(:enhanced_format?, invalid)).to be false
end
end
describe 'simplified conversion methods' do
describe '#convert_array_to_body_params' do
it 'converts empty array' do
converter = described_class.new({}, template)
result = converter.send(:convert_array_to_body_params, [])
expect(result).to eq({})
end
it 'converts array to numbered body parameters' do
converter = described_class.new({}, template)
result = converter.send(:convert_array_to_body_params, %w[John Order123])
expect(result).to eq({ '1' => 'John', '2' => 'Order123' })
end
end
describe '#convert_hash_to_body_params' do
it 'converts hash to body parameters' do
converter = described_class.new({}, template)
result = converter.send(:convert_hash_to_body_params, { 'name' => 'John', 'order' => '123' })
expect(result).to eq({ 'name' => 'John', 'order' => '123' })
end
end
end
end