UNPKG

n8n-nodes-evolution-api

Version:
97 lines 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendDocument = sendDocument; const n8n_workflow_1 = require("n8n-workflow"); const evolutionRequest_1 = require("../evolutionRequest"); async function sendDocument(ef) { var _a, _b, _c; try { const instanceName = ef.getNodeParameter('instanceName', 0); const remoteJid = ef.getNodeParameter('remoteJid', 0); const media = ef.getNodeParameter('media', 0); if (!media.startsWith('http') && !media.startsWith('data:') && !isBase64(media)) { throw new n8n_workflow_1.NodeApiError(ef.getNode(), { message: 'Formato de mídia inválido', description: 'O documento deve ser uma URL válida ou um base64', }); } function isBase64(str) { try { return Buffer.from(str, 'base64').toString('base64') === str; } catch (err) { return false; } } const mimetype = ef.getNodeParameter('mimetype', 0, 'application/pdf') || 'application/pdf'; const caption = ef.getNodeParameter('caption', 0, ''); const fileName = ef.getNodeParameter('fileName', 0, 'document.pdf') || 'document.pdf'; const options = ef.getNodeParameter('options_message', 0, {}); const body = { number: remoteJid, media: media, mediatype: 'document', mimetype: mimetype, caption: caption || '', fileName: fileName, }; if (options.delay) { body.delay = options.delay; } if ((_b = (_a = options.quoted) === null || _a === void 0 ? void 0 : _a.messageQuoted) === null || _b === void 0 ? void 0 : _b.messageId) { body.quoted = { key: { id: options.quoted.messageQuoted.messageId, }, }; } if ((_c = options.mentions) === null || _c === void 0 ? void 0 : _c.mentionsSettings) { const { mentionsEveryOne, mentioned } = options.mentions.mentionsSettings; if (mentionsEveryOne) { body.mentionsEveryOne = true; } else if (mentioned) { const mentionedNumbers = mentioned.split(',') .map(num => num.trim()) .map(num => num.includes('@s.whatsapp.net') ? num : `${num}@s.whatsapp.net`); body.mentioned = mentionedNumbers; } } const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', }, uri: `/message/sendMedia/${instanceName}`, body, json: true, }; return await (0, evolutionRequest_1.evolutionRequest)(ef, requestOptions); } catch (error) { const errorData = { success: false, error: { message: error.message.includes('Could not get parameter') ? 'Parâmetros inválidos ou ausentes' : 'Erro ao enviar documento', details: error.message.includes('Could not get parameter') ? 'Verifique se todos os campos obrigatórios foram preenchidos corretamente' : error.message, code: error.code || 'UNKNOWN_ERROR', timestamp: new Date().toISOString(), }, }; if (!ef.continueOnFail()) { throw new n8n_workflow_1.NodeOperationError(ef.getNode(), error.message, { message: errorData.error.message, description: errorData.error.details, }); } return { json: errorData, error: errorData, }; } } //# sourceMappingURL=sendDocument.js.map