feat: Add liquid processing for SMS campaigns (#10981)

Liquid template processing for SMS campaigns
fixes: https://github.com/chatwoot/chatwoot/issues/10980

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Michael Choi
2025-06-11 12:16:44 -05:00
committed by GitHub
parent 7a67799f35
commit 68bfbc7eb0
6 changed files with 156 additions and 2 deletions

View File

@@ -0,0 +1,26 @@
class Liquid::CampaignTemplateService
pattr_initialize [:campaign!, :contact!]
def call(message)
process_liquid_in_content(message_drops, message)
end
private
def message_drops
{
'contact' => ContactDrop.new(contact),
'agent' => UserDrop.new(campaign.sender),
'inbox' => InboxDrop.new(campaign.inbox),
'account' => AccountDrop.new(campaign.account)
}
end
def process_liquid_in_content(drops, message)
message = message.gsub(/`(.*?)`/m, '{% raw %}`\\1`{% endraw %}')
template = Liquid::Template.parse(message)
template.render(drops)
rescue Liquid::Error
message
end
end

View File

@@ -22,7 +22,8 @@ class Sms::OneoffSmsCampaignService
campaign.account.contacts.tagged_with(audience_labels, any: true).each do |contact|
next if contact.phone_number.blank?
send_message(to: contact.phone_number, content: campaign.message)
content = Liquid::CampaignTemplateService.new(campaign: campaign, contact: contact).call(campaign.message)
send_message(to: contact.phone_number, content: content)
end
end

View File

@@ -22,7 +22,8 @@ class Twilio::OneoffSmsCampaignService
campaign.account.contacts.tagged_with(audience_labels, any: true).each do |contact|
next if contact.phone_number.blank?
channel.send_message(to: contact.phone_number, body: campaign.message)
content = Liquid::CampaignTemplateService.new(campaign: campaign, contact: contact).call(campaign.message)
channel.send_message(to: contact.phone_number, body: content)
end
end
end