Skip to content

Commit

Permalink
feat: update message sending logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DavidsonGomes committed Jun 15, 2024
1 parent 2d49c73 commit 8510666
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 63 deletions.
5 changes: 0 additions & 5 deletions src/api/controllers/sendMessage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isBase64, isURL } from 'class-validator';
import { BadRequestException } from '../../exceptions';
import { InstanceDto } from '../dto/instance.dto';
import {
FakeCallDto,
SendAudioDto,
SendButtonDto,
SendContactDto,
Expand Down Expand Up @@ -85,8 +84,4 @@ export class SendMessageController {
public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto) {
return await this.waMonitor.waInstances[instanceName].statusMessage(data);
}

public async fakeCall({ instanceName }: InstanceDto, data: FakeCallDto) {
return await this.waMonitor.waInstances[instanceName].fakeCall(data);
}
}
2 changes: 1 addition & 1 deletion src/api/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Key {
remoteJid: string;
}
export class ReadMessageDto {
read_messages: Key[];
readMessages: Key[];
}

export class LastMessage {
Expand Down
5 changes: 0 additions & 5 deletions src/api/dto/sendMessage.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ export class SendListDto extends Metadata {
sections: Section[];
}

export class FakeCallDto extends Metadata {
number: string;
delay: number;
}

export class ContactMessage {
fullName: string;
wuid: string;
Expand Down
2 changes: 1 addition & 1 deletion src/api/integrations/chatwoot/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ export class ChatwootService {
};

waInstance?.markMessageAsRead({
read_messages: [
readMessages: [
{
id: key.id,
fromMe: key.fromMe,
Expand Down
12 changes: 0 additions & 12 deletions src/api/routes/sendMessage.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { RequestHandler, Router } from 'express';
import {
audioMessageSchema,
contactMessageSchema,
fakeCallSchema,
listMessageSchema,
locationMessageSchema,
mediaMessageSchema,
Expand All @@ -16,7 +15,6 @@ import {
} from '../../validate/validate.schema';
import { RouterBroker } from '../abstract/abstract.router';
import {
FakeCallDto,
SendAudioDto,
SendContactDto,
SendListDto,
Expand Down Expand Up @@ -145,16 +143,6 @@ export class MessageRouter extends RouterBroker {
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);
});
// .post(this.routerPath('sendButtons'), ...guards, async (req, res) => {
Expand Down
27 changes: 3 additions & 24 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ import { InstanceDto, SetPresenceDto } from '../../dto/instance.dto';
import { HandleLabelDto, LabelDto } from '../../dto/label.dto';
import {
ContactMessage,
FakeCallDto,
MediaMessage,
Options,
SendAudioDto,
Expand Down Expand Up @@ -1985,6 +1984,8 @@ export class BaileysStartupService extends ChannelStartupService {

await this.client.sendPresenceUpdate('paused', sender);
}

return { presence: data.presence };
} catch (error) {
this.logger.error(error);
throw new BadRequestException(error.toString());
Expand Down Expand Up @@ -2696,7 +2697,7 @@ export class BaileysStartupService extends ChannelStartupService {
public async markMessageAsRead(data: ReadMessageDto) {
try {
const keys: proto.IMessageKey[] = [];
data.read_messages.forEach((read) => {
data.readMessages.forEach((read) => {
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
keys.push({
remoteJid: read.remoteJid,
Expand Down Expand Up @@ -3434,26 +3435,4 @@ export class BaileysStartupService extends ChannelStartupService {
public async templateMessage() {
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());
}
}
}
4 changes: 2 additions & 2 deletions src/validate/chat.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const readMessageSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
read_messages: {
readMessages: {
type: 'array',
minItems: 1,
uniqueItems: true,
Expand All @@ -60,7 +60,7 @@ export const readMessageSchema: JSONSchema7 = {
},
},
},
required: ['read_messages'],
required: ['readMessages'],
};

export const archiveChatSchema: JSONSchema7 = {
Expand Down
13 changes: 0 additions & 13 deletions src/validate/message.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,3 @@ export const listMessageSchema: JSONSchema7 = {
},
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'],
};

0 comments on commit 8510666

Please sign in to comment.