n8n-nodes-megaapi
Version:
N8N Community Node for MegaAPI WhatsApp automation - Complete WhatsApp integration with messaging, groups, media, and more
115 lines • 5.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendListMessage = sendListMessage;
const n8n_workflow_1 = require("n8n-workflow");
const megaapiRequest_1 = require("../megaapiRequest");
async function sendListMessage(itemIndex) {
var _a;
const credentials = await this.getCredentials('megaApiCredentialsApi');
const instanceKey = credentials.instanceKey;
const to = this.getNodeParameter('to', itemIndex);
const buttonText = this.getNodeParameter('buttonText', itemIndex);
const text = this.getNodeParameter('text', itemIndex);
const title = this.getNodeParameter('title', itemIndex);
const description = this.getNodeParameter('description', itemIndex);
const sectionsInput = this.getNodeParameter('sections', itemIndex);
const listType = this.getNodeParameter('listType', itemIndex);
console.log('🔐 MegaAPI Credentials loaded');
console.log(`📋 Operation: sendListMessage (item ${itemIndex + 1})`);
console.log(`📱 Instance Key: ${instanceKey}`);
console.log(`👤 To: ${to}`);
console.log(`🔘 Button Text: ${buttonText}`);
console.log(`💬 Text: ${text}`);
console.log(`📝 Title: ${title}`);
console.log(`📄 Description: ${description}`);
console.log(`🔢 List Type: ${listType}`);
try {
let sections;
if (typeof sectionsInput === 'string') {
try {
sections = JSON.parse(sectionsInput);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON format in sections field: ${error}`, { itemIndex });
}
}
else {
sections = sectionsInput;
}
if (!Array.isArray(sections)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Sections must be an array', { itemIndex });
}
for (let i = 0; i < sections.length; i++) {
const section = sections[i];
if (!section.title || !section.rows || !Array.isArray(section.rows)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Section ${i + 1} must have 'title' and 'rows' array`, { itemIndex });
}
for (let j = 0; j < section.rows.length; j++) {
const row = section.rows[j];
if (!row.title || !row.description || !row.rowId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Row ${j + 1} in section ${i + 1} must have 'title', 'description', and 'rowId'`, { itemIndex });
}
}
}
console.log(`📋 Sections: ${sections.length} sections with ${sections.reduce((total, section) => total + section.rows.length, 0)} total rows`);
const requestBody = {
messageData: {
to: to,
buttonText: buttonText,
text: text,
title: title,
description: description,
sections: sections,
listType: listType,
},
};
const apiUrl = `/rest/sendMessage/${instanceKey}/listMessage`;
console.log(`🌐 Full URL: ${credentials.host}${apiUrl}`);
console.log('📤 Request Options');
console.log(` Method: POST`);
console.log(` Authorization: Bearer ${(_a = credentials.token) === null || _a === void 0 ? void 0 : _a.substring(0, 10)}...`);
console.log(` Content-Type: application/json`);
console.log('📦 Request Body:', JSON.stringify(requestBody, null, 2));
console.log('📋 Sending list message via MegaAPI...');
const response = await megaapiRequest_1.megaapiRequest.call(this, 'POST', apiUrl, requestBody);
console.log('📥 Response received', typeof response === 'string' ? response.substring(0, 50) + '...' : response);
let messageData;
if (typeof response === 'string') {
try {
messageData = JSON.parse(response);
}
catch {
messageData = { message: response, status: 'unknown' };
}
}
else {
messageData = response;
}
console.log('✅ List message sent successfully', typeof messageData === 'string' ? messageData.substring(0, 50) + '...' : 'Object received');
return {
json: {
success: true,
operation: 'sendListMessage',
instanceKey,
to,
buttonText,
text,
title,
description,
sectionsCount: sections.length,
totalRows: sections.reduce((total, section) => total + section.rows.length, 0),
listType,
message: 'List message sent successfully',
data: messageData,
timestamp: new Date().toISOString(),
},
pairedItem: { item: itemIndex },
};
}
catch (error) {
console.log('💥 Error in MegaAPI Send List Message operation');
console.log(error);
throw error;
}
}
//# sourceMappingURL=sendListMessage.js.map