Skip to content

Commit 992944b

Browse files
authored
feat(whatsapp): add referral metadata to tags (botpress#14891)
1 parent 4165ea7 commit 992944b

4 files changed

Lines changed: 53 additions & 3 deletions

File tree

integrations/whatsapp/integration.definition.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const defaultBotPhoneNumberId = {
9393
}
9494

9595
export const INTEGRATION_NAME = 'whatsapp'
96-
export const INTEGRATION_VERSION = '4.6.0'
96+
export const INTEGRATION_VERSION = '4.7.0'
9797
export default new IntegrationDefinition({
9898
name: INTEGRATION_NAME,
9999
version: INTEGRATION_VERSION,
@@ -266,6 +266,14 @@ export default new IntegrationDefinition({
266266
title: 'Reply To',
267267
description: 'The ID of the message that this message is a reply to',
268268
},
269+
referralSourceUrl: {
270+
title: 'Referral Source URL',
271+
description: 'The URL of the ad or content that led to the conversation',
272+
},
273+
referralSourceId: {
274+
title: 'Referral Source ID',
275+
description: 'The ID of the ad or content that led to the conversation',
276+
},
269277
},
270278
},
271279
conversation: {

integrations/whatsapp/src/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const getWabaIdsFromTokenResponseSchema = z
1313
granular_scopes: z.array(
1414
z.object({
1515
scope: z.string(),
16-
target_ids: z.array(z.string()),
16+
target_ids: z.array(z.string()).optional(),
1717
})
1818
),
1919
}),

integrations/whatsapp/src/misc/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ const WhatsAppBaseMessageSchema = z.object({
2121
id: z.string().optional(),
2222
})
2323
.optional(),
24+
// there are other fields in the referral object, but we don't need them
25+
referral: z
26+
.object({
27+
source_url: z.string().optional(),
28+
source_id: z.string().optional(),
29+
})
30+
.optional(),
2431
errors: z
2532
.array(
2633
z.object({

integrations/whatsapp/src/webhook/handlers/messages.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ async function _handleIncomingMessage(
8989
}: ValueOf<IncomingMessages> & { incomingMessageType?: string; replyTo?: string }) => {
9090
logger.forBot().debug(`Received ${incomingMessageType ?? type} message from WhatsApp:`, payload)
9191
return client.getOrCreateMessage({
92-
tags: { id: message.id, replyTo },
92+
tags: {
93+
id: message.id,
94+
replyTo,
95+
..._processReferralTags(message, logger),
96+
},
9397
type,
9498
payload,
9599
userId: user.id,
@@ -245,3 +249,34 @@ function _getMediaExpiry(ctx: bp.Context) {
245249
const expiresAt = new Date(Date.now() + expiryDelayHours * 60 * 60 * 1000)
246250
return expiresAt.toISOString()
247251
}
252+
253+
function _processReferralTags(message: WhatsAppMessage, logger: bp.Logger): Record<string, string> {
254+
const { referral } = message
255+
if (!referral) {
256+
return {}
257+
}
258+
259+
const tags: Record<string, string> = {}
260+
261+
if (referral.source_url) {
262+
const originalUrl = referral.source_url
263+
// Urls can go up to 2048 characters, but we limit to 500 to avoid tags limit error
264+
const processedUrl = originalUrl.slice(0, 500)
265+
266+
if (originalUrl !== processedUrl) {
267+
logger
268+
.forBot()
269+
.warn(
270+
`For whatsapp message "${message.id}", referral source URL was truncated from ${originalUrl.length} to 500 characters. Original: ${originalUrl}, Sliced: ${processedUrl}`
271+
)
272+
}
273+
274+
tags.referralSourceUrl = processedUrl
275+
}
276+
277+
if (referral.source_id) {
278+
tags.referralSourceId = referral.source_id
279+
}
280+
281+
return tags
282+
}

0 commit comments

Comments
 (0)