Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add WhatsApp Business Trigger Node #8840

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions packages/nodes-base/credentials/WhatsAppOAuth2Api.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { ICredentialTestRequest, ICredentialType, INodeProperties } from 'n8n-workflow';

export class WhatsAppOAuth2Api implements ICredentialType {
name = 'whatsAppOAuth2Api';

extends = ['oAuth2Api'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct me if I wrong, but oauth2 mechanism is not used, we are using client/app id and secret and manually requesting access token by appAccessTokenRead, this should not extend oAuth2Api and just have 2 parameters App ID and App Secret

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct, I tried getting OAuth2 to work, but wasn't able to.
So we should be able to simplify it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I changed it to a simplified cred version it stopped working so discarded the changes. Will keep it as is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add more details about issue?


displayName = 'WhatsApp OAuth2 API';

documentationUrl = 'whatsApp';

properties: INodeProperties[] = [
{
displayName: 'Grant Type',
name: 'grantType',
type: 'hidden',
default: 'clientCredentials',
},
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden',
default: 'https://www.facebook.com/v19.0/dialog/oauth',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden',
default: 'https://graph.facebook.com/v19.0/oauth/access_token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'hidden',
default: 'whatsapp_business_management whatsapp_business_messaging',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden',
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden',
default: 'header',
},
];

test: ICredentialTestRequest = {
request: {
baseURL: 'https://graph.facebook.com/v19.0',
url: '/',
ignoreHttpStatusErrors: true,
},
rules: [
{
type: 'responseSuccessBody',
properties: {
key: 'error.type',
value: 'OAuthException',
message: 'Invalid access token',
},
},
],
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why http error is concealed here? reason we have this test to validate user credentials information

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy of one of the other credentials, didn't change it. So I do not know. I do not know what this is doing.

}
11 changes: 11 additions & 0 deletions packages/nodes-base/nodes/Facebook/FacebookTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ export class FacebookTrigger implements INodeType {
default: '',
description: 'Facebook APP ID',
},
{
displayName: 'To watch Whatsapp business account events use the Whatsapp trigger node',
name: 'whatsappBusinessAccountNotice',
type: 'notice',
default: '',
displayOptions: {
show: {
object: ['whatsappBusinessAccount'],
},
},
},
{
displayName: 'Object',
name: 'object',
Expand Down
137 changes: 137 additions & 0 deletions packages/nodes-base/nodes/WhatsApp/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
IHttpRequestMethods,
ILoadOptionsFunctions,
IRequestOptions,
IWebhookFunctions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import type {
WhatsAppAppWebhookSubscriptionsResponse,
WhatsAppAppWebhookSubscription,
} from './types';

export async function whatsAppApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
method: IHttpRequestMethods,
resource: string,
body = {},
qs: IDataObject = {},
): Promise<any> {
const options: IRequestOptions = {
headers: {
accept: 'application/json',
},
method,
qs,
body,
gzip: true,
uri: `https://graph.facebook.com/v19.0${resource}`,
json: true,
};

try {
return await this.helpers.requestOAuth2.call(this, 'whatsAppOAuth2Api', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject, {
message: error?.error?.error?.message,
});
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is not used anywhere

export async function appAccessTokenRead(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
): Promise<{ access_token: string }> {
const credentials = await this.getCredentials('whatsAppOAuth2Api');

const options: IRequestOptions = {
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
method: 'POST',
form: {
client_id: credentials.clientId,
client_secret: credentials.clientSecret,
grant_type: 'client_credentials',
},
uri: credentials.accessTokenUrl as string,
json: true,
};
try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

export async function whatsAppAppApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
method: IHttpRequestMethods,
resource: string,
body?: { type: 'json'; payload: IDataObject } | { type: 'form'; payload: IDataObject },
qs: IDataObject = {},
): Promise<any> {
const tokenResponse = await appAccessTokenRead.call(this);
const appAccessToken = tokenResponse.access_token;

const options: IRequestOptions = {
headers: {
accept: 'application/json',
authorization: `Bearer ${appAccessToken}`,
},
method,
qs,
gzip: true,
uri: `https://graph.facebook.com/v19.0${resource}`,
json: true,
};

if (body?.type === 'json') {
options.body = body.payload;
} else if (body?.type === 'form') {
options.form = body.payload;
}

try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

export async function appWebhookSubscriptionList(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
appId: string,
): Promise<WhatsAppAppWebhookSubscription[]> {
const response = (await whatsAppAppApiRequest.call(
this,
'GET',
`/${appId}/subscriptions`,
)) as WhatsAppAppWebhookSubscriptionsResponse;
return response.data;
}

export async function appWebhookSubscriptionCreate(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
appId: string,
subscription: IDataObject,
) {
return await whatsAppAppApiRequest.call(this, 'POST', `/${appId}/subscriptions`, {
type: 'form',
payload: { ...subscription },
});
}

export async function appWebhookSubscriptionDelete(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
appId: string,
object: string,
) {
return await whatsAppAppApiRequest.call(this, 'DELETE', `/${appId}/subscriptions`, {
type: 'form',
payload: { object },
});
}
18 changes: 18 additions & 0 deletions packages/nodes-base/nodes/WhatsApp/WhatsAppTrigger.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"node": "n8n-nodes-base.whatsAppTrigger",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Communication"],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/credentials/whatsapp/"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/trigger-nodes/n8n-nodes-base.whatsapptrigger/"
}
]
}
}
Loading
Loading