2020-06-17 14:46:34 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
2020-12-22 23:51:07 -08:00
|
|
|
} from 'n8n-core';
|
2020-06-11 17:40:58 -07:00
|
|
|
|
2020-06-11 15:57:32 -07:00
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
2020-06-17 14:46:34 -07:00
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2020-06-11 15:57:32 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-06-17 14:46:34 -07:00
|
|
|
import {
|
|
|
|
messageBirdApiRequest,
|
|
|
|
} from './GenericFunctions';
|
2020-06-11 15:57:32 -07:00
|
|
|
|
|
|
|
export class MessageBird implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'MessageBird',
|
|
|
|
name: 'messageBird',
|
2021-03-25 09:10:02 -07:00
|
|
|
icon: 'file:messagebird.svg',
|
2020-06-11 17:40:58 -07:00
|
|
|
group: ['output'],
|
2020-06-11 15:57:32 -07:00
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Sends SMS via MessageBird',
|
2020-06-11 15:57:32 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'MessageBird',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'messageBirdApi',
|
2020-06-17 14:46:34 -07:00
|
|
|
required: true,
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'SMS',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 'sms',
|
|
|
|
},
|
2020-12-22 23:51:07 -08:00
|
|
|
{
|
|
|
|
name: 'Balance',
|
|
|
|
value: 'balance',
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
],
|
|
|
|
default: 'sms',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The resource to operate on.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2020-06-17 14:46:34 -07:00
|
|
|
resource: [
|
|
|
|
'sms',
|
|
|
|
],
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Send',
|
|
|
|
value: 'send',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'Send text messages (SMS)',
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
],
|
|
|
|
default: 'send',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The operation to perform.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
2020-12-22 23:51:07 -08:00
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: [
|
|
|
|
'balance',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Get',
|
|
|
|
value: 'get',
|
|
|
|
description: 'Get the balance',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'get',
|
|
|
|
description: 'The operation to perform.',
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// sms:send
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'From',
|
|
|
|
name: 'originator',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '14155238886',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2020-06-17 14:46:34 -07:00
|
|
|
operation: [
|
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
|
|
|
],
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The number from which to send the message.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'To',
|
|
|
|
name: 'recipients',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '14155238886/+14155238886',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2020-06-17 14:46:34 -07:00
|
|
|
operation: [
|
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
|
|
|
],
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'All recipients separated by commas.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Message',
|
|
|
|
name: 'message',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2020-06-17 14:46:34 -07:00
|
|
|
operation: [
|
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
|
|
|
],
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The message to be send.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Additional Fields',
|
|
|
|
name: 'additionalFields',
|
|
|
|
type: 'collection',
|
2020-12-23 03:59:28 -08:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
placeholder: 'Add Fields',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Created Date-time',
|
|
|
|
name: 'createdDatetime',
|
|
|
|
type: 'dateTime',
|
|
|
|
default: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The date and time of the creation of the message in RFC3339 format (Y-m-dTH:i:sP).',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Datacoding',
|
|
|
|
name: 'datacoding',
|
2020-06-11 17:40:58 -07:00
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Auto',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 'auto',
|
2020-06-11 17:40:58 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Plain',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 'plain',
|
2020-06-11 17:40:58 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Unicode',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 'unicode',
|
|
|
|
},
|
2020-06-11 17:40:58 -07:00
|
|
|
],
|
2020-06-11 15:57:32 -07:00
|
|
|
default: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'Using unicode will limit the maximum number of characters to 70 instead of 160.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Gateway',
|
|
|
|
name: 'gateway',
|
|
|
|
type: 'number',
|
|
|
|
default: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The SMS route that is used to send the message.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
2020-06-11 17:40:58 -07:00
|
|
|
displayName: 'Group IDs',
|
2020-06-11 15:57:32 -07:00
|
|
|
name: 'groupIds',
|
|
|
|
placeholder: '1,2',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'Group IDs separated by commas, If provided recipients can be omitted.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
2020-06-11 17:40:58 -07:00
|
|
|
displayName: 'Message Type',
|
2020-06-11 15:57:32 -07:00
|
|
|
name: 'mclass',
|
|
|
|
type: 'options',
|
2020-06-11 17:40:58 -07:00
|
|
|
placeholder: 'Permissible values from 0-3',
|
2020-06-11 15:57:32 -07:00
|
|
|
options: [
|
|
|
|
{
|
2020-06-11 17:40:58 -07:00
|
|
|
name: 'Flash',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 1,
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
2020-06-12 00:39:56 -07:00
|
|
|
{
|
|
|
|
name: 'Normal',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 0,
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
],
|
2020-06-11 17:40:58 -07:00
|
|
|
default: 1,
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'Indicated the message type. 1 is a normal message, 0 is a flash message.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Reference',
|
|
|
|
name: 'reference',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'A client reference.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Report Url',
|
|
|
|
name: 'reportUrl',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2021-11-25 09:10:06 -08:00
|
|
|
description: 'The status report URL to be used on a per-message basis. Reference is required for a status report webhook to be sent.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Scheduled Date-time',
|
|
|
|
name: 'scheduledDatetime',
|
|
|
|
type: 'dateTime',
|
|
|
|
default: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The scheduled date and time of the message in RFC3339 format (Y-m-dTH:i:sP).',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Type',
|
|
|
|
name: 'type',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
2020-06-11 17:40:58 -07:00
|
|
|
name: 'Binary',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 'binary',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
2020-06-11 17:40:58 -07:00
|
|
|
name: 'Flash',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 'flash',
|
2020-06-12 00:39:56 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS',
|
2020-06-17 14:46:34 -07:00
|
|
|
value: 'sms',
|
|
|
|
},
|
2020-06-11 15:57:32 -07:00
|
|
|
],
|
|
|
|
default: '',
|
2021-11-25 09:10:06 -08:00
|
|
|
description: 'The type of message. Values can be: sms, binary, or flash.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Type Details',
|
|
|
|
name: 'typeDetails',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2021-11-25 09:10:06 -08:00
|
|
|
description: 'A hash with extra information. Is only used when a binary message is sent.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Validity',
|
|
|
|
name: 'validity',
|
|
|
|
type: 'number',
|
2020-06-11 17:40:58 -07:00
|
|
|
default: 1,
|
2020-06-11 15:57:32 -07:00
|
|
|
typeOptions: {
|
2020-06-17 14:46:34 -07:00
|
|
|
minValue: 1,
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The amount of seconds that the message is valid.',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2020-06-11 15:57:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let operation: string;
|
|
|
|
let resource: string;
|
|
|
|
|
|
|
|
// For POST
|
2020-12-22 23:51:07 -08:00
|
|
|
let bodyRequest: IDataObject = {};
|
2020-06-11 15:57:32 -07:00
|
|
|
// For Query string
|
|
|
|
let qs: IDataObject;
|
|
|
|
|
|
|
|
let requestMethod;
|
2020-12-22 23:51:07 -08:00
|
|
|
let requestPath;
|
2020-06-11 15:57:32 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
qs = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
resource = this.getNodeParameter('resource', i) as string;
|
|
|
|
operation = this.getNodeParameter('operation', i) as string;
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'sms') {
|
|
|
|
//https://developers.messagebird.com/api/sms-messaging/#sms-api
|
|
|
|
if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
|
|
// sms:send
|
|
|
|
// ----------------------------------
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
requestMethod = 'POST';
|
|
|
|
requestPath = '/messages';
|
|
|
|
const originator = this.getNodeParameter('originator', i) as string;
|
|
|
|
const body = this.getNodeParameter('message', i) as string;
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
bodyRequest = {
|
|
|
|
recipients: [],
|
|
|
|
originator,
|
|
|
|
body,
|
|
|
|
};
|
|
|
|
const additionalFields = this.getNodeParameter(
|
|
|
|
'additionalFields',
|
|
|
|
i,
|
|
|
|
) as IDataObject;
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.groupIds) {
|
|
|
|
bodyRequest.groupIds = additionalFields.groupIds as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.type) {
|
|
|
|
bodyRequest.type = additionalFields.type as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.reference) {
|
|
|
|
bodyRequest.reference = additionalFields.reference as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.reportUrl) {
|
|
|
|
bodyRequest.reportUrl = additionalFields.reportUrl as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.validity) {
|
|
|
|
bodyRequest.validity = additionalFields.reportUrl as number;
|
|
|
|
}
|
|
|
|
if (additionalFields.gateway) {
|
|
|
|
bodyRequest.gateway = additionalFields.gateway as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.typeDetails) {
|
|
|
|
bodyRequest.typeDetails = additionalFields.typeDetails as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.datacoding) {
|
|
|
|
bodyRequest.datacoding = additionalFields.datacoding as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.mclass) {
|
|
|
|
bodyRequest.mclass = additionalFields.mclass as number;
|
|
|
|
}
|
|
|
|
if (additionalFields.scheduledDatetime) {
|
|
|
|
bodyRequest.scheduledDatetime = additionalFields.scheduledDatetime as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.createdDatetime) {
|
|
|
|
bodyRequest.createdDatetime = additionalFields.createdDatetime as string;
|
|
|
|
}
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const receivers = this.getNodeParameter('recipients', i) as string;
|
|
|
|
bodyRequest.recipients = receivers.split(',').map(item => {
|
|
|
|
|
|
|
|
return parseInt(item, 10);
|
|
|
|
});
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
else {
|
|
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'balance') {
|
|
|
|
requestMethod = 'GET';
|
|
|
|
requestPath = '/balance';
|
2020-12-22 23:51:07 -08:00
|
|
|
}
|
|
|
|
else {
|
2021-07-19 23:58:54 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `The resource "${resource}" is not known!`);
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
2020-12-22 23:51:07 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const responseData = await messageBirdApiRequest.call(
|
|
|
|
this,
|
|
|
|
requestMethod,
|
|
|
|
requestPath,
|
|
|
|
bodyRequest,
|
|
|
|
qs,
|
|
|
|
);
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|