feat: Add appendN8nAttribution option to sendAndWait operation (#13697)

Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
Ria Scholz
2025-03-18 10:43:21 +01:00
committed by GitHub
parent 7e1036187f
commit d6d5a66f5d
16 changed files with 173 additions and 40 deletions

View File

@@ -15,6 +15,7 @@ import type {
WhatsAppAppWebhookSubscription,
} from './types';
import type { SendAndWaitConfig } from '../../utils/sendAndWait/utils';
import { createUtmCampaignLink } from '../../utils/utilities';
export const WHATSAPP_BASE_URL = 'https://graph.facebook.com/v13.0/';
async function appAccessTokenRead(
@@ -109,11 +110,19 @@ export const createMessage = (
sendAndWaitConfig: SendAndWaitConfig,
phoneNumberId: string,
recipientPhoneNumber: string,
instanceId: string,
): IHttpRequestOptions => {
const buttons = sendAndWaitConfig.options.map((option) => {
return `*${option.label}:*\n_${sendAndWaitConfig.url}?approved=${option.value}_\n\n`;
});
let n8nAttribution: string = '';
if (sendAndWaitConfig.appendAttribution) {
const attributionText = 'This message was sent automatically with ';
const link = createUtmCampaignLink('n8n-nodes-base.whatsapp', instanceId);
n8nAttribution = `\n\n${attributionText}${link}`;
}
return {
baseURL: WHATSAPP_BASE_URL,
method: 'POST',
@@ -121,7 +130,7 @@ export const createMessage = (
body: {
messaging_product: 'whatsapp',
text: {
body: `${sendAndWaitConfig.message}\n\n${buttons.join('')}`,
body: `${sendAndWaitConfig.message}\n\n${buttons.join('')}${n8nAttribution}`,
},
type: 'text',
to: recipientPhoneNumber,

View File

@@ -83,11 +83,12 @@ export class WhatsApp implements INodeType {
);
const config = getSendAndWaitConfig(this);
const instanceId = this.getInstanceId();
await this.helpers.httpRequestWithAuthentication.call(
this,
WHATSAPP_CREDENTIALS_TYPE,
createMessage(config, phoneNumberId, recipientPhoneNumber),
createMessage(config, phoneNumberId, recipientPhoneNumber, instanceId),
);
const waitTill = configureWaitTillDate(this);

View File

@@ -43,6 +43,7 @@ describe('createMessage', () => {
mockSendAndWaitConfig,
phoneID,
recipientPhone,
'',
);
expect(request).toEqual({
@@ -77,7 +78,12 @@ describe('createMessage', () => {
],
};
const request: IHttpRequestOptions = createMessage(singleOptionConfig, phoneID, recipientPhone);
const request: IHttpRequestOptions = createMessage(
singleOptionConfig,
phoneID,
recipientPhone,
'',
);
expect(request).toEqual({
baseURL: WHATSAPP_BASE_URL,