2020-06-30 13:35:31 -07:00
|
|
|
import {
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-08-03 14:11:57 -07:00
|
|
|
|
2020-06-30 13:35:31 -07:00
|
|
|
import {
|
|
|
|
IDataObject,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2020-06-30 13:35:31 -07:00
|
|
|
IWebhookResponseData,
|
|
|
|
} from 'n8n-workflow';
|
2020-08-03 14:11:57 -07:00
|
|
|
|
2020-06-30 13:35:31 -07:00
|
|
|
import {
|
2020-09-02 03:25:11 -07:00
|
|
|
customerIoApiRequest,
|
2020-08-03 14:11:57 -07:00
|
|
|
eventExists,
|
2020-06-30 13:35:31 -07:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2020-08-04 01:32:51 -07:00
|
|
|
interface IEvent {
|
2020-08-03 14:11:57 -07:00
|
|
|
customer?: IDataObject;
|
|
|
|
email?: IDataObject;
|
|
|
|
push?: IDataObject;
|
|
|
|
slack?: IDataObject;
|
|
|
|
sms?: IDataObject;
|
|
|
|
webhook?: IDataObject;
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:35:31 -07:00
|
|
|
export class CustomerIoTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Customer.io Trigger',
|
2020-08-04 01:32:51 -07:00
|
|
|
name: 'customerIoTrigger',
|
2020-06-30 13:35:31 -07:00
|
|
|
group: ['trigger'],
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:customerio.svg',
|
2020-06-30 13:35:31 -07:00
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Starts the workflow on a Customer.io update (Beta)',
|
2020-06-30 13:35:31 -07:00
|
|
|
defaults: {
|
2020-08-03 14:11:57 -07:00
|
|
|
name: 'Customer.io Trigger',
|
2020-09-02 03:25:11 -07:00
|
|
|
color: '#ffcd00',
|
2020-06-30 13:35:31 -07:00
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'customerIoApi',
|
|
|
|
required: true,
|
2020-08-03 14:11:57 -07:00
|
|
|
},
|
2020-06-30 13:35:31 -07:00
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Events',
|
|
|
|
name: 'events',
|
|
|
|
type: 'multiOptions',
|
2020-08-04 01:32:51 -07:00
|
|
|
required: true,
|
2020-06-30 13:35:31 -07:00
|
|
|
default: [],
|
|
|
|
description: 'The events that can trigger the webhook and whether they are enabled.',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Customer Subscribed',
|
|
|
|
value: 'customer.subscribed',
|
|
|
|
description: 'Whether the webhook is triggered when a list subscriber is added.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Customer Unsubscribe',
|
|
|
|
value: 'customer.unsubscribed',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email Attempted',
|
|
|
|
value: 'email.attempted',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email Bounced',
|
|
|
|
value: 'email.bounced',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email clicked',
|
|
|
|
value: 'email.clicked',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email converted',
|
|
|
|
value: 'email.converted',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email delivered',
|
|
|
|
value: 'email.delivered',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email drafted',
|
|
|
|
value: 'email.drafted',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email failed',
|
|
|
|
value: 'email.failed',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email opened',
|
|
|
|
value: 'email.opened',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email sent',
|
|
|
|
value: 'email.sent',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Email spammed',
|
|
|
|
value: 'email.spammed',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push attempted',
|
|
|
|
value: 'push.attempted',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push bounced',
|
|
|
|
value: 'push.bounced',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push clicked',
|
|
|
|
value: 'push.clicked',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push delivered',
|
|
|
|
value: 'push.delivered',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push drafted',
|
|
|
|
value: 'push.drafted',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push failed',
|
|
|
|
value: 'push.failed',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push opened',
|
|
|
|
value: 'push.opened',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push sent',
|
|
|
|
value: 'push.sent',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Slack attempted',
|
|
|
|
value: 'slack.attempted',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Slack clicked',
|
|
|
|
value: 'slack.clicked',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Slack drafted',
|
|
|
|
value: 'slack.drafted',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Slack failed',
|
|
|
|
value: 'slack.failed',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Slack sent',
|
|
|
|
value: 'slack.sent',
|
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS attempted',
|
2020-08-03 14:11:57 -07:00
|
|
|
value: 'sms.attempted',
|
2020-06-30 13:35:31 -07:00
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS bounced',
|
2020-08-03 14:11:57 -07:00
|
|
|
value: 'sms.bounced',
|
2020-06-30 13:35:31 -07:00
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS clicked',
|
2020-08-03 14:11:57 -07:00
|
|
|
value: 'sms.clicked',
|
2020-06-30 13:35:31 -07:00
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS delivered',
|
2020-08-03 14:11:57 -07:00
|
|
|
value: 'sms.delivered',
|
2020-06-30 13:35:31 -07:00
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS drafted',
|
2020-08-03 14:11:57 -07:00
|
|
|
value: 'sms.drafted',
|
2020-06-30 13:35:31 -07:00
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS failed',
|
2020-08-03 14:11:57 -07:00
|
|
|
value: 'sms.failed',
|
2020-06-30 13:35:31 -07:00
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS sent',
|
2020-08-03 14:11:57 -07:00
|
|
|
value: 'sms.sent',
|
2020-06-30 13:35:31 -07:00
|
|
|
description: 'Whether the webhook is triggered when a list member unsubscribes.',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
// @ts-ignore (because of request)
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
2020-08-03 14:11:57 -07:00
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
|
2020-06-30 13:35:31 -07:00
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
2020-08-03 14:11:57 -07:00
|
|
|
|
|
|
|
const currentEvents = this.getNodeParameter('events', []) as string[];
|
|
|
|
|
|
|
|
const endpoint = '/reporting_webhooks';
|
|
|
|
|
2020-09-02 03:25:11 -07:00
|
|
|
let { reporting_webhooks: webhooks } = await customerIoApiRequest.call(this, 'GET', endpoint, {}, 'beta');
|
2020-08-03 14:11:57 -07:00
|
|
|
|
|
|
|
if (webhooks === null) {
|
|
|
|
webhooks = [];
|
2020-06-30 13:35:31 -07:00
|
|
|
}
|
2020-08-03 14:11:57 -07:00
|
|
|
|
|
|
|
for (const webhook of webhooks) {
|
|
|
|
if (webhook.endpoint === webhookUrl &&
|
|
|
|
eventExists(currentEvents, webhook.events)) {
|
2020-08-04 01:32:51 -07:00
|
|
|
webhookData.webhookId = webhook.id;
|
|
|
|
return true;
|
2020-06-30 13:35:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:11:57 -07:00
|
|
|
return false;
|
2020-06-30 13:35:31 -07:00
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
|
|
let webhook;
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const events = this.getNodeParameter('events', []) as string[];
|
|
|
|
|
2020-08-03 14:11:57 -07:00
|
|
|
const endpoint = '/reporting_webhooks';
|
2020-06-30 13:35:31 -07:00
|
|
|
|
2020-08-03 14:11:57 -07:00
|
|
|
const data: IEvent = {
|
|
|
|
customer: {},
|
|
|
|
email: {},
|
|
|
|
push: {},
|
|
|
|
slack: {},
|
|
|
|
sms: {},
|
|
|
|
webhook: {},
|
|
|
|
};
|
2020-06-30 13:35:31 -07:00
|
|
|
|
2020-08-03 14:11:57 -07:00
|
|
|
for (const event of events) {
|
|
|
|
const option = event.split('.')[1];
|
|
|
|
if (event.startsWith('customer')) {
|
|
|
|
data.customer![option] = true;
|
|
|
|
}
|
|
|
|
if (event.startsWith('email')) {
|
|
|
|
data.email![option] = true;
|
|
|
|
}
|
|
|
|
if (event.startsWith('push')) {
|
|
|
|
data.push![option] = true;
|
|
|
|
}
|
|
|
|
if (event.startsWith('slack')) {
|
|
|
|
data.slack![option] = true;
|
|
|
|
}
|
|
|
|
if (event.startsWith('sms')) {
|
|
|
|
data.sms![option] = true;
|
|
|
|
}
|
|
|
|
if (event.startsWith('webhook')) {
|
|
|
|
data.webhook![option] = true;
|
|
|
|
}
|
2020-06-30 13:35:31 -07:00
|
|
|
}
|
|
|
|
const body = {
|
|
|
|
endpoint: webhookUrl,
|
2020-08-03 14:11:57 -07:00
|
|
|
events: data,
|
|
|
|
};
|
2020-06-30 13:35:31 -07:00
|
|
|
|
2020-09-02 03:25:11 -07:00
|
|
|
webhook = await customerIoApiRequest.call(this, 'POST', endpoint, body, 'beta');
|
2020-06-30 13:35:31 -07:00
|
|
|
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
webhookData.webhookId = webhook.id as string;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
|
|
|
|
if (webhookData.webhookId !== undefined) {
|
|
|
|
const endpoint = `/reporting_webhooks/${webhookData.webhookId}`;
|
|
|
|
try {
|
2020-09-02 03:25:11 -07:00
|
|
|
await customerIoApiRequest.call(this, 'DELETE', endpoint, {}, 'beta');
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2020-06-30 13:35:31 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete webhookData.webhookId;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-06-30 13:35:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
|
|
const bodyData = this.getBodyData();
|
|
|
|
return {
|
|
|
|
workflowData: [
|
2020-10-22 06:46:03 -07:00
|
|
|
this.helpers.returnJsonArray(bodyData),
|
2020-06-30 13:35:31 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|