mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 20:24:05 -08:00
1d46983b24
## Summary Unify `severity` and `level` for all backend application errors for Sentry Follow-up to: https://github.com/n8n-io/n8n/pull/7914#issuecomment-1840433542 ... #### How to test the change: 1. ... ## Issues fixed Include links to Github issue or Community forum post or **Linear ticket**: > Important in order to close automatically and provide context to reviewers ... ## Review / Merge checklist - [ ] PR title and summary are descriptive. **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** ([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md)) - [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [ ] Tests included. > A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. > > *(internal)* You can use Slack commands to trigger [e2e tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227) or [deploy test instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce) or [deploy early access version on Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
313 lines
8 KiB
TypeScript
313 lines
8 KiB
TypeScript
import { createHmac } from 'crypto';
|
|
import {
|
|
NodeOperationError,
|
|
type IDataObject,
|
|
type IHookFunctions,
|
|
type INodeType,
|
|
type INodeTypeDescription,
|
|
type IWebhookFunctions,
|
|
type IWebhookResponseData,
|
|
} from 'n8n-workflow';
|
|
import {
|
|
appWebhookSubscriptionCreate,
|
|
appWebhookSubscriptionDelete,
|
|
appWebhookSubscriptionList,
|
|
facebookEntityDetail,
|
|
installAppOnPage,
|
|
} from './GenericFunctions';
|
|
import { listSearch } from './methods';
|
|
import type { FacebookForm, FacebookFormLeadData, FacebookPageEvent } from './types';
|
|
|
|
export class FacebookLeadAdsTrigger implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Facebook Lead Ads Trigger',
|
|
name: 'facebookLeadAdsTrigger',
|
|
icon: 'file:facebook.svg',
|
|
group: ['trigger'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["event"]}}',
|
|
description: 'Handle Facebook Lead Ads events via webhooks',
|
|
defaults: {
|
|
name: 'Facebook Lead Ads Trigger',
|
|
},
|
|
inputs: [],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'facebookLeadAdsOAuth2Api',
|
|
required: true,
|
|
},
|
|
],
|
|
webhooks: [
|
|
{
|
|
name: 'setup',
|
|
httpMethod: 'GET',
|
|
responseMode: 'onReceived',
|
|
path: 'webhook',
|
|
},
|
|
{
|
|
name: 'default',
|
|
httpMethod: 'POST',
|
|
responseMode: 'onReceived',
|
|
path: 'webhook',
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName:
|
|
'Due to Facebook API limitations, you can use just one Facebook Lead Ads trigger for each Facebook App',
|
|
name: 'facebookLeadAdsNotice',
|
|
type: 'notice',
|
|
default: '',
|
|
},
|
|
{
|
|
displayName: 'Event',
|
|
name: 'event',
|
|
type: 'options',
|
|
required: true,
|
|
default: 'newLead',
|
|
options: [
|
|
{
|
|
name: 'New Lead',
|
|
value: 'newLead',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
displayName: 'Page',
|
|
name: 'page',
|
|
type: 'resourceLocator',
|
|
default: { mode: 'list', value: '' },
|
|
required: true,
|
|
description: 'The page linked to the form for retrieving new leads',
|
|
modes: [
|
|
{
|
|
displayName: 'From List',
|
|
name: 'list',
|
|
type: 'list',
|
|
typeOptions: {
|
|
searchListMethod: 'pageList',
|
|
},
|
|
},
|
|
{
|
|
displayName: 'By ID',
|
|
name: 'id',
|
|
type: 'string',
|
|
placeholder: '121637951029080',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
displayName: 'Form',
|
|
name: 'form',
|
|
type: 'resourceLocator',
|
|
default: { mode: 'list', value: '' },
|
|
required: true,
|
|
description: 'The form to monitor for fetching lead details upon submission',
|
|
modes: [
|
|
{
|
|
displayName: 'From List',
|
|
name: 'list',
|
|
type: 'list',
|
|
typeOptions: {
|
|
searchListMethod: 'formList',
|
|
},
|
|
},
|
|
{
|
|
displayName: 'By ID',
|
|
name: 'id',
|
|
type: 'string',
|
|
placeholder: '121637951029080',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
displayName: 'Options',
|
|
name: 'options',
|
|
type: 'collection',
|
|
placeholder: 'Add Option',
|
|
default: {},
|
|
options: [
|
|
{
|
|
displayName: 'Simplify Output',
|
|
name: 'simplifyOutput',
|
|
type: 'boolean',
|
|
default: true,
|
|
description:
|
|
'Whether to return a simplified version of the webhook event instead of all fields',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
listSearch,
|
|
};
|
|
|
|
webhookMethods = {
|
|
default: {
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
const webhookUrl = this.getNodeWebhookUrl('default') as string;
|
|
const credentials = await this.getCredentials('facebookLeadAdsOAuth2Api');
|
|
const appId = credentials.clientId as string;
|
|
|
|
const webhooks = await appWebhookSubscriptionList.call(this, appId);
|
|
|
|
const subscription = webhooks.find(
|
|
(webhook) =>
|
|
webhook.object === 'page' &&
|
|
webhook.fields.find((field) => field.name === 'leadgen') &&
|
|
webhook.active,
|
|
);
|
|
|
|
if (!subscription) {
|
|
return false;
|
|
}
|
|
|
|
if (subscription.callback_url !== webhookUrl) {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
`The Facebook App ID ${appId} already has a webhook subscription. Delete it or use another App before executing the trigger. Due to Facebook API limitations, you can have just one trigger per App.`,
|
|
{ level: 'warning' },
|
|
);
|
|
}
|
|
|
|
return true;
|
|
},
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
const webhookUrl = this.getNodeWebhookUrl('default') as string;
|
|
const credentials = await this.getCredentials('facebookLeadAdsOAuth2Api');
|
|
const appId = credentials.clientId as string;
|
|
const pageId = this.getNodeParameter('page', '', { extractValue: true }) as string;
|
|
const verifyToken = this.getNode().id;
|
|
|
|
await appWebhookSubscriptionCreate.call(this, appId, {
|
|
object: 'page',
|
|
callback_url: webhookUrl,
|
|
verify_token: verifyToken,
|
|
fields: ['leadgen'],
|
|
include_values: true,
|
|
});
|
|
|
|
await installAppOnPage.call(this, pageId, 'leadgen');
|
|
|
|
return true;
|
|
},
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
const credentials = await this.getCredentials('facebookLeadAdsOAuth2Api');
|
|
const appId = credentials.clientId as string;
|
|
|
|
await appWebhookSubscriptionDelete.call(this, appId, 'page');
|
|
|
|
return true;
|
|
},
|
|
},
|
|
};
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
const bodyData = this.getBodyData() as unknown as FacebookPageEvent;
|
|
const query = this.getQueryData() as IDataObject;
|
|
const res = this.getResponseObject();
|
|
const req = this.getRequestObject();
|
|
const headerData = this.getHeaderData() as IDataObject;
|
|
const credentials = await this.getCredentials('facebookLeadAdsOAuth2Api');
|
|
const pageId = this.getNodeParameter('page', '', { extractValue: true }) as string;
|
|
const formId = this.getNodeParameter('form', '', { extractValue: true }) as string;
|
|
|
|
// Check if we're getting facebook's challenge request (https://developers.facebook.com/docs/graph-api/webhooks/getting-started)
|
|
if (this.getWebhookName() === 'setup') {
|
|
if (query['hub.challenge']) {
|
|
if (this.getNode().id !== query['hub.verify_token']) {
|
|
return {};
|
|
}
|
|
|
|
res.status(200).send(query['hub.challenge']).end();
|
|
|
|
return { noWebhookResponse: true };
|
|
}
|
|
}
|
|
|
|
const computedSignature = createHmac('sha256', credentials.clientSecret as string)
|
|
.update(req.rawBody)
|
|
.digest('hex');
|
|
if (headerData['x-hub-signature-256'] !== `sha256=${computedSignature}`) {
|
|
return {};
|
|
}
|
|
|
|
if (bodyData.object !== 'page') {
|
|
return {};
|
|
}
|
|
|
|
const events = await Promise.all(
|
|
bodyData.entry
|
|
.map((entry) =>
|
|
entry.changes
|
|
.filter(
|
|
(change) =>
|
|
change.field === 'leadgen' &&
|
|
change.value.page_id === pageId &&
|
|
change.value.form_id === formId,
|
|
)
|
|
.map((change) => change.value),
|
|
)
|
|
.flat()
|
|
.map(async (event) => {
|
|
const [lead, form] = await Promise.all([
|
|
facebookEntityDetail.call(
|
|
this,
|
|
event.leadgen_id,
|
|
'field_data,created_time,ad_id,ad_name,adset_id,adset_name,form_id',
|
|
) as Promise<FacebookFormLeadData>,
|
|
facebookEntityDetail.call(
|
|
this,
|
|
event.form_id,
|
|
'id,name,locale,status,page,questions',
|
|
) as Promise<FacebookForm>,
|
|
]);
|
|
|
|
const simplifyOutput = this.getNodeParameter('options.simplifyOutput', true) as boolean;
|
|
|
|
if (simplifyOutput) {
|
|
return {
|
|
id: lead.id,
|
|
data: lead.field_data.reduce(
|
|
(acc, field) => ({ ...acc, [field.name]: field.values[0] }),
|
|
{},
|
|
),
|
|
form: {
|
|
id: form.id,
|
|
name: form.name,
|
|
locale: form.locale,
|
|
status: form.status,
|
|
},
|
|
ad: { id: lead.ad_id, name: lead.ad_name },
|
|
adset: { id: lead.adset_id, name: lead.adset_name },
|
|
page: form.page,
|
|
created_time: lead.created_time,
|
|
};
|
|
}
|
|
|
|
return {
|
|
id: lead.id,
|
|
field_data: lead.field_data,
|
|
form,
|
|
ad: { id: lead.ad_id, name: lead.ad_name },
|
|
adset: { id: lead.adset_id, name: lead.adset_name },
|
|
page: form.page,
|
|
created_time: lead.created_time,
|
|
event,
|
|
};
|
|
}),
|
|
);
|
|
|
|
if (events.length === 0) {
|
|
return {};
|
|
}
|
|
|
|
return {
|
|
workflowData: [this.helpers.returnJsonArray(events)],
|
|
};
|
|
}
|
|
}
|