n8n-nodes-megaapi
Version:
N8N Community Node for MegaAPI WhatsApp automation - Complete WhatsApp integration with messaging, groups, media, and more
66 lines • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendTextMessage = sendTextMessage;
const megaapiRequest_1 = require("../megaapiRequest");
async function sendTextMessage(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);
console.log('🔐 MegaAPI Credentials loaded');
console.log(`📋 Operation: sendTextMessage (item ${itemIndex + 1})`);
console.log(`📱 Instance Key: ${instanceKey}`);
console.log(`👤 To: ${to}`);
console.log(`💬 Text: ${text.substring(0, 50)}${text.length > 50 ? '...' : ''}`);
try {
const requestBody = {
messageData: {
to: to,
text: text,
},
};
const url = `/rest/sendMessage/${instanceKey}/text`;
console.log(`🌐 Full URL: ${credentials.host}${url}`);
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:', requestBody);
console.log('💬 Sending text message via MegaAPI...');
const response = await megaapiRequest_1.megaapiRequest.call(this, 'POST', url, 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('✅ Text message sent successfully', typeof messageData === 'string' ? messageData.substring(0, 50) + '...' : 'Object received');
return {
json: {
success: true,
operation: 'sendTextMessage',
instanceKey,
to,
text,
message: 'Text message sent successfully',
data: messageData,
timestamp: new Date().toISOString(),
},
pairedItem: { item: itemIndex },
};
}
catch (error) {
console.log('💥 Error in MegaAPI Send Text Message operation');
console.log(error);
throw error;
}
}
//# sourceMappingURL=sendTextMessage.js.map