@connectorx/n8n-nodes-cortex 0.1.22 → 0.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/nodes/Cortex/Cortex.node.js +112 -16
- package/package.json +1 -1
|
@@ -210,9 +210,12 @@ class Cortex {
|
|
|
210
210
|
description: 'Optional caption for the media',
|
|
211
211
|
},
|
|
212
212
|
{
|
|
213
|
-
displayName: 'Template
|
|
214
|
-
name: '
|
|
215
|
-
type: '
|
|
213
|
+
displayName: 'Template',
|
|
214
|
+
name: 'template_id',
|
|
215
|
+
type: 'options',
|
|
216
|
+
typeOptions: {
|
|
217
|
+
loadOptionsMethod: 'getTemplates',
|
|
218
|
+
},
|
|
216
219
|
required: true,
|
|
217
220
|
displayOptions: {
|
|
218
221
|
show: {
|
|
@@ -221,11 +224,40 @@ class Cortex {
|
|
|
221
224
|
msg_type: ['template'],
|
|
222
225
|
},
|
|
223
226
|
},
|
|
224
|
-
default: '
|
|
225
|
-
description: '
|
|
227
|
+
default: '',
|
|
228
|
+
description: 'Select an approved WhatsApp template',
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
displayName: 'Template Variables',
|
|
232
|
+
name: 'template_variables',
|
|
233
|
+
type: 'fixedCollection',
|
|
226
234
|
typeOptions: {
|
|
227
|
-
|
|
235
|
+
multipleValues: true,
|
|
236
|
+
},
|
|
237
|
+
displayOptions: {
|
|
238
|
+
show: {
|
|
239
|
+
resource: ['message'],
|
|
240
|
+
operation: ['send'],
|
|
241
|
+
msg_type: ['template'],
|
|
242
|
+
},
|
|
228
243
|
},
|
|
244
|
+
default: {},
|
|
245
|
+
options: [
|
|
246
|
+
{
|
|
247
|
+
name: 'variable',
|
|
248
|
+
displayName: 'Variable',
|
|
249
|
+
values: [
|
|
250
|
+
{
|
|
251
|
+
displayName: 'Value',
|
|
252
|
+
name: 'value',
|
|
253
|
+
type: 'string',
|
|
254
|
+
default: '',
|
|
255
|
+
description: 'Variable value — fill in the same order as shown in the template dropdown',
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
description: 'Fill in the template variable values in the same order as shown in the template dropdown (e.g. "Vars: nome, email" → 1st value = nome, 2nd value = email)',
|
|
229
261
|
},
|
|
230
262
|
{
|
|
231
263
|
displayName: 'Sender ID',
|
|
@@ -456,6 +488,35 @@ class Cortex {
|
|
|
456
488
|
return [];
|
|
457
489
|
}
|
|
458
490
|
},
|
|
491
|
+
async getTemplates() {
|
|
492
|
+
const credentials = await this.getCredentials('cortexApi');
|
|
493
|
+
const baseUrl = credentials.apiBaseUrl.replace(/\/$/, '');
|
|
494
|
+
const options = {
|
|
495
|
+
headers: {
|
|
496
|
+
'Content-Type': 'application/json',
|
|
497
|
+
'Authorization': `Bearer ${credentials.accessToken}`,
|
|
498
|
+
},
|
|
499
|
+
method: 'GET',
|
|
500
|
+
uri: `${baseUrl}/templates`,
|
|
501
|
+
json: true,
|
|
502
|
+
};
|
|
503
|
+
try {
|
|
504
|
+
const response = await this.helpers.request(options);
|
|
505
|
+
const data = Array.isArray(response) ? response : (response.data || []);
|
|
506
|
+
return data.map((t) => {
|
|
507
|
+
const vars = (t.variables || []);
|
|
508
|
+
const varNames = vars.map((v) => v.name).join(', ');
|
|
509
|
+
const label = `${t.name} (${t.language || 'pt_BR'})${varNames ? ' — Vars: ' + varNames : ''}`;
|
|
510
|
+
return {
|
|
511
|
+
name: label,
|
|
512
|
+
value: JSON.stringify({ id: t.id, name: t.name, language: t.language, variables: t.variables }),
|
|
513
|
+
};
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
catch (error) {
|
|
517
|
+
return [];
|
|
518
|
+
}
|
|
519
|
+
},
|
|
459
520
|
},
|
|
460
521
|
};
|
|
461
522
|
}
|
|
@@ -505,18 +566,53 @@ class Cortex {
|
|
|
505
566
|
}
|
|
506
567
|
}
|
|
507
568
|
else if (msgType === 'template') {
|
|
508
|
-
const
|
|
509
|
-
if (
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
569
|
+
const templateDataStr = this.getNodeParameter('template_id', i);
|
|
570
|
+
if (!templateDataStr)
|
|
571
|
+
throw new Error('Please select a template');
|
|
572
|
+
let templateData;
|
|
573
|
+
try {
|
|
574
|
+
templateData = JSON.parse(templateDataStr);
|
|
575
|
+
}
|
|
576
|
+
catch (e) {
|
|
577
|
+
throw new Error('Invalid template selection');
|
|
578
|
+
}
|
|
579
|
+
const varsCollection = (() => { try {
|
|
580
|
+
return this.getNodeParameter('template_variables', i, {});
|
|
581
|
+
}
|
|
582
|
+
catch (_e) {
|
|
583
|
+
return {};
|
|
584
|
+
} })();
|
|
585
|
+
const userValues = ((varsCollection === null || varsCollection === void 0 ? void 0 : varsCollection.variable) || []).map((v) => v.value || '');
|
|
586
|
+
const templateVars = templateData.variables || [];
|
|
587
|
+
const components = [];
|
|
588
|
+
const headerVars = templateVars.filter((v) => v.component === 'header');
|
|
589
|
+
const bodyVars = templateVars.filter((v) => v.component === 'body');
|
|
590
|
+
let valueIdx = 0;
|
|
591
|
+
if (headerVars.length > 0) {
|
|
592
|
+
const params = headerVars.map((tv) => {
|
|
593
|
+
const isPositional = /^Variável \d+$/.test(tv.name);
|
|
594
|
+
const param = { type: 'text', text: userValues[valueIdx++] || '' };
|
|
595
|
+
if (!isPositional)
|
|
596
|
+
param.parameter_name = tv.name;
|
|
597
|
+
return param;
|
|
598
|
+
});
|
|
599
|
+
components.push({ type: 'header', parameters: params });
|
|
516
600
|
}
|
|
517
|
-
|
|
518
|
-
|
|
601
|
+
if (bodyVars.length > 0) {
|
|
602
|
+
const params = bodyVars.map((tv) => {
|
|
603
|
+
const isPositional = /^Variável \d+$/.test(tv.name);
|
|
604
|
+
const param = { type: 'text', text: userValues[valueIdx++] || '' };
|
|
605
|
+
if (!isPositional)
|
|
606
|
+
param.parameter_name = tv.name;
|
|
607
|
+
return param;
|
|
608
|
+
});
|
|
609
|
+
components.push({ type: 'body', parameters: params });
|
|
519
610
|
}
|
|
611
|
+
content = {
|
|
612
|
+
name: templateData.name,
|
|
613
|
+
language: templateData.language || 'pt_BR',
|
|
614
|
+
components,
|
|
615
|
+
};
|
|
520
616
|
}
|
|
521
617
|
const body = {
|
|
522
618
|
conversation_id: conversationId,
|