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

@@ -42,20 +42,20 @@ class Contacts::ContactableInboxesService
end
def email_contactable_inbox(inbox)
return unless @contact.email
return if @contact.email.blank?
{ source_id: @contact.email, inbox: inbox }
end
def whatsapp_contactable_inbox(inbox)
return unless @contact.phone_number
return if @contact.phone_number.blank?
# Remove the plus since thats the format 360 dialog uses
{ source_id: @contact.phone_number.delete('+'), inbox: inbox }
end
def sms_contactable_inbox(inbox)
return unless @contact.phone_number
return if @contact.phone_number.blank?
{ source_id: @contact.phone_number, inbox: inbox }
end

View File

@@ -28,14 +28,13 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
message.update!(source_id: message_id) if message_id.present?
end
# rubocop:disable Metrics/CyclomaticComplexity
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 } }
processed_templates_params(template_params)
]
end
@@ -56,7 +55,6 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
end
[nil, nil, nil, nil]
end
# rubocop:enable Metrics/CyclomaticComplexity
def template_match_object(template)
body_object = validated_body_object(template)
@@ -82,6 +80,25 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
Regexp.new template_match_string
end
def template(template_params)
channel.message_templates.find do |t|
t['name'] == template_params['name'] && t['language'] == template_params['language']
end
end
def processed_templates_params(template_params)
template = template(template_params)
return if template.blank?
parameter_format = template['parameter_format']
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
end
def validated_body_object(template)
# we don't care if its not approved template
return if template['status'] != 'approved'