2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
2020-01-14 08:13:17 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodePropertyOptions,
|
2020-01-14 14:24:54 -08:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
IWebhookResponseData,
|
2020-01-14 08:13:17 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { activeCampaignApiRequest, activeCampaignApiRequestAllItems } from './GenericFunctions';
|
2020-01-14 08:13:17 -08:00
|
|
|
|
|
|
|
export class ActiveCampaignTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2020-10-20 03:11:23 -07:00
|
|
|
displayName: 'ActiveCampaign Trigger',
|
2020-01-14 08:13:17 -08:00
|
|
|
name: 'activeCampaignTrigger',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-01-14 08:13:17 -08:00
|
|
|
icon: 'file:activeCampaign.png',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
2020-10-20 03:11:23 -07:00
|
|
|
description: 'Handle ActiveCampaign events via webhooks',
|
2020-01-14 08:13:17 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'ActiveCampaign Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'activeCampaignApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-01-14 08:13:17 -08:00
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
2022-06-20 07:54:01 -07:00
|
|
|
displayName: 'Event Names or IDs',
|
2020-01-14 08:13:17 -08:00
|
|
|
name: 'events',
|
|
|
|
type: 'multiOptions',
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2020-01-14 08:13:17 -08:00
|
|
|
typeOptions: {
|
|
|
|
loadOptionsMethod: 'getEvents',
|
|
|
|
},
|
|
|
|
default: [],
|
|
|
|
options: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Source',
|
|
|
|
name: 'sources',
|
|
|
|
type: 'multiOptions',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Public',
|
|
|
|
value: 'public',
|
|
|
|
description: 'Run the hooks when a contact triggers the action',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Admin',
|
|
|
|
value: 'admin',
|
|
|
|
description: 'Run the hooks when an admin user triggers the action',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Api',
|
|
|
|
value: 'api',
|
|
|
|
description: 'Run the hooks when an API call triggers the action',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'System',
|
|
|
|
value: 'system',
|
|
|
|
description: 'Run the hooks when automated systems triggers the action',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-01-14 08:13:17 -08:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the events to display them to user so that they can
|
2020-01-14 08:13:17 -08:00
|
|
|
// select them easily
|
|
|
|
async getEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const events = await activeCampaignApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/api/3/webhook/events',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
'webhookEvents',
|
|
|
|
);
|
2020-01-14 08:13:17 -08:00
|
|
|
for (const event of events) {
|
|
|
|
const eventName = event;
|
|
|
|
const eventId = event;
|
|
|
|
returnData.push({
|
|
|
|
name: eventName,
|
|
|
|
value: eventId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-01-14 08:13:17 -08:00
|
|
|
};
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-01-14 08:13:17 -08:00
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
if (webhookData.webhookId === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const endpoint = `/api/3/webhooks/${webhookData.webhookId}`;
|
|
|
|
try {
|
|
|
|
await activeCampaignApiRequest.call(this, 'GET', endpoint, {});
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2020-01-14 08:13:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default') as string;
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
2020-01-14 14:24:54 -08:00
|
|
|
const events = this.getNodeParameter('events', []) as string[];
|
|
|
|
const sources = this.getNodeParameter('sources', '') as string[];
|
2020-01-14 08:13:17 -08:00
|
|
|
const body: IDataObject = {
|
|
|
|
webhook: {
|
|
|
|
name: `n8n-webhook:${webhookUrl}`,
|
|
|
|
url: webhookUrl,
|
|
|
|
events,
|
|
|
|
sources,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-01-14 14:24:54 -08:00
|
|
|
};
|
2022-08-01 13:47:55 -07:00
|
|
|
const { webhook } = await activeCampaignApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/api/3/webhooks',
|
|
|
|
body,
|
|
|
|
);
|
2020-01-14 08:13:17 -08:00
|
|
|
webhookData.webhookId = webhook.id;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
try {
|
2022-08-01 13:47:55 -07:00
|
|
|
await activeCampaignApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/api/3/webhooks/${webhookData.webhookId}`,
|
|
|
|
{},
|
|
|
|
);
|
2020-09-18 00:42:46 -07:00
|
|
|
} catch (error) {
|
2020-01-14 08:13:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete webhookData.webhookId;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
|
|
const req = this.getRequestObject();
|
|
|
|
return {
|
2023-02-27 19:39:43 -08:00
|
|
|
workflowData: [this.helpers.returnJsonArray(req.body as IDataObject[])],
|
2020-01-14 08:13:17 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|