UNPKG

n8n-nodes-alive5

Version:

Send and receive SMS messages via alive5

249 lines 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Alive5Trigger = void 0; const n8n_workflow_1 = require("n8n-workflow"); const alive5Api_utils_1 = require("./alive5Api.utils"); function isWebhookUrlAlreadyExistsError(body) { if (!body || typeof body !== 'object') return false; const code = body.code; const error = body.error; return code === 400 && typeof error === 'string' && error.toLowerCase().includes('webhook_url already exists'); } async function callRegisterWebhookUrl(credentials, action, channelId, webhookUrl) { const requestOptions = { method: 'POST', url: `${(0, alive5Api_utils_1.withApiVersion)(credentials.baseUrl, '1.0')}/channel/register-webhook-url`, formData: { action, channel_id: channelId, webhook_url: webhookUrl, }, json: true, simple: false, resolveWithFullResponse: true, }; const response = await this.helpers.requestWithAuthentication.call(this, 'alive5Api', requestOptions); if (response && typeof response === 'object' && 'body' in response) { return { statusCode: response.statusCode, body: response.body, }; } return { body: response }; } function getRegisterWebhookError(body, statusCode) { if (typeof statusCode === 'number') { if (statusCode === 200) return; if (isWebhookUrlAlreadyExistsError(body)) return; return `Alive5 webhook registration failed (HTTP ${statusCode})`; } if (!body || typeof body !== 'object') return; const code = body.code; if (code === 200) return; if (isWebhookUrlAlreadyExistsError(body)) return; if (typeof code === 'number') { return `Alive5 webhook registration failed (code ${code})`; } return; } function normalizeChannelsResponse(response) { const data = typeof response === 'object' && response !== null && 'data' in response ? response.data : response; const items = typeof data === 'object' && data !== null && 'Items' in data ? data.Items : data; if (!Array.isArray(items)) return []; return items .map((c) => { var _a, _b, _c, _d, _e; return ({ id: String((_b = (_a = c.channel_id) !== null && _a !== void 0 ? _a : c.id) !== null && _b !== void 0 ? _b : ''), name: String((_e = (_d = (_c = c.channel_label) !== null && _c !== void 0 ? _c : c.name) !== null && _d !== void 0 ? _d : c.label) !== null && _e !== void 0 ? _e : 'Unnamed Channel'), }); }) .filter((c) => c.id); } class Alive5Trigger { constructor() { this.description = { displayName: 'Alive5 Trigger', name: 'alive5Trigger', icon: 'file:alive5.svg', group: ['trigger'], version: 1, description: 'Triggers when a new message is received in Alive5', defaults: { name: 'Alive5 Trigger', }, inputs: [], outputs: ['main'], credentials: [ { name: 'alive5Api', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'alive5', }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'SMS', value: 'sms', }, ], default: 'sms', required: true, }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['sms'], }, }, options: [ { name: 'On Message Received', value: 'onMessageReceived', description: 'Start workflow when a message is received', action: 'Wait for an incoming SMS message', }, ], default: 'onMessageReceived', required: true, }, { displayName: 'Channel Name or ID', name: 'channelId', type: 'options', typeOptions: { loadOptionsMethod: 'getChannels', }, displayOptions: { show: { resource: ['sms'], operation: ['onMessageReceived'], }, }, default: '', description: 'Select a channel. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.', required: true, }, ], }; this.methods = { loadOptions: { async getChannels() { const credentials = (await this.getCredentials('alive5Api')); let response; try { response = await this.helpers.httpRequestWithAuthentication.call(this, 'alive5Api', { method: 'GET', url: `${(0, alive5Api_utils_1.withApiVersion)(credentials.baseUrl, '1.1')}/channels`, json: true, }); } catch { response = await this.helpers.httpRequestWithAuthentication.call(this, 'alive5Api', { method: 'GET', url: `${(0, alive5Api_utils_1.withApiVersion)(credentials.baseUrl, '1.1')}/objects/channels-and-users/list`, json: true, }); } const channels = normalizeChannelsResponse(response); return channels.map((c) => ({ name: c.name, value: c.id, })); }, }, }; this.webhookMethods = { default: { async checkExists() { const staticData = this.getWorkflowStaticData('node'); const storedChannelId = staticData.channelId; const storedWebhookUrl = staticData.webhookUrl; if (!storedChannelId || !storedWebhookUrl) return false; const channelId = this.getNodeParameter('channelId'); const webhookUrl = this.getNodeWebhookUrl('default'); return Boolean(channelId && webhookUrl && storedChannelId === channelId && storedWebhookUrl === webhookUrl); }, async create() { const credentials = (await this.getCredentials('alive5Api')); const staticData = this.getWorkflowStaticData('node'); const resource = this.getNodeParameter('resource'); const operation = this.getNodeParameter('operation'); if (resource !== 'sms' || operation !== 'onMessageReceived') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Unsupported resource/operation selection for this trigger'); } const channelId = this.getNodeParameter('channelId'); const webhookUrl = this.getNodeWebhookUrl('default'); if (!webhookUrl) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No webhook URL could be determined'); } const { statusCode, body } = await callRegisterWebhookUrl.call(this, credentials, 'add', channelId, webhookUrl); const registerError = getRegisterWebhookError(body, statusCode); if (registerError) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), registerError); } staticData.channelId = channelId; staticData.webhookUrl = webhookUrl; return true; }, async delete() { const credentials = (await this.getCredentials('alive5Api')); const staticData = this.getWorkflowStaticData('node'); const channelId = staticData.channelId; const webhookUrl = staticData.webhookUrl; if (!channelId || !webhookUrl) return true; const { statusCode, body } = await callRegisterWebhookUrl.call(this, credentials, 'remove', channelId, webhookUrl); if (typeof statusCode === 'number' && statusCode !== 200) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to de-register webhook URL (HTTP ${statusCode})`); } if (body && typeof body === 'object' && 'code' in body && body.code !== 200) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to de-register webhook URL (code ${body.code})`); } delete staticData.channelId; delete staticData.webhookUrl; return true; }, }, }; } async webhook() { const body = this.getBodyData(); const returnItem = { json: body, }; return { workflowData: [[returnItem]], webhookResponse: 'OK', }; } } exports.Alive5Trigger = Alive5Trigger; //# sourceMappingURL=Alive5Trigger.node.js.map