diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 9a8fae2d..8527ee32 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -196,7 +196,9 @@ export const statusMessageSchema: JSONSchema7 = { statusMessage: { type: 'object', properties: { - text: { type: 'string' }, + type: { type: 'string', enum: ['text', 'image', 'audio', 'video'] }, + content: { type: 'string' }, + caption: { type: 'string' }, backgroundColor: { type: 'string' }, font: { type: 'integer', minimum: 0, maximum: 5 }, statusJidList: { @@ -210,8 +212,8 @@ export const statusMessageSchema: JSONSchema7 = { }, }, }, - required: ['text', 'backgroundColor', 'font', 'statusJidList'], - ...isNotEmpty('text', 'backgroundColor', 'font', 'statusJidList'), + required: ['type', 'content', 'statusJidList'], + ...isNotEmpty('type', 'content', 'statusJidList'), }, }, required: ['statusMessage'], diff --git a/src/whatsapp/dto/sendMessage.dto.ts b/src/whatsapp/dto/sendMessage.dto.ts index 1b9708fa..61d4379c 100644 --- a/src/whatsapp/dto/sendMessage.dto.ts +++ b/src/whatsapp/dto/sendMessage.dto.ts @@ -32,11 +32,13 @@ class linkPreviewMessage { text: string; } -class StatusMessage { - text: string; - backgroundColor: string; - font: number; +export class StatusMessage { + type: string; + content: string; statusJidList: string[]; + caption?: string; + backgroundColor?: string; + font?: number; } class PollMessage { diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 4ad8ce39..9f551569 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -78,6 +78,7 @@ import { SendLinkPreviewDto, SendStickerDto, SendStatusDto, + StatusMessage, } from '../dto/sendMessage.dto'; import { arrayUnique, isBase64, isURL } from 'class-validator'; import { @@ -1236,7 +1237,7 @@ export class WAStartupService { !message['poll'] && !message['linkPreview'] && !message['sticker'] && - !message['status'] + !sender.includes('@broadcast') ) { if (!message['audio']) { this.logger.verbose('Sending message'); @@ -1265,17 +1266,16 @@ export class WAStartupService { ); } - if (message['status']) { + if (sender.includes('@broadcast')) { this.logger.verbose('Sending message'); + console.log(message['status']); return await this.client.sendMessage( sender, + message['status'].content as unknown as AnyMessageContent, { - text: message['status'].text, - } as unknown as AnyMessageContent, - { - backgroundColor: message['status'].backgroundColor, - font: message['status'].font, - statusJidList: message['status'].statusJidList, + backgroundColor: message['status'].option.backgroundColor, + font: message['status'].option.font, + statusJidList: message['status'].option.statusJidList, } as unknown as MiscMessageGenerationOptions, ); } @@ -1357,10 +1357,89 @@ export class WAStartupService { ); } + private async formatStatusMessage(status: StatusMessage) { + if (!status.type) { + throw new BadRequestException('Type is required'); + } + + if (!status.content) { + throw new BadRequestException('Content is required'); + } + + if ( + !status.statusJidList || + !Array.isArray(status.statusJidList) || + !status.statusJidList.length + ) { + throw new BadRequestException('Status jid list is required'); + } + + if (status.type === 'text') { + if (!status.backgroundColor) { + throw new BadRequestException('Background color is required'); + } + + if (!status.font) { + throw new BadRequestException('Font is required'); + } + + return { + content: { + text: status.content, + }, + option: { + backgroundColor: status.backgroundColor, + font: status.font, + statusJidList: status.statusJidList, + }, + }; + } + if (status.type === 'image') { + return { + content: { + image: { + url: status.content, + }, + caption: status.caption, + }, + option: { + statusJidList: status.statusJidList, + }, + }; + } + if (status.type === 'video') { + return { + content: { + video: { + url: status.content, + }, + caption: status.caption, + }, + option: { + statusJidList: status.statusJidList, + }, + }; + } + if (status.type === 'audio') { + return { + content: { + audio: { + url: status.content, + }, + }, + option: { + statusJidList: status.statusJidList, + }, + }; + } + + throw new BadRequestException('Type not found'); + } + public async statusMessage(data: SendStatusDto) { this.logger.verbose('Sending status message'); return await this.sendMessageWithTyping('status@broadcast', { - status: data.statusMessage, + status: await this.formatStatusMessage(data.statusMessage), }); }