Files
leadchat/app/services/contacts/contactable_inboxes_service.rb
Pranav 9fb3053007 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
2025-03-28 14:07:03 -07:00

74 lines
2.0 KiB
Ruby

class Contacts::ContactableInboxesService
pattr_initialize [:contact!]
def get
account = contact.account
account.inboxes.filter_map { |inbox| get_contactable_inbox(inbox) }
end
private
def get_contactable_inbox(inbox)
case inbox.channel_type
when 'Channel::TwilioSms'
twilio_contactable_inbox(inbox)
when 'Channel::Whatsapp'
whatsapp_contactable_inbox(inbox)
when 'Channel::Sms'
sms_contactable_inbox(inbox)
when 'Channel::Email'
email_contactable_inbox(inbox)
when 'Channel::Api'
api_contactable_inbox(inbox)
when 'Channel::WebWidget'
website_contactable_inbox(inbox)
end
end
def website_contactable_inbox(inbox)
latest_contact_inbox = inbox.contact_inboxes.where(contact: @contact).last
return unless latest_contact_inbox
# FIXME : change this when multiple conversations comes in
return if latest_contact_inbox.conversations.present?
{ source_id: latest_contact_inbox.source_id, inbox: inbox }
end
def api_contactable_inbox(inbox)
latest_contact_inbox = inbox.contact_inboxes.where(contact: @contact).last
source_id = latest_contact_inbox&.source_id || SecureRandom.uuid
{ source_id: source_id, inbox: inbox }
end
def email_contactable_inbox(inbox)
return if @contact.email.blank?
{ source_id: @contact.email, inbox: inbox }
end
def whatsapp_contactable_inbox(inbox)
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 if @contact.phone_number.blank?
{ source_id: @contact.phone_number, inbox: inbox }
end
def twilio_contactable_inbox(inbox)
return if @contact.phone_number.blank?
case inbox.channel.medium
when 'sms'
{ source_id: @contact.phone_number, inbox: inbox }
when 'whatsapp'
{ source_id: "whatsapp:#{@contact.phone_number}", inbox: inbox }
end
end
end