Skip to content

Commit

Permalink
feat: add sendGroupMessages in webhook configs
Browse files Browse the repository at this point in the history
  • Loading branch information
clairton committed Oct 14, 2024
1 parent 5b1016c commit 44c0f31
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ The `.env` can be save one configm, but on redis use different webhook by sessio
{
"url": "http://localhost:3000/whatsapp/webhook",
"token": "kslflkhlkwq",
"header": "api_access_token"
"header": "api_access_token",
"sendGroupMessages": false,
"sendGroupMessages": false,
"sendNewMessages": false,
}
],
"ignoreDataStore": false
Expand Down
52 changes: 52 additions & 0 deletions __tests__/services/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getNormalizedMessage,
isSaveMedia,
extractDestinyPhone,
isGroupMessage,
} from '../../src/services/transformer'
const key = { remoteJid: 'XXXX@s.whatsapp.net', id: 'abc' }

Expand Down Expand Up @@ -68,6 +69,57 @@ describe('service transformer', () => {
expect(extractDestinyPhone(payload)).toBe('x')
})

test('return isGroupMessage false with status', async () => {
const payload = {
entry: [
{
changes: [
{
value: {
statuses: [{ recipient_id: 'x' }]
}
}
]
}
]
}
expect(isGroupMessage(payload)).toBe(false)
})

test('return isGroupMessage false with non group', async () => {
const payload = {
entry: [
{
changes: [
{
value: {
contacts: [{ wa_id: 'y' }],
},
},
],
},
],
}
expect(isGroupMessage(payload)).toBe(false)
})

test('return isGroupMessage true', async () => {
const payload = {
entry: [
{
changes: [
{
value: {
contacts: [{ group_id: 'y' }],
},
},
],
},
],
}
expect(isGroupMessage(payload)).toBe(true)
})

test('return empty extractDestinyPhone from api payload', async () => {
expect(extractDestinyPhone({ to: 'y' })).toBe('y')
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unoapi-cloud",
"version": "1.19.2",
"version": "1.20.0",
"description": "Unoapi Cloud",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Webhook = {
header: string
timeoutMs: number
sendNewMessages: boolean
sendGroupMessages: boolean
}

export type Config = {
Expand Down Expand Up @@ -92,6 +93,7 @@ export const defaultConfig: Config = {
header: '',
timeoutMs: 5_000,
sendNewMessages: false,
sendGroupMessages: true,
},
],
getMessageMetadata: getMessageMetadataDefault,
Expand Down
6 changes: 5 additions & 1 deletion src/services/outgoing_cloud_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Outgoing } from './outgoing'
import fetch, { Response, RequestInit } from 'node-fetch'
import { Webhook, getConfig } from './config'
import logger from './logger'
import { completeCloudApiWebHook } from './transformer'
import { completeCloudApiWebHook, isGroupMessage } from './transformer'
import { isInBlacklist } from './blacklist'
import { EnqueueOption } from '../amqp'

Expand Down Expand Up @@ -32,6 +32,10 @@ export class OutgoingCloudApi implements Outgoing {
logger.info(`Session phone %s webhook %s and destiny phone %s are in blacklist`, phone, webhook.id, destinyPhone)
return
}
if (!webhook.sendGroupMessages && isGroupMessage(message)) {
logger.info(`Session phone %s webhook %s configured to not send group message for this webhook`, phone, webhook.id)
return
}
const body = JSON.stringify(message)
const headers = {
'Content-Type': 'application/json; charset=utf-8',
Expand Down
20 changes: 20 additions & 0 deletions src/services/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,26 @@ export const extractDestinyPhone = (payload: object) => {
return number
}

export const isGroupMessage = (payload: object) => {
const data = payload as any
return !!(
(
data.entry
&& data.entry[0]
&& data.entry[0].changes
&& data.entry[0].changes[0]
&& data.entry[0].changes[0].value
) && (
(
data.entry[0].changes[0].value.contacts
&& data.entry[0].changes[0].value.contacts[0]
&& data.entry[0].changes[0].value.contacts[0].group_id
)
)
)
}


// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const jidToPhoneNumber = (value: any, plus = '+', retry = true): string => {
const number = (value || '').split('@')[0].split(':')[0].replace('+', '')
Expand Down

0 comments on commit 44c0f31

Please sign in to comment.