From a057b129f1feaf95b0b0b5bb9c22f60ef34d61cf Mon Sep 17 00:00:00 2001 From: Cleber Wilson Date: Sun, 20 Oct 2024 19:37:18 -0300 Subject: [PATCH] add route: link message --- package.json | 2 +- ...se-multi-file-auth-state-provider-files.ts | 2 +- src/validate/validate.schema.ts | 23 +++++++++++++ .../controllers/instance.controller.ts | 2 +- .../controllers/sendMessage.controller.ts | 5 +++ src/whatsapp/dto/chat.dto.ts | 2 +- src/whatsapp/dto/sendMessage.dto.ts | 14 +++++++- src/whatsapp/routers/sendMessage.router.ts | 13 +++++++- src/whatsapp/services/instance.service.ts | 8 +++++ src/whatsapp/services/whatsapp.service.ts | 33 ++++++++++++++++++- 10 files changed, 97 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index c1dac396..0c106643 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,8 @@ "@adiwajshing/keyed-db": "^0.2.4", "@hapi/boom": "^10.0.1", "@prisma/client": "^5.19.1", + "@whiskeysockets/baileys": "^6.7.8", "axios": "^1.7.7", - "baileys": "github:/jrCleber/Baileys", "class-validator": "^0.14.0", "cross-env": "^7.0.3", "dayjs": "^1.11.13", diff --git a/src/utils/use-multi-file-auth-state-provider-files.ts b/src/utils/use-multi-file-auth-state-provider-files.ts index 29e9c835..b38c6b2f 100644 --- a/src/utils/use-multi-file-auth-state-provider-files.ts +++ b/src/utils/use-multi-file-auth-state-provider-files.ts @@ -43,7 +43,7 @@ import { initAuthCreds, proto, SignalDataTypeMap, -} from 'baileys'; +} from '@whiskeysockets/baileys'; import { Logger } from '../config/logger.config'; import { ConfigService } from '../config/env.config'; import { ProviderFiles } from '../provider/sessions'; diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index e241c487..f9c32ca4 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -45,6 +45,7 @@ */ import { JSONSchema7, JSONSchema7Definition } from 'json-schema'; +import { title } from 'process'; import { ulid } from 'ulid'; const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { @@ -408,6 +409,28 @@ export const reactionMessageSchema: JSONSchema7 = { required: ['reactionMessage'], }; +export const sendLinkSchema: JSONSchema7 = { + $id: ulid(), + type: 'object', + properties: { + number: { ...numberDefinition }, + options: { ...optionsSchema }, + linkMessage: { + type: 'object', + properties: { + text: { type: 'string' }, + link: { type: 'string' }, + description: { type: 'string' }, + title: { type: 'string' }, + thumbnailUrl: { type: 'string' }, + }, + required: ['link'], + ...isNotEmpty('text', 'link', 'description', 'title', 'thumbnailUrl'), + }, + }, + required: ['linkMessage', 'number'], +}; + // Chat Schema export const whatsappNumberSchema: JSONSchema7 = { $id: ulid(), diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 5b2135ea..b7808399 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -38,7 +38,7 @@ * └──────────────────────────────────────────────────────────────────────────────┘ */ -import { delay } from 'baileys'; +import { delay } from '@whiskeysockets/baileys'; import EventEmitter2 from 'eventemitter2'; import { ConfigService } from '../../config/env.config'; import { diff --git a/src/whatsapp/controllers/sendMessage.controller.ts b/src/whatsapp/controllers/sendMessage.controller.ts index 3d56456a..adb81529 100644 --- a/src/whatsapp/controllers/sendMessage.controller.ts +++ b/src/whatsapp/controllers/sendMessage.controller.ts @@ -43,6 +43,7 @@ import { SendAudioDto, SendButtonsDto, SendContactDto, + SendLinkDto, SendListDto, SendListLegacyDto, SendLocationDto, @@ -138,4 +139,8 @@ export class SendMessageController { public async sendListLegacy({ instanceName }: InstanceDto, data: SendListLegacyDto) { return await this.waMonitor.waInstances.get(instanceName).listLegacy(data); } + + public async sendLinkPreview({ instanceName }: InstanceDto, data: SendLinkDto) { + return await this.waMonitor.waInstances.get(instanceName).linkMessage(data); + } } diff --git a/src/whatsapp/dto/chat.dto.ts b/src/whatsapp/dto/chat.dto.ts index f9725244..05ce0cff 100644 --- a/src/whatsapp/dto/chat.dto.ts +++ b/src/whatsapp/dto/chat.dto.ts @@ -37,7 +37,7 @@ * └──────────────────────────────────────────────────────────────────────────────┘ */ -import { WAPresence } from 'baileys'; +import { WAPresence } from '@whiskeysockets/baileys'; export class OnWhatsAppDto { constructor( diff --git a/src/whatsapp/dto/sendMessage.dto.ts b/src/whatsapp/dto/sendMessage.dto.ts index 7a23f547..6b02530b 100644 --- a/src/whatsapp/dto/sendMessage.dto.ts +++ b/src/whatsapp/dto/sendMessage.dto.ts @@ -38,7 +38,7 @@ * └──────────────────────────────────────────────────────────────────────────────┘ */ -import { proto, WAPresence } from 'baileys'; +import { proto, WAPresence } from '@whiskeysockets/baileys'; import { ulid } from 'ulid'; export class Options { @@ -318,3 +318,15 @@ export class SendListLegacyDto extends Metadata { this.listMessage = new ListLegacy(props.listMessage); } } + +export class LinkMessage { + link: string; + thumbnailUrl?: string; + text?: string; + title?: string; + description?: string; +} + +export class SendLinkDto extends Metadata { + linkMessage: LinkMessage; +} diff --git a/src/whatsapp/routers/sendMessage.router.ts b/src/whatsapp/routers/sendMessage.router.ts index 2bc7677c..3a1a2205 100644 --- a/src/whatsapp/routers/sendMessage.router.ts +++ b/src/whatsapp/routers/sendMessage.router.ts @@ -46,15 +46,16 @@ import { mediaFileMessageSchema, mediaMessageSchema, reactionMessageSchema, + sendLinkSchema, textMessageSchema, } from '../../validate/validate.schema'; import { AudioMessageFileDto, - Button, MediaFileDto, SendAudioDto, SendButtonsDto, SendContactDto, + SendLinkDto, SendListDto, SendListLegacyDto, SendLocationDto, @@ -226,6 +227,16 @@ export function MessageRouter( }, }); + return res.status(HttpStatus.CREATED).json(response); + }) + .post(routerPath('sendLink'), ...guards, async (req, res) => { + const response = await dataValidate({ + request: req, + schema: sendLinkSchema, + execute: (instance, data) => + sendMessageController.sendLinkPreview(instance, data), + }); + return res.status(HttpStatus.CREATED).json(response); }); diff --git a/src/whatsapp/services/instance.service.ts b/src/whatsapp/services/instance.service.ts index d1c18f2b..02baf7f9 100644 --- a/src/whatsapp/services/instance.service.ts +++ b/src/whatsapp/services/instance.service.ts @@ -175,6 +175,14 @@ export class InstanceService { profilePicUrl: true, createdAt: true, updatedAt: true, + Auth: { + select: { + id: true, + token: true, + createdAt: true, + updatedAt: true, + }, + }, Webhook: { select: { id: true, diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 79bb9896..a4ea4250 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -67,7 +67,7 @@ import makeWASocket, { WAMessageUpdate, WASocket, WAVersion, -} from 'baileys'; +} from '@whiskeysockets/baileys'; import { ConfigService, ConfigSessionPhone, @@ -96,6 +96,7 @@ import { SendAudioDto, SendButtonsDto, SendContactDto, + SendLinkDto, SendListDto, SendListLegacyDto, SendLocationDto, @@ -1630,6 +1631,36 @@ export class WAStartupService { }); } + public async linkMessage(data: SendLinkDto) { + return await this.sendMessageWithTyping(data.number, { + extendedTextMessage: { + text: (() => { + if (data.linkMessage?.text) { + let t = data.linkMessage.link; + t += '\n\n'; + t += data.linkMessage.text; + return t; + } + })(), + canonicalUrl: data.linkMessage.link, + matchedText: data.linkMessage?.text, + previewType: 0, + title: data.linkMessage?.title, + description: data.linkMessage?.description, + jpegThumbnail: await (async () => { + if (data.linkMessage?.thumbnailUrl) { + try { + const response = await axios.get(data.linkMessage.thumbnailUrl, { + responseType: 'arraybuffer', + }); + return new Uint8Array(response.data); + } catch {} + } + })(), + }, + }); + } + // Chat Controller public async whatsappNumber(data: WhatsAppNumberDto) { const onWhatsapp: OnWhatsAppDto[] = [];