UNPKG

@poli-digital/n8n-nodes-poli

Version:
172 lines 7.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SendTemplateByPhoneNumber = void 0; const n8n_workflow_1 = require("n8n-workflow"); const transport_1 = require("./transport"); const parameterUtils_1 = require("./utils/parameterUtils"); class SendTemplateByPhoneNumber { constructor() { this.description = { displayName: 'Send Template By Phone Number', name: 'sendTemplateByPhoneNumber', icon: 'file:poli.svg', group: ['output'], version: 1, description: 'Send a template message to a contact by phone number', defaults: { name: 'Send Template By Phone Number', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'poliApi', required: true, }, ], properties: [ { displayName: 'Account UUID', name: 'accountUuid', type: 'string', default: '', required: true, }, { displayName: 'Phone', name: 'contactChannelUid', type: 'string', default: '', required: true, description: 'Phone number in E.164 format', }, { displayName: 'Account Channel UUID', name: 'accountChannelUuid', type: 'string', default: '', required: true, }, { displayName: 'Template UUID', name: 'templateUuid', type: 'string', default: '', required: true, }, { displayName: 'Header Parameters', name: 'headerParams', type: 'fixedCollection', typeOptions: { multipleValues: true }, placeholder: 'Add Header Parameter', default: [], options: [ { name: 'parameter', displayName: 'Parameter', values: [ { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'Text', value: 'text' }, { name: 'Date/Time', value: 'date_time' }, ], default: 'text', }, { displayName: 'Value', name: 'value', type: 'string', default: '', }, ], }, ], }, { displayName: 'Body Parameters', name: 'bodyParams', type: 'fixedCollection', typeOptions: { multipleValues: true }, placeholder: 'Add Body Parameter', default: [], options: [ { name: 'parameter', displayName: 'Parameter', values: [ { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'Text', value: 'text' }, { name: 'Date/Time', value: 'date_time' }, ], default: 'text', }, { displayName: 'Value', name: 'value', type: 'string', default: '', }, ], }, ], }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const accountUuid = (0, parameterUtils_1.getParameterSafe)(this, 'accountUuid', i, '', true); const contactChannelUid = (0, parameterUtils_1.getParameterSafe)(this, 'contactChannelUid', i, ''); const accountChannelUuid = (0, parameterUtils_1.getParameterSafe)(this, 'accountChannelUuid', i, ''); const templateUuid = (0, parameterUtils_1.getParameterSafe)(this, 'templateUuid', i, ''); const headerParams = (0, parameterUtils_1.getParameterSafe)(this, 'headerParams.parameter', i, []); const bodyParams = (0, parameterUtils_1.getParameterSafe)(this, 'bodyParams.parameter', i, []); const components = {}; if (headerParams.length > 0) { components.header = { parameters: headerParams.map((param) => ({ type: param.type, [param.type === 'text' ? 'text' : 'date_time']: param.value, })), }; } if (bodyParams.length > 0) { components.body = { parameters: bodyParams.map((param) => ({ type: param.type, [param.type === 'text' ? 'text' : 'date_time']: param.value, })), }; } const body = { provider: 'WHATSAPP', account_channel_uuid: accountChannelUuid, template_uuid: templateUuid, type: 'TEMPLATE', version: 'v3', components, }; const endpoint = `/accounts/${accountUuid}/contacts/${contactChannelUid}/messages?include=contact`; const responseData = await transport_1.apiRequest.call(this, 'POST', endpoint, body); returnData.push({ json: responseData }); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } return [returnData]; } } exports.SendTemplateByPhoneNumber = SendTemplateByPhoneNumber; //# sourceMappingURL=SendTemplateByPhoneNumber.operation.js.map