fix: Add support for named parameter templates in WhatsApp (#11198)

The expected payload on WhatsApp Cloud API is the following. 
```json
{ 
  "template": {
    "name": "TEMPLATE_NAME",
    "language": {
      "code": "LANGUAGE_AND_LOCALE_CODE"
    },
    "components": [
         "<NAMED_PARAMETER_INPUT>",
         "<POSITIONAL_PARAMETER_INPUT>"
     ]
  }
}
```
Named templates expect a `parameter_name`

```json
{
   "type": "body",
    "parameters": [
      {
        "type": "text",
        "parameter_name": "customer_name",
        "text": "John"
      },
      {
        "type": "text",
        "parameter_name": "order_id",
        "text": "9128312831"
      }        
    ]
}
```

In this PR, we would check if the template is a name template, then we
would send the `parameter_name` as well.

Reference: https://github.com/chatwoot/chatwoot/issues/10886
This commit is contained in:
Pranav
2025-03-28 14:07:03 -07:00
committed by GitHub
parent 4e58a2a91d
commit 9fb3053007
4 changed files with 97 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ describe Whatsapp::SendOnWhatsappService do
context 'when a valid message' do
let(:whatsapp_request) { instance_double(HTTParty::Response) }
let!(:whatsapp_channel) { create(:channel_whatsapp, sync_templates: false) }
let!(:contact_inbox) { create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: '123456789') }
let!(:conversation) { create(:conversation, contact_inbox: contact_inbox, inbox: whatsapp_channel.inbox) }
let(:api_key) { 'test_key' }
@@ -35,6 +36,21 @@ describe Whatsapp::SendOnWhatsappService do
}
end
let(:named_template_body) do
{
messaging_product: 'whatsapp',
to: '123456789',
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
let(:success_response) { { 'messages' => [{ 'id' => '123456789' }] }.to_json }
it 'calls channel.send_message when with in 24 hour limit' do
@@ -82,6 +98,31 @@ describe Whatsapp::SendOnWhatsappService do
expect(message.reload.source_id).to eq('123456789')
end
it 'calls channel.send_template with named params if template parameter type is NAMED' do
whatsapp_cloud_channel = create(:channel_whatsapp, provider: 'whatsapp_cloud', sync_templates: false, validate_provider_config: false)
cloud_contact_inbox = create(:contact_inbox, inbox: whatsapp_cloud_channel.inbox, source_id: '123456789')
cloud_conversation = create(:conversation, contact_inbox: cloud_contact_inbox, inbox: whatsapp_cloud_channel.inbox)
named_template_params = {
name: 'ticket_status_updated',
language: 'en_US',
category: 'UTILITY',
processed_params: { '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']}" },
:body => named_template_body.to_json
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
message = create(:message,
additional_attributes: { template_params: named_template_params },
content: 'Your package will be delivered in 3 business days.', conversation: cloud_conversation, message_type: :outgoing)
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
it 'calls channel.send_template when template has regexp characters' do
message = create(
:message,