diff --git a/backend/campaigns/tasks.py b/backend/campaigns/tasks.py index fca4300..e42bd47 100644 --- a/backend/campaigns/tasks.py +++ b/backend/campaigns/tasks.py @@ -10,6 +10,7 @@ from .sms_service import send_sms, initiate_call from .models import CampaignLead, SequenceStep from leads.models import BlockedDomain, normalize_domain +from .utils import parse_spintax logger = logging.getLogger(__name__) @@ -458,6 +459,9 @@ def send_email_step(campaign_lead_id, step_id): return subject, body = personalize_email(step.template_subject, step.template_body, clead.lead) + + subject = parse_spintax(subject) + body = parse_spintax(body) account = clead.campaign.connected_account if account: diff --git a/backend/campaigns/utils.py b/backend/campaigns/utils.py index cb8dfb8..4f08b72 100644 --- a/backend/campaigns/utils.py +++ b/backend/campaigns/utils.py @@ -1,12 +1,35 @@ from django.core.signing import Signer, BadSignature +import random +import re signer = Signer() + def generate_unsubscribe_token(lead_id): return signer.sign(str(lead_id)) + def verify_unsubscribe_token(token): try: return signer.unsign(token) except BadSignature: - return None \ No newline at end of file + return None + + +def parse_spintax(text): + if not text: + return text + + pattern = r"\{([^{}]*\|[^{}]*)\}" + + while re.search(pattern, text): + text = re.sub( + pattern, + lambda match: random.choice( + match.group(1).split("|") + ), + text, + count=1, + ) + + return text \ No newline at end of file