mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 19:48:08 +00:00
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:
26
app/services/liquid/campaign_template_service.rb
Normal file
26
app/services/liquid/campaign_template_service.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
107
spec/services/liquid/campaign_template_service_spec.rb
Normal file
107
spec/services/liquid/campaign_template_service_spec.rb
Normal file
@@ -0,0 +1,107 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Liquid::CampaignTemplateService do
|
||||
subject(:template_service) { described_class.new(campaign: campaign, contact: contact) }
|
||||
|
||||
let(:account) { create(:account) }
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:contact) { create(:contact, account: account, name: 'John Doe', phone_number: '+1234567890') }
|
||||
let(:campaign) { create(:campaign, account: account, inbox: inbox, sender: agent, message: message_content) }
|
||||
|
||||
describe '#call' do
|
||||
context 'with liquid template variables' do
|
||||
let(:message_content) { 'Hello {{contact.name}}, this is {{agent.name}} from {{account.name}}' }
|
||||
|
||||
it 'processes liquid template correctly' do
|
||||
result = template_service.call(message_content)
|
||||
agent_drop_name = UserDrop.new(agent).name
|
||||
contact_drop_name = ContactDrop.new(contact).name
|
||||
|
||||
expect(result).to eq("Hello #{contact_drop_name}, this is #{agent_drop_name} from #{account.name}")
|
||||
end
|
||||
end
|
||||
|
||||
context 'with code blocks' do
|
||||
let(:message_content) { 'Check this code: `const x = {{contact.name}}`' }
|
||||
|
||||
it 'preserves code blocks without processing liquid' do
|
||||
result = template_service.call(message_content)
|
||||
|
||||
expect(result).to include('`const x = {{contact.name}}`')
|
||||
expect(result).not_to include(contact.name)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with multiline code blocks' do
|
||||
let(:message_content) do
|
||||
<<~MESSAGE
|
||||
Here's some code:
|
||||
```
|
||||
function greet() {
|
||||
return "Hello {{contact.name}}";
|
||||
}
|
||||
```
|
||||
MESSAGE
|
||||
end
|
||||
|
||||
it 'preserves multiline code blocks without processing liquid' do
|
||||
result = template_service.call(message_content)
|
||||
|
||||
expect(result).to include('{{contact.name}}')
|
||||
expect(result).not_to include(contact.name)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with malformed liquid syntax' do
|
||||
let(:message_content) { 'Hello {{contact.name missing closing braces' }
|
||||
|
||||
it 'returns original message when liquid parsing fails' do
|
||||
result = template_service.call(message_content)
|
||||
|
||||
expect(result).to eq(message_content)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with invalid liquid tags' do
|
||||
let(:message_content) { 'Hello {% invalid_tag %} world' }
|
||||
|
||||
it 'returns original message when liquid parsing fails' do
|
||||
result = template_service.call(message_content)
|
||||
|
||||
expect(result).to eq(message_content)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with mixed content' do
|
||||
let(:message_content) { 'Hi {{contact.name}}, use this code: `{{agent.name}}` to contact {{agent.name}}' }
|
||||
|
||||
it 'processes liquid outside code blocks but preserves code blocks' do
|
||||
result = template_service.call(message_content)
|
||||
agent_drop_name = UserDrop.new(agent).name
|
||||
contact_drop_name = ContactDrop.new(contact).name
|
||||
|
||||
expect(result).to include("Hi #{contact_drop_name}")
|
||||
expect(result).to include("contact #{agent_drop_name}")
|
||||
expect(result).to include('`{{agent.name}}`')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with all drop types' do
|
||||
let(:message_content) do
|
||||
'Contact: {{contact.name}}, Agent: {{agent.name}}, Inbox: {{inbox.name}}, Account: {{account.name}}'
|
||||
end
|
||||
|
||||
it 'processes all available drops' do
|
||||
result = template_service.call(message_content)
|
||||
agent_drop_name = UserDrop.new(agent).name
|
||||
contact_drop_name = ContactDrop.new(contact).name
|
||||
|
||||
expect(result).to include("Contact: #{contact_drop_name}")
|
||||
expect(result).to include("Agent: #{agent_drop_name}")
|
||||
expect(result).to include("Inbox: #{inbox.name}")
|
||||
expect(result).to include("Account: #{account.name}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -43,5 +43,14 @@ describe Sms::OneoffSmsCampaignService do
|
||||
assert_requested(:post, 'https://messaging.bandwidth.com/api/v2/users/1/messages', times: 3)
|
||||
expect(campaign.reload.completed?).to be true
|
||||
end
|
||||
|
||||
it 'uses liquid template service to process campaign message' do
|
||||
contact = create(:contact, :with_phone_number, account: account)
|
||||
contact.update_labels([label1.title])
|
||||
|
||||
expect(Liquid::CampaignTemplateService).to receive(:new).with(campaign: campaign, contact: contact).and_call_original
|
||||
|
||||
sms_campaign_service.perform
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,5 +60,15 @@ describe Twilio::OneoffSmsCampaignService do
|
||||
sms_campaign_service.perform
|
||||
expect(campaign.reload.completed?).to be true
|
||||
end
|
||||
|
||||
it 'uses liquid template service to process campaign message' do
|
||||
contact = create(:contact, :with_phone_number, account: account)
|
||||
contact.update_labels([label1.title])
|
||||
|
||||
expect(Liquid::CampaignTemplateService).to receive(:new).with(campaign: campaign, contact: contact).and_call_original
|
||||
expect(twilio_messages).to receive(:create).once
|
||||
|
||||
sms_campaign_service.perform
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user