Merge pull request #37 from ArnasDon/fix/automations-template-param-ordering

fix(automations): order send_template params numerically, not lexicographically
This commit is contained in:
Arnas Donauskas 2026-04-17 21:51:36 +03:00 committed by GitHub
commit 53bb056803
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -315,9 +315,22 @@ async function runStep(step: AutomationStep, args: ExecuteArgs): Promise<string>
if (!args.contactId) throw new Error('send_template needs a contact') if (!args.contactId) throw new Error('send_template needs a contact')
if (!cfg.template_name) throw new Error('send_template needs template_name') if (!cfg.template_name) throw new Error('send_template needs template_name')
const conversationId = await resolveConversationId(args) 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 const params = cfg.variables
? Object.keys(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])) .map((k) => String(cfg.variables![k]))
: [] : []
const { whatsapp_message_id } = await engineSendTemplate({ const { whatsapp_message_id } = await engineSendTemplate({