Add Sendy-Node (#1013)

*  Sendy-Node

*  Improvements

*  Small improvement
This commit is contained in:
Ricardo Espinoza 2020-10-13 04:48:20 -04:00 committed by GitHub
parent 6f98caf748
commit 6098384a30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 925 additions and 0 deletions

View file

@ -0,0 +1,24 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class SendyApi implements ICredentialType {
name = 'sendyApi';
displayName = 'Sendy API';
properties = [
{
displayName: 'URL',
name: 'url',
type: 'string' as NodePropertyTypes,
default: '',
placeholder: 'https://yourdomain.com',
},
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,240 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const campaignOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'campaign',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a campaign',
},
],
default: 'create',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const campaignFields = [
/* -------------------------------------------------------------------------- */
/* campaign:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'From Name',
name: 'fromName',
type: 'string',
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
default: '',
description: `The 'From name' of your campaign`,
},
{
displayName: 'From Email',
name: 'fromEmail',
type: 'string',
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
default: '',
description: `The 'From email' of your campaign`,
},
{
displayName: 'Reply To',
name: 'replyTo',
type: 'string',
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
default: '',
description: `The 'Reply to' of your campaign`,
},
{
displayName: 'Title',
name: 'title',
type: 'string',
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
default: '',
description: `The 'Title' of your campaign`,
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
default: '',
description: `The 'Subject' of your campaign`,
},
{
displayName: 'HTML Text',
name: 'htmlText',
type: 'string',
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
default: '',
},
{
displayName: 'Send Campaign',
name: 'sendCampaign',
type: 'boolean',
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
default: false,
description: `Set to true if you want to send the campaign as well and not just create a draft. Default is false.`,
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'campaign',
],
operation: [
'create',
],
},
},
options: [
{
displayName: 'Brand ID',
name: 'brandId',
type: 'string',
displayOptions: {
show: {
'/sendCampaign': [
false,
],
},
},
default: '',
},
{
displayName: 'Exclude List IDs',
name: 'excludeListIds',
type: 'string',
default: '',
description: ` Lists to exclude from your campaign. List IDs should be single or comma-separated`,
},
{
displayName: 'Exclude Segment IDs',
name: 'excludeSegmentIds',
type: 'string',
default: '',
description: `Segments to exclude from your campaign. Segment IDs should be single or comma-separated.`,
},
{
displayName: 'List IDs',
name: 'listIds',
type: 'string',
default: '',
description: `List IDs should be single or comma-separated`,
},
{
displayName: 'Plain Text',
name: 'plainText',
type: 'string',
default: '',
description: `The 'Plain text version' of your campaign`
},
{
displayName: 'Querystring',
name: 'queryString',
type: 'string',
default: '',
description: `Google Analytics tags`,
},
{
displayName: 'Segment IDs',
name: 'segmentIds',
type: 'string',
default: '',
description: `Segment IDs should be single or comma-separated.`,
},
{
displayName: 'Track Clicks',
name: 'trackClicks',
type: 'boolean',
default: true,
description: ` Set to false if you want to disable clicks tracking. Default is true.`,
},
{
displayName: 'Track Opens',
name: 'trackOpens',
type: 'boolean',
default: true,
description: `Set to false if you want to disable opens tracking. Default is true.`,
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,48 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function sendyApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, path: string, body: any = {}, qs: IDataObject = {}, option = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('sendyApi') as IDataObject;
body.api_key = credentials.apiKey;
body.boolean = true;
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method,
form: body,
qs,
uri: `${credentials.url}${path}`,
};
try {
//@ts-ignore
return await this.helpers.request.call(this, options);
} catch (error) {
if (error.response && error.response.body && error.response.body.error) {
let errors = error.response.body.error.errors;
errors = errors.map((e: IDataObject) => e.message);
// Try to return the error prettier
throw new Error(
`Sendy error response [${error.statusCode}]: ${errors.join('|')}`
);
}
throw error;
}
}

View file

@ -0,0 +1,319 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
sendyApiRequest,
} from './GenericFunctions';
import {
campaignFields,
campaignOperations,
} from './CampaignDescription';
import {
subscriberFields,
subscriberOperations,
} from './SubscriberDescription';
export class Sendy implements INodeType {
description: INodeTypeDescription = {
displayName: 'Sendy',
name: 'sendy',
icon: 'file:sendy.png',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Sendy API.',
defaults: {
name: 'Sendy',
color: '#000000',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'sendyApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Campaign',
value: 'campaign',
},
{
name: 'Subscriber',
value: 'subscriber',
},
],
default: 'subscriber',
description: 'The resource to operate on.'
},
...campaignOperations,
...campaignFields,
...subscriberOperations,
...subscriberFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'campaign') {
if (operation === 'create') {
const fromName = this.getNodeParameter('fromName', i) as string;
const fromEmail = this.getNodeParameter('fromEmail', i) as string;
const replyTo = this.getNodeParameter('replyTo', i) as string;
const title = this.getNodeParameter('title', i) as string;
const subject = this.getNodeParameter('subject', i) as string;
const htmlText = this.getNodeParameter('htmlText', i) as boolean;
const sendCampaign = this.getNodeParameter('sendCampaign', i) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IDataObject = {
from_name: fromName,
from_email: fromEmail,
reply_to: replyTo,
title,
subject,
send_campaign: sendCampaign,
html_text: htmlText,
};
if (additionalFields.plainText) {
body.plain_text = additionalFields.plainText;
}
if (additionalFields.listIds) {
body.list_ids = additionalFields.listIds as string;
}
if (additionalFields.segmentIds) {
body.segment_ids = additionalFields.segmentIds as string;
}
if (additionalFields.excludeListIds) {
body.exclude_list_ids = additionalFields.excludeListIds as string;
}
if (additionalFields.excludeSegmentIds) {
body.exclude_segments_ids = additionalFields.excludeSegmentIds as string;
}
if (additionalFields.brandId) {
body.brand_id = additionalFields.brandId as string;
}
if (additionalFields.queryString) {
body.query_string = additionalFields.queryString as string;
}
if (additionalFields.trackOpens) {
body.track_opens = additionalFields.trackOpens as boolean;
}
if (additionalFields.trackClicks) {
body.track_clicks = additionalFields.trackClicks as boolean;
}
responseData = await sendyApiRequest.call(
this,
'POST',
'/api/campaigns/create.php',
body,
);
const success = [
'Campaign created',
'Campaign created and now sending',
];
if (success.includes(responseData)) {
responseData = { message: responseData };
} else {
throw new Error(`Sendy error response [${400}]: ${responseData}`);
}
}
}
if (resource === 'subscriber') {
if (operation === 'add') {
const email = this.getNodeParameter('email', i) as string;
const listId = this.getNodeParameter('listId', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IDataObject = {
email,
list: listId,
};
Object.assign(body, additionalFields);
responseData = await sendyApiRequest.call(
this,
'POST',
'/subscribe',
body,
);
if (responseData === '1') {
responseData = { success: true };
} else {
throw new Error(`Sendy error response [${400}]: ${responseData}`);
}
}
if (operation === 'count') {
const listId = this.getNodeParameter('listId', i) as string;
const body: IDataObject = {
list_id: listId,
};
responseData = await sendyApiRequest.call(
this,
'POST',
'/api/subscribers/active-subscriber-count.php',
body,
);
const errors = [
'No data passed',
'API key not passed',
'Invalid API key',
'List ID not passed',
'List does not exist',
];
if (!errors.includes(responseData)) {
responseData = { count: responseData };
} else {
throw new Error(`Sendy error response [${400}]: ${responseData}`);
}
}
if (operation === 'delete') {
const email = this.getNodeParameter('email', i) as string;
const listId = this.getNodeParameter('listId', i) as string;
const body: IDataObject = {
email,
list_id: listId,
};
responseData = await sendyApiRequest.call(
this,
'POST',
'/api/subscribers/delete.php',
body,
);
if (responseData === '1') {
responseData = { success: true };
} else {
throw new Error(`Sendy error response [${400}]: ${responseData}`);
}
}
if (operation === 'remove') {
const email = this.getNodeParameter('email', i) as string;
const listId = this.getNodeParameter('listId', i) as string;
const body: IDataObject = {
email,
list: listId,
};
responseData = await sendyApiRequest.call(
this,
'POST',
'/unsubscribe',
body,
);
if (responseData === '1') {
responseData = { success: true };
} else {
throw new Error(`Sendy error response [${400}]: ${responseData}`);
}
}
if (operation === 'status') {
const email = this.getNodeParameter('email', i) as string;
const listId = this.getNodeParameter('listId', i) as string;
const body: IDataObject = {
email,
list_id: listId,
};
responseData = await sendyApiRequest.call(
this,
'POST',
'/api/subscribers/subscription-status.php',
body,
);
const status = [
'Subscribed',
'Unsubscribed',
'Unconfirmed',
'Bounced',
'Soft bounced',
'Complained',
];
if (status.includes(responseData)) {
responseData = { status: responseData };
} else {
throw new Error(`Sendy error response [${400}]: ${responseData}`);
}
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,292 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const subscriberOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'subscriber',
],
},
},
options: [
{
name: 'Add',
value: 'add',
description: 'Add a subscriber to a list',
},
{
name: 'Count',
value: 'count',
description: 'Count subscribers',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a subscriber from a list',
},
{
name: 'Remove',
value: 'remove',
description: 'Unsubscribe user from a list',
},
{
name: 'Status',
value: 'status',
description: 'Get the status of subscriber',
},
],
default: 'add',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const subscriberFields = [
/* -------------------------------------------------------------------------- */
/* subscriber:add */
/* -------------------------------------------------------------------------- */
{
displayName: 'Email',
name: 'email',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'add',
],
},
},
default: '',
description: 'Email address of the subscriber.',
},
{
displayName: 'List ID',
name: 'listId',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'add',
],
},
},
default: '',
description: `The list id you want to subscribe a user to.<br>
This encrypted & hashed id can be found under View all lists section named ID.`,
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'add',
],
},
},
options: [
{
displayName: 'Country',
name: 'country',
type: 'string',
default: '',
description: `User's 2 letter country code`,
},
{
displayName: 'GDRP',
name: 'gdpr',
type: 'boolean',
default: false,
description: `If you're signing up EU users in a GDPR compliant manner, set this to "true"`,
},
{
displayName: 'Honeypot',
name: 'hp',
type: 'boolean',
default: false,
description: `Include this 'honeypot' field to prevent spambots from signing up via this API call. When spambots fills in this field, this API call will exit, preventing them from signing up fake addresses to your form. This parameter is only supported in Sendy 3.0 onwards.`,
},
{
displayName: 'IP Address',
name: 'ipaddress',
type: 'string',
default: '',
description: `User's IP address`,
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: `User's name`,
},
{
displayName: 'Referrer',
name: 'referrer',
type: 'string',
default: '',
description: `The URL where the user signed up from`,
},
{
displayName: 'Silent',
name: 'silent',
type: 'boolean',
default: false,
description: `Set to "true" if your list is 'Double opt-in' but you want to bypass that and signup the user to the list as 'Single Opt-in instead' (optional)`,
},
],
},
/* -------------------------------------------------------------------------- */
/* subscriber:count */
/* -------------------------------------------------------------------------- */
{
displayName: 'List ID',
name: 'listId',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'count',
],
},
},
default: '',
description: `The list id you want to subscribe a user to.<br>
This encrypted & hashed id can be found under View all lists section named ID.`,
},
/* -------------------------------------------------------------------------- */
/* subscriber:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Email',
name: 'email',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'delete',
],
},
},
default: '',
description: 'Email address of the subscriber.',
},
{
displayName: 'List ID',
name: 'listId',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'delete',
],
},
},
default: '',
description: `The list id you want to subscribe a user to.<br>
This encrypted & hashed id can be found under View all lists section named ID.`,
},
/* -------------------------------------------------------------------------- */
/* subscriber:remove */
/* -------------------------------------------------------------------------- */
{
displayName: 'Email',
name: 'email',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'remove',
],
},
},
default: '',
description: 'Email address of the subscriber.',
},
{
displayName: 'List ID',
name: 'listId',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'remove',
],
},
},
default: '',
description: `The list id you want to subscribe a user to.<br>
This encrypted & hashed id can be found under View all lists section named ID.`,
},
/* -------------------------------------------------------------------------- */
/* subscriber:status */
/* -------------------------------------------------------------------------- */
{
displayName: 'Email',
name: 'email',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'status',
],
},
},
default: '',
description: 'Email address of the subscriber.',
},
{
displayName: 'List ID',
name: 'listId',
type: 'string',
displayOptions: {
show: {
resource: [
'subscriber',
],
operation: [
'status',
],
},
},
default: '',
description: `The list id you want to subscribe a user to.<br>
This encrypted & hashed id can be found under View all lists section named ID.`,
},
] as INodeProperties[];

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

View file

@ -156,6 +156,7 @@
"dist/credentials/StripeApi.credentials.js",
"dist/credentials/SalesmateApi.credentials.js",
"dist/credentials/SegmentApi.credentials.js",
"dist/credentials/SendyApi.credentials.js",
"dist/credentials/Sftp.credentials.js",
"dist/credentials/Signl4Api.credentials.js",
"dist/credentials/SpotifyOAuth2Api.credentials.js",
@ -355,6 +356,7 @@
"dist/nodes/Switch.node.js",
"dist/nodes/Salesmate/Salesmate.node.js",
"dist/nodes/Segment/Segment.node.js",
"dist/nodes/Sendy/Sendy.node.js",
"dist/nodes/SurveyMonkey/SurveyMonkeyTrigger.node.js",
"dist/nodes/Taiga/Taiga.node.js",
"dist/nodes/Taiga/TaigaTrigger.node.js",