UNPKG

n8n-nodes-alive5

Version:

Send SMS messages via alive5

209 lines 9.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Alive5 = void 0; const n8n_workflow_1 = require("n8n-workflow"); const n8n_workflow_2 = require("n8n-workflow"); const BASE_URL = 'https://api.alive5.com/public/1.1'; class Alive5 { constructor() { this.description = { displayName: 'Alive5', name: 'alive5', group: ['communication'], version: 1, description: 'Send SMS messages via alive5 API', defaults: { name: 'Alive5', }, icon: 'file:alive5.svg', inputs: ['main'], outputs: ['main'], credentials: [ { name: 'alive5Api', required: true, }, ], properties: [ { displayName: 'Channel name or ID', name: 'channelId', type: 'options', typeOptions: { loadOptionsMethod: 'getChannels', }, 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, }, { displayName: 'User email or ID', name: 'userId', type: 'options', typeOptions: { loadOptionsDependsOn: ['channelId'], loadOptionsMethod: 'getAgents', }, default: '', description: 'Select a user from the selected channel. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.', required: true, displayOptions: { hide: { channelId: [''], }, }, }, { displayName: 'To phone number', name: 'phoneNumberTo', type: 'string', default: '', placeholder: '+1234567890', description: 'The phone number to send the SMS to', required: true, }, { displayName: 'Message', name: 'message', type: 'string', default: '', description: 'The message to send', required: true, }, ], }; this.methods = { loadOptions: { async getChannels() { var _a; try { let response = await this.helpers.request({ method: 'GET', url: `${BASE_URL}/objects/channels-and-users/list`, headers: { 'X-A5-APIKEY': (await this.getCredentials('alive5Api')).apiKey, }, }); response = typeof response !== 'object' ? JSON.parse(response) : response; const items = ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.Items) || []; const validChannels = items.filter((channel) => channel.channel_phone_number && channel.channel_phone_number !== 'undefined' && channel.channel_phone_number.startsWith('+')); return validChannels.map((channel) => ({ name: channel.channel_label || 'Unnamed Channel', value: channel.channel_id, channel_phone_number: channel.channel_phone_number, })); } catch (error) { n8n_workflow_1.LoggerProxy.error('Error fetching channels:', error); throw new n8n_workflow_2.NodeOperationError(this.getNode(), `Failed to load channels: ${error instanceof Error ? error.message : String(error)}`); } }, async getAgents() { var _a; try { const channelId = this.getNodeParameter('channelId', 0); if (!channelId) { return []; } let response = await this.helpers.request({ method: 'GET', url: `${BASE_URL}/objects/channels-and-users/list`, headers: { 'X-A5-APIKEY': (await this.getCredentials('alive5Api')).apiKey, }, }); response = typeof response !== 'object' ? JSON.parse(response) : response; const items = ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.Items) || []; const channel = items.find((channel) => channel.channel_id === channelId); if (!channel || !channel.agents || !Array.isArray(channel.agents)) { return []; } return channel.agents.map((agent) => ({ name: agent.screen_name || 'Unnamed Agent', value: agent.user_id, })); } catch (error) { n8n_workflow_1.LoggerProxy.error('Error fetching agents:', error); return []; } }, }, }; } async execute() { var _a; const items = this.getInputData(); const returnData = []; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { const channelId = this.getNodeParameter('channelId', itemIndex); const phoneNumberTo = this.getNodeParameter('phoneNumberTo', itemIndex); const message = this.getNodeParameter('message', itemIndex); const userId = this.getNodeParameter('userId', itemIndex); let response = await this.helpers.request({ method: 'GET', url: `${BASE_URL}/objects/channels-and-users/list`, headers: { 'X-A5-APIKEY': (await this.getCredentials('alive5Api')).apiKey, }, }); response = typeof response !== 'object' ? JSON.parse(response) : response; const items = ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.Items) || []; const channel = items.find((channel) => channel.channel_id === channelId); if (!channel) { throw new n8n_workflow_2.NodeOperationError(this.getNode(), `Channel with ID ${channelId} not found`); } const phoneNumberFrom = channel.channel_phone_number; if (!phoneNumberFrom.match(/^\+[1-9]\d{1,14}$/)) { throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'Invalid "From" phone number format. Must be in E.164 format (e.g., +1234567890)'); } if (!phoneNumberTo.match(/^\+[1-9]\d{1,14}$/)) { throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'Invalid "To" phone number format. Must be in E.164 format (e.g., +1234567890)'); } const requestBody = { phone_number_from: phoneNumberFrom, phone_number_to: phoneNumberTo, message: message, channel_id: channelId, user_id: userId, }; response = await this.helpers.request({ method: 'POST', url: `${BASE_URL}/conversations/sms/send`, body: requestBody, headers: { 'X-A5-APIKEY': (await this.getCredentials('alive5Api')).apiKey, 'Content-Type': 'application/json', }, }); response = typeof response !== 'object' ? JSON.parse(response) : response; returnData.push({ json: response, pairedItem: itemIndex, }); } catch (error) { n8n_workflow_1.LoggerProxy.error('Error executing node:', error); if (this.continueOnFail()) { returnData.push({ json: this.getInputData(itemIndex)[0].json, error: error instanceof n8n_workflow_2.NodeOperationError ? error : new n8n_workflow_2.NodeOperationError(this.getNode(), String(error)), pairedItem: itemIndex, }); continue; } throw new n8n_workflow_2.NodeOperationError(this.getNode(), error instanceof Error ? error.message : String(error), { itemIndex, }); } } return [returnData]; } } exports.Alive5 = Alive5; //# sourceMappingURL=Alive5.node.js.map