2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
twilioApiRequest,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
export class Twilio implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Twilio',
|
|
|
|
name: 'twilio',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:twilio.svg',
|
2019-06-23 03:35:23 -07:00
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
2019-07-14 09:53:17 -07:00
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2019-06-23 03:35:23 -07:00
|
|
|
description: 'Send SMS and WhatsApp messages or make phone calls',
|
|
|
|
defaults: {
|
|
|
|
name: 'Twilio',
|
|
|
|
color: '#cf272d',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'twilioApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
properties: [
|
2019-07-14 09:53:17 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'SMS',
|
|
|
|
value: 'sms',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'sms',
|
|
|
|
description: 'The resource to operate on.',
|
|
|
|
},
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2019-07-14 09:53:17 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: [
|
|
|
|
'sms',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
options: [
|
|
|
|
{
|
2019-07-14 09:53:17 -07:00
|
|
|
name: 'Send',
|
|
|
|
value: 'send',
|
2019-06-23 03:35:23 -07:00
|
|
|
description: 'Send SMS/MMS/WhatsApp message',
|
|
|
|
},
|
|
|
|
],
|
2019-07-14 09:53:17 -07:00
|
|
|
default: 'send',
|
2019-06-23 03:35:23 -07:00
|
|
|
description: 'The operation to perform.',
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
2019-07-14 09:53:17 -07:00
|
|
|
// sms
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// sms:send
|
2019-06-23 03:35:23 -07:00
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'From',
|
|
|
|
name: 'from',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '+14155238886',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
2019-07-14 09:53:17 -07:00
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
description: 'The number from which to send the message',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'To',
|
|
|
|
name: 'to',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '+14155238886',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
2019-07-14 09:53:17 -07:00
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
description: 'The number to which to send the message',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'To Whatsapp',
|
|
|
|
name: 'toWhatsapp',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
2019-07-14 09:53:17 -07:00
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
description: 'If the message should be send to WhatsApp',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Message',
|
|
|
|
name: 'message',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
2019-07-14 09:53:17 -07:00
|
|
|
'send',
|
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'sms',
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
description: 'The message to send',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
2019-07-14 09:53:17 -07:00
|
|
|
let operation: string;
|
|
|
|
let resource: string;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
// For Post
|
|
|
|
let body: IDataObject;
|
|
|
|
// For Query string
|
|
|
|
let qs: IDataObject;
|
|
|
|
|
|
|
|
let requestMethod: string;
|
|
|
|
let endpoint: string;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
requestMethod = 'GET';
|
|
|
|
endpoint = '';
|
|
|
|
body = {};
|
|
|
|
qs = {};
|
|
|
|
|
|
|
|
resource = this.getNodeParameter('resource', i) as string;
|
|
|
|
operation = this.getNodeParameter('operation', i) as string;
|
|
|
|
|
|
|
|
if (resource === 'sms') {
|
|
|
|
if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
|
|
// sms:send
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
requestMethod = 'POST';
|
|
|
|
endpoint = '/Messages.json';
|
|
|
|
|
|
|
|
body.From = this.getNodeParameter('from', i) as string;
|
|
|
|
body.To = this.getNodeParameter('to', i) as string;
|
|
|
|
body.Body = this.getNodeParameter('message', i) as string;
|
|
|
|
|
|
|
|
const toWhatsapp = this.getNodeParameter('toWhatsapp', i) as boolean;
|
|
|
|
|
|
|
|
if (toWhatsapp === true) {
|
|
|
|
body.From = `whatsapp:${body.From}`;
|
|
|
|
body.To = `whatsapp:${body.To}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
|
2019-07-14 09:53:17 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `The resource "${resource}" is not known!`);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const responseData = await twilioApiRequest.call(this, requestMethod, endpoint, body, qs);
|
2019-06-23 03:35:23 -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;
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|