UNPKG

@poli-digital/n8n-nodes-poli

Version:
265 lines 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SendMediaByContactId = void 0; const n8n_workflow_1 = require("n8n-workflow"); const parameterUtils_1 = require("./utils/parameterUtils"); class SendMediaByContactId { constructor() { this.description = { displayName: 'Send Media By Contact UUID', name: 'sendMediaByContactId', icon: 'file:poli.svg', group: ['output'], version: 1, description: 'Send media to a contact by UUID using form-data', defaults: { name: 'Send Media By Contact UUID', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'poliApi', required: true, }, ], properties: [ { displayName: 'Contact UUID', name: 'contactId', type: 'string', default: '', required: true, description: 'The UUID of the contact to send media to', }, { displayName: 'Account Channel UUID', name: 'accountChannelUuid', type: 'string', default: '9b8bae9e-350d-4084-89e0-c4fe983420ef', required: true, description: 'The account channel UUID', }, { displayName: 'Media Type', name: 'mediaType', type: 'options', options: [ { name: 'Image', value: 'IMAGE', }, { name: 'Document', value: 'DOCUMENT', }, { name: 'Video', value: 'VIDEO', }, { name: 'Audio', value: 'AUDIO', }, ], default: 'IMAGE', required: true, description: 'Type of media to send', }, { displayName: 'Caption', name: 'caption', type: 'string', default: '', description: 'Caption for the media (optional)', }, { displayName: 'File Input Method', name: 'fileInputMethod', type: 'options', options: [ { name: 'Binary Data', value: 'binaryData', description: 'Use binary data from previous node (recommended)', }, { name: 'File Path', value: 'filePath', description: 'Specify file path on the server', }, { name: 'Base64 Data', value: 'base64', description: 'Provide base64 encoded file data', }, ], default: 'binaryData', description: 'Choose how to provide the file', }, { displayName: 'Binary Property Name', name: 'binaryPropertyName', type: 'string', default: 'data', required: true, displayOptions: { show: { fileInputMethod: ['binaryData'], }, }, description: 'Binary data property name that contains the file to send (usually "data")', }, { displayName: 'File Path', name: 'filePath', type: 'string', default: '', required: true, displayOptions: { show: { fileInputMethod: ['filePath'], }, }, description: 'Path to the file on the server (e.g., /tmp/image.jpg)', }, { displayName: 'File Name', name: 'fileName', type: 'string', default: 'file', displayOptions: { show: { fileInputMethod: ['filePath', 'base64'], }, }, description: 'Name for the file (with extension)', }, { displayName: 'Base64 Data', name: 'base64Data', type: 'string', default: '', required: true, displayOptions: { show: { fileInputMethod: ['base64'], }, }, description: 'Base64 encoded file data (without the data:image/jpeg;base64, prefix)', }, { displayName: 'MIME Type', name: 'mimeType', type: 'string', default: 'image/jpeg', displayOptions: { show: { fileInputMethod: ['filePath', 'base64'], }, }, description: 'MIME type of the file (e.g., image/jpeg, image/png, application/pdf)', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const contactId = (0, parameterUtils_1.getParameterSafe)(this, 'contactId', i, '', true); const accountChannelUuid = (0, parameterUtils_1.getParameterSafe)(this, 'accountChannelUuid', i, '9b8bae9e-350d-4084-89e0-c4fe983420ef', true); const mediaType = (0, parameterUtils_1.getParameterSafe)(this, 'mediaType', i, 'IMAGE'); const caption = (0, parameterUtils_1.getParameterSafe)(this, 'caption', i, ''); const fileInputMethod = (0, parameterUtils_1.getParameterSafe)(this, 'fileInputMethod', i, 'binaryData'); const formDataPayload = { provider: 'WHATSAPP', account_channel_uuid: accountChannelUuid, type: 'MEDIA', version: 'v3', direction: 'OUT', components: { attachments: [ { type: mediaType, media: { uploaded: true, caption: caption, }, }, ], }, }; let fileBuffer; let fileName; let mimeType; if (fileInputMethod === 'binaryData') { const binaryPropertyName = (0, parameterUtils_1.getParameterSafe)(this, 'binaryPropertyName', i, 'data', true); const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName); fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName); fileName = binaryData.fileName || 'file'; mimeType = binaryData.mimeType || 'application/octet-stream'; } else if (fileInputMethod === 'filePath') { const fs = require('fs'); const path = require('path'); const filePath = (0, parameterUtils_1.getParameterSafe)(this, 'filePath', i, '', true); const userFileName = (0, parameterUtils_1.getParameterSafe)(this, 'fileName', i, ''); const userMimeType = (0, parameterUtils_1.getParameterSafe)(this, 'mimeType', i, ''); if (!fs.existsSync(filePath)) { throw new Error(`File not found: ${filePath}`); } fileBuffer = fs.readFileSync(filePath); fileName = userFileName || path.basename(filePath); mimeType = userMimeType || 'application/octet-stream'; } else if (fileInputMethod === 'base64') { const base64Data = (0, parameterUtils_1.getParameterSafe)(this, 'base64Data', i, '', true); const userFileName = (0, parameterUtils_1.getParameterSafe)(this, 'fileName', i, 'file'); const userMimeType = (0, parameterUtils_1.getParameterSafe)(this, 'mimeType', i, 'image/jpeg'); fileBuffer = Buffer.from(base64Data, 'base64'); fileName = userFileName; mimeType = userMimeType; } else { throw new Error('Invalid file input method'); } const FormData = require('form-data'); const formData = new FormData(); formData.append('form-data', JSON.stringify(formDataPayload)); formData.append('file', fileBuffer, { filename: fileName, contentType: mimeType, }); const credentials = await this.getCredentials('poliApi'); const baseUrl = 'https://foundation-api.poli.digital/v3'; const endpoint = `/contacts/${contactId}/messages`; const normalizedEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`; const options = { method: 'POST', url: `${baseUrl}${normalizedEndpoint}`, headers: { 'Authorization': `Bearer ${credentials.apiKey}`, ...formData.getHeaders(), }, body: formData, timeout: 60000, }; try { const responseData = await this.helpers.httpRequest(options); returnData.push({ json: responseData }); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } return [returnData]; } } exports.SendMediaByContactId = SendMediaByContactId; //# sourceMappingURL=SendMediaByContactId.operation.js.map