n8n-nodes-alive5
Version:
Send and receive SMS messages via alive5
268 lines • 12.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Alive5 = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const alive5Api_utils_1 = require("./alive5Api.utils");
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: '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: 'Send',
value: 'send',
description: 'Send an SMS message',
action: 'Send an SMS message',
},
],
default: 'send',
required: true,
},
{
displayName: 'Channel name or ID',
name: 'channelId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getChannels',
},
displayOptions: {
show: {
resource: ['sms'],
operation: ['send'],
},
},
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: {
show: {
resource: ['sms'],
operation: ['send'],
},
hide: {
channelId: [''],
},
},
},
{
displayName: 'To phone number',
name: 'phoneNumberTo',
type: 'string',
displayOptions: {
show: {
resource: ['sms'],
operation: ['send'],
},
},
default: '',
placeholder: '+1234567890',
description: 'The phone number to send the SMS to',
required: true,
},
{
displayName: 'Message',
name: 'message',
type: 'string',
displayOptions: {
show: {
resource: ['sms'],
operation: ['send'],
},
},
default: '',
description: 'The message to send',
required: true,
},
],
};
this.methods = {
loadOptions: {
async getChannels() {
var _a;
try {
const credentials = (await this.getCredentials('alive5Api'));
let 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`,
});
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) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to load channels: ${error instanceof Error ? error.message : String(error)}`);
}
},
async getAgents() {
var _a;
try {
const credentials = (await this.getCredentials('alive5Api'));
const channelId = this.getNodeParameter('channelId', 0);
if (!channelId) {
return [];
}
let 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`,
});
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) {
return [];
}
},
},
};
}
async execute() {
var _a;
const inputItems = this.getInputData();
const returnData = [];
const credentials = (await this.getCredentials('alive5Api'));
for (let itemIndex = 0; itemIndex < inputItems.length; itemIndex++) {
try {
const resource = this.getNodeParameter('resource', itemIndex);
const operation = this.getNodeParameter('operation', itemIndex);
if (resource !== 'sms') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The resource "${resource}" is not supported`, {
itemIndex,
});
}
if (operation !== 'send') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The operation "${operation}" is not supported`, {
itemIndex,
});
}
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.httpRequestWithAuthentication.call(this, 'alive5Api', {
method: 'GET',
url: `${(0, alive5Api_utils_1.withApiVersion)(credentials.baseUrl, '1.1')}/objects/channels-and-users/list`,
});
response = typeof response !== 'object' ? JSON.parse(response) : response;
const channels = ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.Items) || [];
const channel = channels.find((channel) => channel.channel_id === channelId);
if (!channel) {
throw new n8n_workflow_1.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_1.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_1.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.httpRequestWithAuthentication.call(this, 'alive5Api', {
method: 'POST',
url: `${(0, alive5Api_utils_1.withApiVersion)(credentials.baseUrl, '1.1')}/conversations/sms/send`,
body: requestBody,
headers: {
'Content-Type': 'application/json',
},
json: true,
});
response = typeof response !== 'object' ? JSON.parse(response) : response;
returnData.push({
json: response,
pairedItem: itemIndex,
});
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: this.getInputData(itemIndex)[0].json,
error: error instanceof n8n_workflow_1.NodeOperationError
? error
: new n8n_workflow_1.NodeOperationError(this.getNode(), String(error)),
pairedItem: itemIndex,
});
continue;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error instanceof Error ? error.message : String(error), {
itemIndex,
});
}
}
return [returnData];
}
}
exports.Alive5 = Alive5;
//# sourceMappingURL=Alive5.node.js.map