feat: update message sending logic

Refactored sendMessage.controller.ts, chat.dto.ts, and sendMessage.dto.ts to improve message handling. Updated chatwoot.service.ts and sendMessage.router.ts for better integration with Chatwoot. Enhanced whatsapp.baileys.service.ts for more reliable WhatsApp communication. Adjusted chat.schema.ts and message.schema.ts for validation improvements. These changes enhance the overall messaging functionality and reliability.
This commit is contained in:
Davidson Gomes 2024-06-15 12:46:52 -03:00
parent 2d49c73023
commit 85106667a2
8 changed files with 7 additions and 63 deletions

View File

@ -3,7 +3,6 @@ import { isBase64, isURL } from 'class-validator';
import { BadRequestException } from '../../exceptions'; import { BadRequestException } from '../../exceptions';
import { InstanceDto } from '../dto/instance.dto'; import { InstanceDto } from '../dto/instance.dto';
import { import {
FakeCallDto,
SendAudioDto, SendAudioDto,
SendButtonDto, SendButtonDto,
SendContactDto, SendContactDto,
@ -85,8 +84,4 @@ export class SendMessageController {
public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto) { public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto) {
return await this.waMonitor.waInstances[instanceName].statusMessage(data); return await this.waMonitor.waInstances[instanceName].statusMessage(data);
} }
public async fakeCall({ instanceName }: InstanceDto, data: FakeCallDto) {
return await this.waMonitor.waInstances[instanceName].fakeCall(data);
}
} }

View File

@ -59,7 +59,7 @@ class Key {
remoteJid: string; remoteJid: string;
} }
export class ReadMessageDto { export class ReadMessageDto {
read_messages: Key[]; readMessages: Key[];
} }
export class LastMessage { export class LastMessage {

View File

@ -131,11 +131,6 @@ export class SendListDto extends Metadata {
sections: Section[]; sections: Section[];
} }
export class FakeCallDto extends Metadata {
number: string;
delay: number;
}
export class ContactMessage { export class ContactMessage {
fullName: string; fullName: string;
wuid: string; wuid: string;

View File

@ -1346,7 +1346,7 @@ export class ChatwootService {
}; };
waInstance?.markMessageAsRead({ waInstance?.markMessageAsRead({
read_messages: [ readMessages: [
{ {
id: key.id, id: key.id,
fromMe: key.fromMe, fromMe: key.fromMe,

View File

@ -3,7 +3,6 @@ import { RequestHandler, Router } from 'express';
import { import {
audioMessageSchema, audioMessageSchema,
contactMessageSchema, contactMessageSchema,
fakeCallSchema,
listMessageSchema, listMessageSchema,
locationMessageSchema, locationMessageSchema,
mediaMessageSchema, mediaMessageSchema,
@ -16,7 +15,6 @@ import {
} from '../../validate/validate.schema'; } from '../../validate/validate.schema';
import { RouterBroker } from '../abstract/abstract.router'; import { RouterBroker } from '../abstract/abstract.router';
import { import {
FakeCallDto,
SendAudioDto, SendAudioDto,
SendContactDto, SendContactDto,
SendListDto, SendListDto,
@ -145,16 +143,6 @@ export class MessageRouter extends RouterBroker {
execute: (instance, data) => sendMessageController.sendList(instance, data), execute: (instance, data) => sendMessageController.sendList(instance, data),
}); });
return res.status(HttpStatus.CREATED).json(response);
})
.post(this.routerPath('fakeCall'), ...guards, async (req, res) => {
const response = await this.dataValidate<FakeCallDto>({
request: req,
schema: fakeCallSchema,
ClassRef: FakeCallDto,
execute: (instance, data) => sendMessageController.fakeCall(instance, data),
});
return res.status(HttpStatus.CREATED).json(response); return res.status(HttpStatus.CREATED).json(response);
}); });
// .post(this.routerPath('sendButtons'), ...guards, async (req, res) => { // .post(this.routerPath('sendButtons'), ...guards, async (req, res) => {

View File

@ -109,7 +109,6 @@ import { InstanceDto, SetPresenceDto } from '../../dto/instance.dto';
import { HandleLabelDto, LabelDto } from '../../dto/label.dto'; import { HandleLabelDto, LabelDto } from '../../dto/label.dto';
import { import {
ContactMessage, ContactMessage,
FakeCallDto,
MediaMessage, MediaMessage,
Options, Options,
SendAudioDto, SendAudioDto,
@ -1985,6 +1984,8 @@ export class BaileysStartupService extends ChannelStartupService {
await this.client.sendPresenceUpdate('paused', sender); await this.client.sendPresenceUpdate('paused', sender);
} }
return { presence: data.presence };
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
throw new BadRequestException(error.toString()); throw new BadRequestException(error.toString());
@ -2696,7 +2697,7 @@ export class BaileysStartupService extends ChannelStartupService {
public async markMessageAsRead(data: ReadMessageDto) { public async markMessageAsRead(data: ReadMessageDto) {
try { try {
const keys: proto.IMessageKey[] = []; const keys: proto.IMessageKey[] = [];
data.read_messages.forEach((read) => { data.readMessages.forEach((read) => {
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) { if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
keys.push({ keys.push({
remoteJid: read.remoteJid, remoteJid: read.remoteJid,
@ -3434,26 +3435,4 @@ export class BaileysStartupService extends ChannelStartupService {
public async templateMessage() { public async templateMessage() {
throw new Error('Method not available in the Baileys service'); throw new Error('Method not available in the Baileys service');
} }
public async fakeCall(data: FakeCallDto) {
try {
const number = this.createJid(data.number);
if (number.includes('@g.us')) {
throw new BadRequestException('Group calls are not supported');
}
const mdDelay = data.delay ?? 0;
const call = await this.client.offerCall(number);
await delay(mdDelay);
await this.client.rejectCall(call.callId, call.toJid);
return call;
} catch (error) {
throw new BadRequestException('Error making fake call', error.toString());
}
}
} }

View File

@ -45,7 +45,7 @@ export const readMessageSchema: JSONSchema7 = {
$id: v4(), $id: v4(),
type: 'object', type: 'object',
properties: { properties: {
read_messages: { readMessages: {
type: 'array', type: 'array',
minItems: 1, minItems: 1,
uniqueItems: true, uniqueItems: true,
@ -60,7 +60,7 @@ export const readMessageSchema: JSONSchema7 = {
}, },
}, },
}, },
required: ['read_messages'], required: ['readMessages'],
}; };
export const archiveChatSchema: JSONSchema7 = { export const archiveChatSchema: JSONSchema7 = {

View File

@ -358,16 +358,3 @@ export const listMessageSchema: JSONSchema7 = {
}, },
required: ['number', 'title', 'footerText', 'buttonText', 'sections'], required: ['number', 'title', 'footerText', 'buttonText', 'sections'],
}; };
export const fakeCallSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
number: { ...numberDefinition },
delay: {
type: 'integer',
description: 'Enter a value in milliseconds',
},
},
required: ['number', 'delay'],
};