2020-06-17 14:46:34 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} 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,
|
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',
|
|
|
|
icon: 'file:messagebird.png',
|
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"]}}',
|
|
|
|
description: 'Sending SMS',
|
|
|
|
defaults: {
|
|
|
|
name: 'MessageBird',
|
2020-06-17 14:46:34 -07:00
|
|
|
color: '#2481d7',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
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-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
|
|
|
},
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// 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',
|
|
|
|
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: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The status report URL to be used on a per-message basis.<br /> 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: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'The type of message.<br /> Values can be: sms, binary, or flash.',
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Type Details',
|
|
|
|
name: 'typeDetails',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2020-06-17 14:46:34 -07:00
|
|
|
description: 'A hash with extra information.<br /> 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
|
|
|
|
let bodyRequest: IDataObject;
|
|
|
|
// For Query string
|
|
|
|
let qs: IDataObject;
|
|
|
|
|
|
|
|
let requestMethod;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
qs = {};
|
|
|
|
|
|
|
|
resource = this.getNodeParameter('resource', i) as string;
|
|
|
|
operation = this.getNodeParameter('operation', i) as string;
|
|
|
|
|
|
|
|
if (resource === 'sms') {
|
|
|
|
//https://developers.messagebird.com/api/sms-messaging/#sms-api
|
|
|
|
if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
|
|
// sms:send
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
requestMethod = 'POST';
|
|
|
|
const originator = this.getNodeParameter('originator', i) as string;
|
|
|
|
const body = this.getNodeParameter('message', i) as string;
|
|
|
|
|
|
|
|
bodyRequest = {
|
|
|
|
recipients: [],
|
|
|
|
originator,
|
2020-10-22 06:46:03 -07:00
|
|
|
body,
|
2020-06-11 15:57:32 -07:00
|
|
|
};
|
|
|
|
const additionalFields = this.getNodeParameter(
|
|
|
|
'additionalFields',
|
2020-10-22 09:00:28 -07:00
|
|
|
i,
|
2020-06-11 15:57:32 -07:00
|
|
|
) as IDataObject;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const receivers = this.getNodeParameter('recipients', i) as string;
|
|
|
|
|
|
|
|
bodyRequest.recipients = receivers.split(',').map(item => {
|
|
|
|
return parseInt(item, 10);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error(`The operation "${operation}" is not known!`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error(`The resource "${resource}" is not known!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseData = await messageBirdApiRequest.call(
|
|
|
|
this,
|
|
|
|
requestMethod,
|
|
|
|
'/messages',
|
|
|
|
bodyRequest,
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-06-11 15:57:32 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|