mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
✨ Add Facebook Trigger Node (#1106)
* ⚡ Facebook trigger from webhooks Uses tokens just like the GraphQL API but this one requires an app token instead of an user access token. * ⚡ Improvements :zap Improvements * 🐛 Fix credential file name Co-authored-by: Omar Ajoue <krynble@gmail.com>
This commit is contained in:
53
packages/nodes-base/nodes/Facebook/GenericFunctions.ts
Normal file
53
packages/nodes-base/nodes/Facebook/GenericFunctions.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IExecuteSingleFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
IWebhookFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export async function facebookApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
let credentials;
|
||||
|
||||
if (this.getNode().name.includes('Trigger')) {
|
||||
credentials = this.getCredentials('facebookGraphSubscriptionApi') as IDataObject;
|
||||
} else {
|
||||
credentials = this.getCredentials('facebookGraphApi') as IDataObject;
|
||||
}
|
||||
|
||||
qs.access_token = credentials!.accessToken;
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
accept: 'application/json,text/*;q=0.99',
|
||||
},
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
gzip: true,
|
||||
uri: uri ||`https://graph.facebook.com/v8.0${resource}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
try {
|
||||
return await this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
|
||||
if (error.response.body && error.response.body.error) {
|
||||
const message = error.response.body.error.message;
|
||||
throw new Error(
|
||||
`Facebook Trigger error response [${error.statusCode}]: ${message}`,
|
||||
);
|
||||
}
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user