🔀 Merge branch 'RicardoE105-master'

This commit is contained in:
Jan Oberhauser 2020-02-14 16:56:37 -08:00
commit e70272be42
9 changed files with 1052 additions and 0 deletions

View file

@ -0,0 +1,23 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class MailjetEmailApi implements ICredentialType {
name = 'mailjetEmailApi';
displayName = 'Mailjet Email API';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Secret Key',
name: 'secretKey',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,17 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class MailjetSmsApi implements ICredentialType {
name = 'mailjetSmsApi';
displayName = 'Mailjet SMS API';
properties = [
{
displayName: 'Token',
name: 'token',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,429 @@
import { INodeProperties } from 'n8n-workflow';
export const emailOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'email',
],
},
},
options: [
{
name: 'Send',
value: 'send',
description: 'Send a email',
},
{
name: 'Send Template',
value: 'sendTemplate',
description: 'Send a email template',
},
],
default: 'send',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const emailFields = [
/* -------------------------------------------------------------------------- */
/* email:send */
/* -------------------------------------------------------------------------- */
{
displayName: 'From Email',
name: 'fromEmail',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
]
},
},
placeholder: 'admin@example.com',
description: 'The title for the email',
},
{
displayName: 'To Email',
name: 'toEmail',
type: 'string',
required: true,
placeholder: 'info@example.com',
description: 'Email address of the recipient. Multiple ones can be separated by comma.',
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
]
},
},
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
default: '',
placeholder: 'My subject line',
description: 'Subject line of the email.',
},
{
displayName: 'Text',
name: 'text',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
]
},
},
default: '',
description: 'Plain text message of email.',
},
{
displayName: 'HTML',
name: 'html',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
]
},
},
default: '',
description: 'HTML text message of email.',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
],
},
},
options: [
{
displayName: 'Bcc Email',
name: 'bccEmail',
type: 'string',
description: 'Bcc Email address of the recipient. Multiple ones can be separated by comma.',
default: '',
},
{
displayName: 'Cc Addresses',
name: 'ccAddresses',
type: 'string',
description: 'Cc Email address of the recipient. Multiple ones can be separated by comma.',
default: '',
},
{
displayName: 'From Name',
name: 'fromName',
type: 'string',
default: '',
},
{
displayName: 'Priority',
name: 'priority',
type: 'number',
default: 2,
},
{
displayName: 'Template Language',
name: 'templateLanguage',
type: 'boolean',
default: true,
},
{
displayName: 'Track Clicks',
name: 'trackClicks',
type: 'options',
options: [
{
name: 'Account Default',
value: 'account_default',
description: 'Use the values specified in the Mailjet account',
},
{
name: 'Disabled',
value: 'disabled',
description: 'Disable tracking for this message',
},
{
name: 'Enabled',
value: 'enabled',
description: 'Enable tracking for this message',
},
],
description: 'Enable or disable open tracking on this message.',
default: 'account_default',
},
{
displayName: 'Track Opens',
name: 'trackOpens',
type: 'options',
options: [
{
name: 'Account Default',
value: 'account_default',
description: 'Use the values specified in the Mailjet account',
},
{
name: 'Disabled',
value: 'disabled',
description: 'Disable tracking for this message',
},
{
name: 'Enabled',
value: 'enabled',
description: 'Enable tracking for this message',
},
],
description: 'Enable or disable open tracking on this message.',
default: 'account_default',
},
]
},
{
displayName: 'Variables',
name: 'variablesUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'send',
]
},
},
placeholder: 'Add Variable',
default: {},
options: [
{
name: 'variablesValues',
displayName: 'Variable',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
]
},
],
},
/* -------------------------------------------------------------------------- */
/* email:sendTemplate */
/* -------------------------------------------------------------------------- */
{
displayName: 'From Email',
name: 'fromEmail',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
]
},
},
placeholder: 'admin@example.com',
description: 'The title for the email',
},
{
displayName: 'To Email',
name: 'toEmail',
type: 'string',
required: true,
placeholder: 'info@example.com',
description: 'Email address of the recipient. Multiple ones can be separated by comma.',
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
]
},
},
},
{
displayName: 'Template',
name: 'templateId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
]
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
],
},
},
options: [
{
displayName: 'Bcc Email',
name: 'bccEmail',
type: 'string',
description: 'Bcc Recipients of the email separated by ,.',
default: '',
},
{
displayName: 'Cc Email',
name: 'ccEmail',
type: 'string',
description: 'Cc recipients of the email separated by ,.',
default: '',
},
{
displayName: 'From Name',
name: 'fromName',
type: 'string',
default: '',
},
{
displayName: 'Priority',
name: 'priority',
type: 'number',
default: 2,
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
default: '',
},
{
displayName: 'Track Opens',
name: 'trackOpens',
type: 'string',
description: 'Enable or disable open tracking on this message.',
default: '',
},
{
displayName: 'Track Clicks',
name: 'trackClicks',
type: 'string',
description: 'Enable or disable open tracking on this message.',
default: '',
},
{
displayName: 'Template Language',
name: 'templateLanguage',
type: 'boolean',
default: true,
},
]
},
{
displayName: 'Variables',
name: 'variablesUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource: [
'email',
],
operation: [
'sendTemplate',
]
},
},
placeholder: 'Add Variable',
default: {},
options: [
{
name: 'variablesValues',
displayName: 'Variable',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
]
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,62 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import { IDataObject, IHookFunctions } from 'n8n-workflow';
export async function mailjetApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const emailApiCredentials = this.getCredentials('mailjetEmailApi');
let options: OptionsWithUri = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method,
qs,
body,
uri: uri ||`https://api.mailjet.com${resource}`,
json: true
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
if (emailApiCredentials !== undefined) {
const base64Credentials = Buffer.from(`${emailApiCredentials.apiKey}:${emailApiCredentials.secretKey}`).toString('base64');
//@ts-ignore
options.headers['Authorization'] = `Basic ${base64Credentials}`;
} else {
const smsApiCredentials = this.getCredentials('mailjetSmsApi');
//@ts-ignore
options.headers['Authorization'] = `Bearer ${smsApiCredentials.token}`;
}
try {
return await this.helpers.request!(options);
} catch (error) {
if (error.response.body || error.response.body.ErrorMessage) {
throw new Error(`Mailjet Error: response [${error.statusCode}]: ${error.response.body.ErrorMessage}`);
}
throw new Error(error);
}
}
export async function mailjetApiRequestAllItems(this: IExecuteFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
query.Limit = 1000;
query.Offset = 0;
do {
responseData = await mailjetApiRequest.call(this, method, endpoint, body, query, undefined, { resolveWithFullResponse: true });
returnData.push.apply(returnData, responseData.body);
query.Offset = query.Offset + query.Limit;
} while (
responseData.length !== 0
);
return returnData;
}

