feat: Add liquid template support for WhatsApp template parameters (#12227)
Extends liquid template processing to WhatsApp `template_params`,
allowing dynamic variable substitution in template parameter values.
Users can now use liquid variables in WhatsApp template parameters:
```
{
"template_params": {
"name": "greet",
"category": "MARKETING",
"language": "en",
"processed_params": {
"body": {
"customer_name": "{{contact.name}}",
"customer_email": "{{contact.email | default: 'no-email@example.com'}}"
}
}
}
}
```
When the message is saved, {{contact.name}} gets replaced with the
actual contact name.
Supported Variables
- {{contact.name}}, {{contact.email}}, {{contact.phone_number}}
- {{agent.name}}, {{agent.first_name}}
- {{account.name}}, {{inbox.name}}
- {{conversation.display_id}}
- Custom attributes: {{contact.custom_attribute.key_name}}
- Liquid filters: {{ contact.email | default: "fallback@example.com" }}
This commit is contained in:
@@ -3,6 +3,7 @@ module Liquidable
|
||||
|
||||
included do
|
||||
before_create :process_liquid_in_content
|
||||
before_create :process_liquid_in_template_params
|
||||
end
|
||||
|
||||
private
|
||||
@@ -35,4 +36,61 @@ module Liquidable
|
||||
# We don't want to process liquid in code blocks
|
||||
content.gsub(/`(.*?)`/m, '{% raw %}`\\1`{% endraw %}')
|
||||
end
|
||||
|
||||
def process_liquid_in_template_params
|
||||
return unless template_params_present? && liquid_processable_template_params?
|
||||
|
||||
processed_params = process_liquid_in_hash(template_params_data['processed_params'])
|
||||
|
||||
# Update the additional_attributes with processed template_params
|
||||
self.additional_attributes = additional_attributes.merge(
|
||||
'template_params' => template_params_data.merge('processed_params' => processed_params)
|
||||
)
|
||||
rescue Liquid::Error
|
||||
# If there is an error in the liquid syntax, we don't want to process it
|
||||
end
|
||||
|
||||
def template_params_present?
|
||||
additional_attributes&.dig('template_params', 'processed_params').present?
|
||||
end
|
||||
|
||||
def liquid_processable_template_params?
|
||||
message_type == 'outgoing' || message_type == 'template'
|
||||
end
|
||||
|
||||
def template_params_data
|
||||
additional_attributes['template_params']
|
||||
end
|
||||
|
||||
def process_liquid_in_hash(hash)
|
||||
return hash unless hash.is_a?(Hash)
|
||||
|
||||
hash.transform_values { |value| process_liquid_value(value) }
|
||||
end
|
||||
|
||||
def process_liquid_value(value)
|
||||
case value
|
||||
when String
|
||||
process_liquid_string(value)
|
||||
when Hash
|
||||
process_liquid_in_hash(value)
|
||||
when Array
|
||||
process_liquid_array(value)
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
def process_liquid_array(array)
|
||||
array.map { |item| process_liquid_value(item) }
|
||||
end
|
||||
|
||||
def process_liquid_string(string)
|
||||
return string if string.blank?
|
||||
|
||||
template = Liquid::Template.parse(string)
|
||||
template.render(message_drops)
|
||||
rescue Liquid::Error
|
||||
string
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user