UNPKG

n8n-nodes-megaapi

Version:

N8N Community Node for MegaAPI WhatsApp automation - Complete WhatsApp integration with messaging, groups, media, and more

118 lines 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addParticipants = addParticipants; const n8n_workflow_1 = require("n8n-workflow"); const megaapiRequest_1 = require("../megaapiRequest"); async function addParticipants(itemIndex) { var _a; const credentials = await this.getCredentials('megaApiCredentialsApi'); const instanceKey = credentials.instanceKey; const groupJid = this.getNodeParameter('groupJid', itemIndex); const participantsString = this.getNodeParameter('participants', itemIndex); console.log('🔐 MegaAPI Credentials loaded'); console.log(`📋 Operation: addParticipants (item ${itemIndex + 1})`); console.log(`📱 Instance Key: ${instanceKey}`); console.log(`🏷️ Group JID: ${groupJid}`); console.log(`👥 Participants String: ${participantsString}`); try { if (!groupJid || !groupJid.includes('@g.us')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Group JID must be in format: group_id@g.us (e.g., 120363360763649404@g.us)', { itemIndex }); } if (!participantsString || participantsString.trim().length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Participants list is required and cannot be empty', { itemIndex }); } const participantsList = participantsString .split(',') .map(participant => participant.trim()) .filter(participant => participant.length > 0); if (participantsList.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'At least one valid participant is required', { itemIndex }); } const validParticipants = []; for (const participant of participantsList) { if (!participant.includes('@s.whatsapp.net')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid participant format: ${participant}. Use format: 551188888888@s.whatsapp.net`, { itemIndex }); } validParticipants.push(participant); } console.log(`👥 Processed Participants (${validParticipants.length}):`); validParticipants.forEach((participant, index) => { console.log(` ${index + 1}. ${participant}`); }); const requestBody = { group_data: { jid: groupJid.trim(), participants: validParticipants } }; const apiUrl = `/rest/group/${instanceKey}/addParticipants`; 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('📋 Adding participants to WhatsApp group 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 responseData; if (typeof response === 'string') { try { responseData = JSON.parse(response); } catch { responseData = { message: response, status: 'unknown' }; } } else { responseData = response; } let operationStatus = 'Unknown'; let addedCount = 0; let failedCount = 0; if (responseData && typeof responseData === 'object') { if (responseData.error === false || responseData.success === true) { operationStatus = 'Success'; } else if (responseData.error === true) { operationStatus = 'Error'; } if (responseData.participants) { addedCount = responseData.participants.filter((p) => p.status === 'added' || p.added === true).length; failedCount = responseData.participants.filter((p) => p.status === 'failed' || p.added === false).length; } else { addedCount = validParticipants.length; } } console.log(`✅ Add participants operation completed`); console.log(` Group JID: ${groupJid}`); console.log(` Operation Status: ${operationStatus}`); console.log(` Participants Requested: ${validParticipants.length}`); console.log(` Participants Added: ${addedCount}`); console.log(` Participants Failed: ${failedCount}`); return { json: { success: operationStatus === 'Success', operation: 'addParticipants', instanceKey, groupJid, operationStatus, participantsRequested: validParticipants.length, participantsAdded: addedCount, participantsFailed: failedCount, participants: validParticipants, message: `Add participants operation completed for group ${groupJid}`, data: responseData, timestamp: new Date().toISOString(), }, pairedItem: { item: itemIndex }, }; } catch (error) { console.log('💥 Error in MegaAPI Add Participants operation'); console.log(error); throw error; } } //# sourceMappingURL=addParticipants.js.map