View file

@ -0,0 +1,286 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeExecutionData,
INodeType,
} from 'n8n-workflow';
import {
mailjetApiRequest,
} from './GenericFunctions';
import {
emailFields,
emailOperations,
} from './EmailDescription';
import {
smsFields,
smsOperations,
} from './SmsDescription';
export class Mailjet implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mailjet',
name: 'mailjet',
icon: 'file:mailjet.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Mailjet API',
defaults: {
name: 'Mailjet',
color: '#ff9f48',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'mailjetEmailApi',
required: true,
displayOptions: {
show: {
resource: [
'email',
],
},
},
},
{
name: 'mailjetSmsApi',
required: true,
displayOptions: {
show: {
resource: [
'sms',
],
},
},
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Email',
value: 'email',
},
{
name: 'SMS',
value: 'sms',
},
],
default: 'email',
description: 'Resource to consume.',
},
...emailOperations,
...emailFields,
...smsOperations,
...smsFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
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 === 'email') {
//https://dev.mailjet.com/email/guides/send-api-v31/#send-a-basic-email
if (operation === 'send') {
const fromEmail = this.getNodeParameter('fromEmail', i) as string;
const htmlBody = this.getNodeParameter('html', i) as string;
const textBody = this.getNodeParameter('text', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const toEmail = (this.getNodeParameter('toEmail', i) as string).split(',') as string[];
const variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[];
const body: IDataObject = {
Messages: [
{
From: {
email: fromEmail,
},
to: [],
Cc: [],
Bcc: [],
Variables: {},
},
],
//SandboxMode: true,
};
for (const email of toEmail) {
//@ts-ignore
body.Messages[0].to.push({
email,
});
}
if (variables) {
for (const variable of variables) {
//@ts-ignore
body.Messages[0].Variables[variable.name] = variable.value;
}
}
if (htmlBody) {
//@ts-ignore
body.Messages[0].HTMLPart = htmlBody;
}
if (textBody) {
//@ts-ignore
body.Messages[0].TextPart = textBody;
}
if (additionalFields.bccEmail) {
const bccEmail = (additionalFields.bccEmail as string).split(',') as string[];
for (const email of bccEmail) {
//@ts-ignore
body.Messages[0].Bcc.push({
email,
});
}
}
if (additionalFields.ccEmail) {
const ccEmail = (additionalFields.ccEmail as string).split(',') as string[];
for (const email of ccEmail) {
//@ts-ignore
body.Messages[0].Cc.push({
email,
});
}
}
if (additionalFields.subject) {
//@ts-ignore
body.Messages[0].subject = additionalFields.subject as string;
}
if (additionalFields.trackOpens) {
//@ts-ignore
body.Messages[0].TrackOpens = additionalFields.trackOpens as string;
}
if (additionalFields.trackClicks) {
//@ts-ignore
body.Messages[0].TrackClicks = additionalFields.trackClicks as string;
}
if (additionalFields.fromName) {
//@ts-ignore
body.Messages[0].From.name = additionalFields.fromName as string;
}
if (additionalFields.templateLanguage) {
//@ts-ignore
body.Messages[0].From.TemplateLanguage = additionalFields.templateLanguage as boolean;
}
if (additionalFields.priority) {
//@ts-ignore
body.Messages[0].Priority = additionalFields.priority as number;
}
responseData = await mailjetApiRequest.call(this, 'POST', '/v3.1/send', body);
responseData = responseData.Messages;
}
//https://dev.mailjet.com/email/guides/send-api-v31/#use-a-template
if (operation === 'sendTemplate') {
const fromEmail = this.getNodeParameter('fromEmail', i) as string;
const templateId = this.getNodeParameter('templateId', i) as string;
const subject = this.getNodeParameter('subject', i) as string;
const variables = (this.getNodeParameter('variablesUi', i) as IDataObject).variablesValues as IDataObject[];
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const toEmail = (this.getNodeParameter('toEmail', i) as string).split(',') as string[];
const body: IDataObject = {
Messages: [
{
From: {
email: fromEmail,
},
Subject: subject,
to: [],
Cc: [],
Bcc: [],
Variables: {},
TemplateID: templateId,
},
],
//SandboxMode: true,
};
for (const email of toEmail) {
//@ts-ignore
body.Messages[0].to.push({
email,
});
}
if (variables) {
for (const variable of variables) {
//@ts-ignore
body.Messages[0].Variables[variable.name] = variable.value;
}
}
if (additionalFields.bccEmail) {
const bccEmail = (additionalFields.bccEmail as string).split(',') as string[];
for (const email of bccEmail) {
//@ts-ignore
body.Messages[0].Bcc.push({
email,
});
}
}
if (additionalFields.ccEmail) {
const ccEmail = (additionalFields.ccEmail as string).split(',') as string[];
for (const email of ccEmail) {
//@ts-ignore
body.Messages[0].Cc.push({
email,
});
}
}
if (additionalFields.trackOpens) {
//@ts-ignore
body.Messages[0].TrackOpens = additionalFields.trackOpens as string;
}
if (additionalFields.trackClicks) {
//@ts-ignore
body.Messages[0].TrackClicks = additionalFields.trackClicks as string;
}
if (additionalFields.fromName) {
//@ts-ignore
body.Messages[0].From.name = additionalFields.fromName as string;
}
if (additionalFields.templateLanguage) {
//@ts-ignore
body.Messages[0].From.TemplateLanguage = additionalFields.templateLanguage as boolean;
}
if (additionalFields.priority) {
//@ts-ignore
body.Messages[0].Priority = additionalFields.priority as number;
}
responseData = await mailjetApiRequest.call(this, 'POST', '/v3.1/send', body);
responseData = responseData.Messages;
}
}
if (resource === 'sms') {
//https://dev.mailjet.com/sms/reference/send-message#v4_post_sms-send
if (operation === 'send') {
const from = this.getNodeParameter('from', i) as string;
const to = this.getNodeParameter('to', i) as boolean;
const text = this.getNodeParameter('text', i) as string;
const body: IDataObject = {
From: from,
To: to,
Text: text,
};
responseData = await mailjetApiRequest.call(this, 'POST', '/v4/sms-send', body);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,143 @@
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeType,
IWebhookResponseData,
} from 'n8n-workflow';
import {
mailjetApiRequest,
} from './GenericFunctions';
export class MailjetTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mailjet Trigger',
name: 'mailjetTrigger',
icon: 'file:mailjet.png',
group: ['trigger'],
version: 1,
description: 'Handle Mailjet events via webhooks',
defaults: {
name: 'Mailjet Trigger',
color: '#ff9f48',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'mailjetEmailApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Event',
name: 'event',
type: 'options',
required: true,
default: 'open',
options: [
{
name: 'email.bounce',
value: 'bounce',
},
{
name: 'email.blocked',
value: 'blocked',
},
{
name: 'email.open',
value: 'open',
},
{
name: 'email.sent',
value: 'sent',
},
{
name: 'email.spam',
value: 'spam',
},
{
name: 'email.unsub',
value: 'unsub',
},
],
description: 'Determines which resource events the webhook is triggered for.',
},
],
};
// @ts-ignore
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const endpoint = `/v3/rest/eventcallbackurl`;
const responseData = await mailjetApiRequest.call(this, 'GET', endpoint);
const event = this.getNodeParameter('event') as string;
const webhookUrl = this.getNodeWebhookUrl('default');
for (const webhook of responseData.Data) {
if (webhook.EventType === event && webhook.Url === webhookUrl) {
// Set webhook-id to be sure that it can be deleted
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.ID as string;
return true;
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string;
const endpoint = '/v3/rest/eventcallbackurl';
const body: IDataObject = {
Url: webhookUrl,
EventType: event,
Status: 'alive',
isBackup: 'false',
};
const { Data } = await mailjetApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = Data[0].ID;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const endpoint = `/v3/rest/eventcallbackurl/${webhookData.webhookId}`;
try {
await mailjetApiRequest.call(this, 'DELETE', endpoint);
} catch(error) {
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
//@ts-ignore
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
}

View file

@ -0,0 +1,88 @@
import { INodeProperties } from 'n8n-workflow';
export const smsOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'sms',
],
},
},
options: [
{
name: 'Send',
value: 'send',
description: 'Send a sms',
},
],
default: 'send',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const smsFields = [
/* -------------------------------------------------------------------------- */
/* sms:send */
/* -------------------------------------------------------------------------- */
{
displayName: 'From',
name: 'from',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'sms',
],
operation: [
'send',
]
},
},
description: 'Customizable sender name. Should be between 3 and 11 characters in length, only alphanumeric characters are allowed.',
},
{
displayName: 'To',
name: 'to',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'sms',
],
operation: [
'send',
]
},
},
description: 'Message recipient. Should be between 3 and 15 characters in length. The number always starts with a plus sign followed by a country code, followed by the number. Phone numbers are expected to comply with the E.164 format.',
},
{
displayName: 'Text',
name: 'text',
type: 'string',
required: true,
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: [
'sms',
],
operation: [
'send',
]
},
},
},
] as INodeProperties[];

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

@ -69,6 +69,8 @@
"dist/credentials/MongoDb.credentials.js",
"dist/credentials/Msg91Api.credentials.js",
"dist/credentials/MySql.credentials.js",
"dist/credentials/MailjetEmailApi.credentials.js",
"dist/credentials/MailjetSmsApi.credentials.js",
"dist/credentials/NextCloudApi.credentials.js",
"dist/credentials/OpenWeatherMapApi.credentials.js",
"dist/credentials/PayPalApi.credentials.js",
@ -167,6 +169,8 @@
"dist/nodes/MoveBinaryData.node.js",
"dist/nodes/Msg91/Msg91.node.js",
"dist/nodes/MySql/MySql.node.js",
"dist/nodes/Mailjet/Mailjet.node.js",
"dist/nodes/Mailjet/MailjetTrigger.node.js",
"dist/nodes/NextCloud/NextCloud.node.js",
"dist/nodes/NoOp.node.js",
"dist/nodes/OpenWeatherMap.node.js",