diff --git a/src/lib/automations/engine.ts b/src/lib/automations/engine.ts index 1a131fa..43120ac 100644 --- a/src/lib/automations/engine.ts +++ b/src/lib/automations/engine.ts @@ -315,9 +315,22 @@ async function runStep(step: AutomationStep, args: ExecuteArgs): Promise if (!args.contactId) throw new Error('send_template needs a contact') if (!cfg.template_name) throw new Error('send_template needs template_name') const conversationId = await resolveConversationId(args) + // Meta templates use positional {{1}}, {{2}}, … placeholders, so + // we MUST emit params in strict numeric order. Lexicographic sort + // of "1", "2", …, "10" yields "1", "10", "2", … which silently + // scrambles every template with ≥10 variables. const params = cfg.variables ? Object.keys(cfg.variables) - .sort() + .sort((a, b) => { + const na = Number(a) + const nb = Number(b) + const aNum = Number.isFinite(na) + const bNum = Number.isFinite(nb) + if (aNum && bNum) return na - nb + if (aNum) return -1 + if (bNum) return 1 + return a.localeCompare(b) + }) .map((k) => String(cfg.variables![k])) : [] const { whatsapp_message_id } = await engineSendTemplate({