Skip to content

Commit 7f7aa2d

Browse files
committed
save media payload on webhook sendNewMessages
1 parent 4d642e6 commit 7f7aa2d

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unoapi-cloud",
3-
"version": "2.0.9",
3+
"version": "2.0.10",
44
"description": "Unoapi Cloud",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const DATA_JID_TTL: number = parseInt(process.env.DATA_JID_TTL || `${60 *
6060
export const SESSION_TTL: number = parseInt(process.env.SESSION_TTL || '-1')
6161
export const UNOAPI_X_COUNT_RETRIES: string = process.env.UNOAPI_X_COUNT_RETRIES || 'x-unoapi-count-retries'
6262
export const UNOAPI_X_MAX_RETRIES: string = process.env.UNOAPI_X_MAX_RETRIES || 'x-unoapi-max-retries'
63-
export const UNOAPI_EXCHANGE_NAME = process.env.UNOAPI_EXCHANGE_BROKER_NAME || 'unoapi'
63+
export const UNOAPI_EXCHANGE_NAME = process.env.UNOAPI_EXCHANGE_NAME || 'unoapi'
6464
export const UNOAPI_EXCHANGE_BROKER_NAME =`${UNOAPI_EXCHANGE_NAME}.broker`
6565
export const UNOAPI_EXCHANGE_BRIDGE_NAME = `${UNOAPI_EXCHANGE_NAME}.brigde`
6666
export const UNOAPI_QUEUE_NAME = process.env.UNOAPI_QUEUE_NAME || 'unoapi'

src/jobs/incoming.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Outgoing } from '../services/outgoing'
33
import { UNOAPI_QUEUE_COMMANDER, UNOAPI_QUEUE_BULK_STATUS, FETCH_TIMEOUT_MS, UNOAPI_SERVER_NAME, UNOAPI_EXCHANGE_BROKER_NAME } from '../defaults'
44
import { PublishOption, amqpPublish } from '../amqp'
55
import { getConfig } from '../services/config'
6-
import { jidToPhoneNumber, getMimetype, toBuffer } from '../services/transformer'
6+
import { jidToPhoneNumber, getMimetype, toBuffer, TYPE_MESSAGES_MEDIA } from '../services/transformer'
77
import logger from '../services/logger'
88
import fetch, { Response } from 'node-fetch'
99
import mime from 'mime-types'
@@ -57,7 +57,7 @@ export class IncomingJob {
5757
dataStore.setKey(idUno, key)
5858
}
5959
let messagePayload = payload[payload.type]
60-
if (['image', 'audio', 'document', 'video'].includes(payload.type)) {
60+
if (TYPE_MESSAGES_MEDIA.includes(payload.type)) {
6161
const { mediaStore } = await config.getStore(phone, config)
6262
const mediaKey = `${phone}/${idUno}`
6363
const link = payload[payload.type].link
@@ -74,6 +74,7 @@ export class IncomingJob {
7474
mime_type: mimetype,
7575
}
7676
delete messagePayload['link']
77+
await dataStore.setMediaPayload(idUno, messagePayload)
7778
}
7879
const webhookMessage = {
7980
object: 'whatsapp_business_account',
@@ -175,7 +176,13 @@ export class IncomingJob {
175176
],
176177
}
177178
}
178-
await amqpPublish(UNOAPI_EXCHANGE_BROKER_NAME, UNOAPI_QUEUE_BULK_STATUS, phone, { payload: outgingPayload, type: 'whatsapp' })
179+
await amqpPublish(
180+
UNOAPI_EXCHANGE_BROKER_NAME,
181+
UNOAPI_QUEUE_BULK_STATUS,
182+
phone,
183+
{ payload: outgingPayload, type: 'whatsapp' },
184+
{ type: 'topic' }
185+
)
179186
await Promise.all(config.webhooks.map((w) => this.outgoing.sendHttp(phone, w, outgingPayload, optionsOutgoing)))
180187
return response
181188
}

src/jobs/outgoing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Webhook } from '../services/config'
22
import { Outgoing } from '../services/outgoing'
33
import { amqpPublish } from '../amqp'
44
import { UNOAPI_DELAY_AFTER_FIRST_MESSAGE_WEBHOOK_MS, UNOAPI_EXCHANGE_BROKER_NAME, UNOAPI_QUEUE_OUTGOING } from '../defaults'
5-
import { extractDestinyPhone } from '../services/transformer'
5+
import { extractDestinyPhone, TYPE_MESSAGES_MEDIA } from '../services/transformer'
66
import logger from '../services/logger'
77
import { getConfig } from '../services/config'
88
import { isUpdateMessage } from '../services/transformer'
@@ -89,7 +89,7 @@ export class OutgoingJob {
8989
} else {
9090
payload.entry[0].changes[0].value.messages = await Promise.all(
9191
payload.entry[0].changes[0].value.messages.map(async message => {
92-
if (['image', 'audio', 'document', 'video'].includes(message.type)) {
92+
if (TYPE_MESSAGES_MEDIA.includes(message.type)) {
9393
const { mediaStore } = store
9494
message = await mediaStore.saveMediaForwarder(message)
9595
}

src/services/client_baileys.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from './socket'
2121
import { Client, getClient, clients, Contact } from './client'
2222
import { Config, configs, defaultConfig, getConfig, getMessageMetadataDefault } from './config'
23-
import { toBaileysMessageContent, phoneNumberToJid, jidToPhoneNumber, getMessageType, TYPE_MESSAGES_TO_READ } from './transformer'
23+
import { toBaileysMessageContent, phoneNumberToJid, jidToPhoneNumber, getMessageType, TYPE_MESSAGES_TO_READ, TYPE_MESSAGES_MEDIA } from './transformer'
2424
import { v1 as uuid } from 'uuid'
2525
import { Response } from './response'
2626
import QRCode from 'qrcode'
@@ -452,13 +452,13 @@ export class ClientBaileys implements Client {
452452
throw new Error(`Unknow message status ${status}`)
453453
}
454454
} else if (type) {
455-
if (['text', 'image', 'audio', 'document', 'video', 'template', 'interactive', 'contacts'].includes(type)) {
455+
if (['text', 'image', 'audio', 'sticker', 'document', 'video', 'template', 'interactive', 'contacts'].includes(type)) {
456456
let content
457457
if ('template' === type) {
458458
const template = new Template(this.getConfig)
459459
content = await template.bind(this.phone, payload.template.name, payload.template.components)
460460
} else {
461-
if (VALIDATE_MEDIA_LINK_BEFORE_SEND && ['image', 'audio', 'document', 'video'].includes(type)) {
461+
if (VALIDATE_MEDIA_LINK_BEFORE_SEND && TYPE_MESSAGES_MEDIA.includes(type)) {
462462
const link = payload[type] && payload[type].link
463463
if (link) {
464464
const response: FetchResponse = await fetch(link, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), method: 'HEAD'})

src/services/transformer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { t } from '../i18n'
99

1010
export const TYPE_MESSAGES_TO_PROCESS_FILE = ['imageMessage', 'videoMessage', 'audioMessage', 'documentMessage', 'stickerMessage', 'ptvMessage']
1111

12+
export const TYPE_MESSAGES_MEDIA = ['image', 'audio', 'document', 'video', 'sticker']
1213

1314
const MESSAGE_STUB_TYPE_ERRORS = [
1415
'Message absent from node'.toLowerCase(),

0 commit comments

Comments
 (0)