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:
Ricardo Espinoza
2020-11-04 06:25:18 -05:00
committed by GitHub
parent 28b0c87136
commit ff9964e10b
6 changed files with 331 additions and 2 deletions

View 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);
}
}