UNPKG

n8n-nodes-megaapi

Version:

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

120 lines 5.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeParticipants = removeParticipants; const n8n_workflow_1 = require("n8n-workflow"); const megaapiRequest_1 = require("../megaapiRequest"); async function removeParticipants(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: removeParticipants (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., 120363041490582303@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}/removeParticipants`; 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: text/plain`); console.log('📋 Request Body:', JSON.stringify(requestBody, null, 2)); console.log('📋 Removing participants from WhatsApp group via MegaAPI...'); const response = await megaapiRequest_1.megaapiRequest.call(this, 'POST', apiUrl, requestBody, { 'Content-Type': 'text/plain' }); 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 removedCount = 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) { removedCount = responseData.participants.filter((p) => p.status === 'removed' || p.removed === true).length; failedCount = responseData.participants.filter((p) => p.status === 'failed' || p.removed === false).length; } else { removedCount = validParticipants.length; } } console.log(`✅ Remove participants operation completed`); console.log(` Group JID: ${groupJid}`); console.log(` Operation Status: ${operationStatus}`); console.log(` Participants Requested: ${validParticipants.length}`); console.log(` Participants Removed: ${removedCount}`); console.log(` Participants Failed: ${failedCount}`); return { json: { success: operationStatus === 'Success', operation: 'removeParticipants', instanceKey, groupJid, operationStatus, participantsRequested: validParticipants.length, participantsRemoved: removedCount, participantsFailed: failedCount, participants: validParticipants, message: `Remove participants operation completed for group ${groupJid}`, data: responseData, timestamp: new Date().toISOString(), }, pairedItem: { item: itemIndex }, }; } catch (error) { console.log('💥 Error in MegaAPI Remove Participants operation'); console.log(error); throw error; } } //# sourceMappingURL=removeParticipants.js.map