UNPKG

n8n-nodes-megaapi

Version:

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

109 lines 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.quoteMessage = quoteMessage; const n8n_workflow_1 = require("n8n-workflow"); const megaapiRequest_1 = require("../megaapiRequest"); async function quoteMessage(itemIndex) { var _a; const credentials = await this.getCredentials('megaApiCredentialsApi'); const instanceKey = credentials.instanceKey; const to = this.getNodeParameter('to', itemIndex); const text = this.getNodeParameter('text', itemIndex); const keyInput = this.getNodeParameter('key', itemIndex); const messageInput = this.getNodeParameter('message', itemIndex); console.log('🔐 MegaAPI Credentials loaded'); console.log(`📋 Operation: quoteMessage (item ${itemIndex + 1})`); console.log(`📱 Instance Key: ${instanceKey}`); console.log(`👤 To: ${to}`); console.log(`💬 Text: ${text.substring(0, 100)}${text.length > 100 ? '...' : ''}`); 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; } let message; if (typeof messageInput === 'string') { try { message = JSON.parse(messageInput); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON format in message field: ${error}`, { itemIndex }); } } else { message = messageInput; } 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 }); } if (!message || typeof message !== 'object') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Message must be a valid object', { itemIndex }); } console.log(`🔑 Key: remoteJid=${key.remoteJid}, fromMe=${key.fromMe}, id=${key.id}`); console.log(`📝 Original Message: ${JSON.stringify(message).substring(0, 100)}${JSON.stringify(message).length > 100 ? '...' : ''}`); const requestBody = { messageData: { to: to, text: text, key: key, message: message, }, }; const apiUrl = `/rest/sendMessage/${instanceKey}/quoteMessage`; 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 quoted 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('✅ Quoted message sent successfully', typeof messageData === 'string' ? messageData.substring(0, 50) + '...' : 'Object received'); return { json: { success: true, operation: 'quoteMessage', instanceKey, to, text, originalMessageId: key.id, originalRemoteJid: key.remoteJid, originalFromMe: key.fromMe, message: 'Quoted message sent successfully', data: messageData, timestamp: new Date().toISOString(), }, pairedItem: { item: itemIndex }, }; } catch (error) { console.log('💥 Error in MegaAPI Quote Message operation'); console.log(error); throw error; } } //# sourceMappingURL=quoteMessage.js.map