n8n-nodes-megaapi
Version:
N8N Community Node for MegaAPI WhatsApp automation - Complete WhatsApp integration with messaging, groups, media, and more
90 lines • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteMessage = deleteMessage;
const n8n_workflow_1 = require("n8n-workflow");
const megaapiRequest_1 = require("../megaapiRequest");
async function deleteMessage(itemIndex) {
var _a;
const credentials = await this.getCredentials('megaApiCredentialsApi');
const instanceKey = credentials.instanceKey;
const to = this.getNodeParameter('to', itemIndex);
const keyInput = this.getNodeParameter('key', itemIndex);
console.log('🔐 MegaAPI Credentials loaded');
console.log(`📋 Operation: deleteMessage (item ${itemIndex + 1})`);
console.log(`📱 Instance Key: ${instanceKey}`);
console.log(`👤 To: ${to}`);
try {
let key;
if (typeof keyInput === 'string') {
try {
key = JSON.parse(keyInput);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON format in key field: ${error}`, { itemIndex });
}
}
else {
key = keyInput;
}
if (!key || typeof key !== 'object') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Key must be a valid object', { itemIndex });
}
if (!key.remoteJid || !key.hasOwnProperty('fromMe') || !key.id) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Key must contain remoteJid, fromMe, and id fields', { itemIndex });
}
console.log(`🔑 Key: remoteJid=${key.remoteJid}, fromMe=${key.fromMe}, id=${key.id}`);
if (!key.fromMe) {
console.log('⚠️ Warning: Attempting to delete message not sent by this instance (fromMe: false)');
}
const requestBody = {
messageData: {
to: to,
key: key,
},
};
const apiUrl = `/rest/chat/${instanceKey}/deleteMessage`;
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('🗑️ Deleting 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 deleteData;
if (typeof response === 'string') {
try {
deleteData = JSON.parse(response);
}
catch {
deleteData = { message: response, status: 'unknown' };
}
}
else {
deleteData = response;
}
console.log('✅ Message deletion request sent successfully', typeof deleteData === 'string' ? deleteData.substring(0, 50) + '...' : 'Object received');
return {
json: {
success: true,
operation: 'deleteMessage',
instanceKey,
to,
messageId: key.id,
remoteJid: key.remoteJid,
fromMe: key.fromMe,
message: 'Message deletion request sent successfully',
data: deleteData,
timestamp: new Date().toISOString(),
},
pairedItem: { item: itemIndex },
};
}
catch (error) {
console.log('💥 Error in MegaAPI Delete Message operation');
console.log(error);
throw error;
}
}
//# sourceMappingURL=deleteMessage.js.map