Skip to content

Commit 3662a59

Browse files
committed
feat: add support for editing sent messages via new PATCH endpoint - Closes #169
Implemented the ability to edit previously sent messages by introducing a PATCH endpoint at /chat/editMessage/codechat. Messages can now be updated using an id and new text payload. \n\nThis adds support for message editing similar to Baileys, improving message control for users.
1 parent 6f41efa commit 3662a59

File tree

6 files changed

+75
-4
lines changed

6 files changed

+75
-4
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": "whatsapp-api",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/src/main.js",
66
"scripts": {

src/validate/validate.schema.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,16 @@ export const mediaUrlSchema: JSONSchema7 = {
614614
...isNotEmpty('id'),
615615
};
616616

617+
export const editMessageSchema: JSONSchema7 = {
618+
$id: ulid(),
619+
type: 'object',
620+
properties: {
621+
text: { type: 'string' },
622+
},
623+
required: ['text'],
624+
...isNotEmpty('text'),
625+
};
626+
617627
// Group Schema
618628
export const createGroupSchema: JSONSchema7 = {
619629
$id: ulid(),

src/whatsapp/controllers/chat.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
WhatsAppNumberDto,
4444
ReadMessageIdDto,
4545
RejectCallDto,
46+
EditMessage,
4647
} from '../dto/chat.dto';
4748
import { InstanceDto } from '../dto/instance.dto';
4849
import { WAMonitoringService } from '../services/monitor.service';
@@ -115,4 +116,8 @@ export class ChatController {
115116
.get(instanceName)
116117
.assertSessions(data.numbers);
117118
}
119+
120+
public async editMessage({ instanceName }: InstanceDto, data: EditMessage) {
121+
return await this.waMonitor.waInstances.get(instanceName).editMessage(data);
122+
}
118123
}

src/whatsapp/dto/chat.dto.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,19 @@ export class ArchiveChatDto {
8282
archive: boolean;
8383
}
8484

85-
export class DeleteMessage {
85+
export class MessageId {
8686
id: string;
87+
}
88+
89+
export class DeleteMessage extends MessageId {
8790
everyOne?: 'true' | 'false';
8891
}
8992

9093
export class RejectCallDto {
9194
callId: string;
9295
callFrom: string;
9396
}
97+
98+
export class EditMessage extends MessageId {
99+
text: string;
100+
}

src/whatsapp/routers/chat.router.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
archiveChatSchema,
4040
contactValidateSchema,
4141
deleteMessageSchema,
42+
editMessageSchema,
4243
messageValidateSchema,
4344
profilePictureSchema,
4445
readMessageForIdSchema,
@@ -50,6 +51,7 @@ import {
5051
import {
5152
ArchiveChatDto,
5253
DeleteMessage,
54+
EditMessage,
5355
NumberDto,
5456
ReadMessageDto,
5557
ReadMessageIdDto,
@@ -280,6 +282,15 @@ export function ChatRouter(chatController: ChatController, ...guards: RequestHan
280282
});
281283

282284
res.status(HttpStatus.OK).json(response);
285+
})
286+
.post(routerPath('editMessage'), ...guards, async (req, res) => {
287+
const response = await dataValidate<EditMessage>({
288+
request: req,
289+
schema: editMessageSchema,
290+
execute: (instance, data) => chatController.editMessage(instance, data),
291+
});
292+
293+
return response;
283294
});
284295

285296
return router;

src/whatsapp/services/whatsapp.service.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,18 @@ import {
104104
SendReactionDto,
105105
SendTextDto,
106106
} from '../dto/sendMessage.dto';
107-
import { isArray, isBase64, isNotEmpty, isURL } from 'class-validator';
107+
import {
108+
isArray,
109+
isBase64,
110+
isInt,
111+
isNotEmpty,
112+
isNumberString,
113+
isURL,
114+
} from 'class-validator';
108115
import {
109116
ArchiveChatDto,
110117
DeleteMessage,
118+
EditMessage,
111119
OnWhatsAppDto,
112120
ReadMessageDto,
113121
ReadMessageIdDto,
@@ -1281,7 +1289,7 @@ export class WAStartupService {
12811289

12821290
const messageId = options?.messageId || ulid(Date.now());
12831291

1284-
if (message?.['react']) {
1292+
if (message?.['react'] || message?.['edit']) {
12851293
m = await this.client.sendMessage(recipient, message as AnyMessageContent, {
12861294
quoted: q,
12871295
messageId,
@@ -1913,6 +1921,36 @@ export class WAStartupService {
19131921
});
19141922
}
19151923

1924+
public async editMessage(data: EditMessage) {
1925+
try {
1926+
const where: any = {
1927+
instanceId: this.instance.id,
1928+
};
1929+
if (isInt(data.id)) {
1930+
const id = Number.parseInt(data.id);
1931+
where.id = id;
1932+
} else {
1933+
where.keyId = data.id;
1934+
}
1935+
1936+
const message = await this.repository.message.findFirst({ where });
1937+
const messageKey: proto.IMessageKey = {
1938+
id: message.keyId,
1939+
fromMe: message.keyFromMe,
1940+
remoteJid: message.keyRemoteJid,
1941+
participant: message?.keyParticipant,
1942+
};
1943+
1944+
return await this.sendMessageWithTyping<AnyMessageContent>(message.keyRemoteJid, {
1945+
edit: messageKey,
1946+
text: data.text,
1947+
});
1948+
} catch (error) {
1949+
this.logger.error(error);
1950+
throw new BadRequestException(error.toString());
1951+
}
1952+
}
1953+
19161954
// Chat Controller
19171955
public async whatsappNumber(data: WhatsAppNumberDto) {
19181956
const onWhatsapp: OnWhatsAppDto[] = [];

0 commit comments

Comments
 (0)