Skip to content

Commit 072171d

Browse files
committed
feat(chatwoot): read last message on WhatsApp when a message is sent from Chatwoot
1 parent 901954d commit 072171d

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

Docker/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ TYPEBOT_KEEP_OPEN=false
122122
#Chatwoot
123123
# If you leave this option as false, when deleting the message for everyone on WhatsApp, it will not be deleted on Chatwoot.
124124
CHATWOOT_MESSAGE_DELETE=false # false | true
125+
# If you leave this option as true, when sending a message in Chatwoot, the client's last message will be marked as read on WhatsApp.
126+
CHATWOOT_MESSAGE_READ=false # false | true
125127
# This db connection is used to import messages from whatsapp to chatwoot database
126128
CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgres://user:password@hostname:port/dbname
127129
CHATWOOT_IMPORT_DATABASE_PLACEHOLDER_MEDIA_MESSAGE=true

src/config/env.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export type QrCode = { LIMIT: number; COLOR: string };
164164
export type Typebot = { API_VERSION: string; KEEP_OPEN: boolean };
165165
export type Chatwoot = {
166166
MESSAGE_DELETE: boolean;
167+
MESSAGE_READ: boolean;
167168
IMPORT: {
168169
DATABASE: {
169170
CONNECTION: {
@@ -379,6 +380,7 @@ export class ConfigService {
379380
},
380381
CHATWOOT: {
381382
MESSAGE_DELETE: process.env.CHATWOOT_MESSAGE_DELETE === 'false',
383+
MESSAGE_READ: process.env.CHATWOOT_MESSAGE_READ === 'false',
382384
IMPORT: {
383385
DATABASE: {
384386
CONNECTION: {

src/dev-env.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ TYPEBOT:
166166
CHATWOOT:
167167
# If you leave this option as false, when deleting the message for everyone on WhatsApp, it will not be deleted on Chatwoot.
168168
MESSAGE_DELETE: true # false | true
169+
# If you leave this option as true, when sending a message in Chatwoot, the client's last message will be marked as read on WhatsApp.
170+
MESSAGE_READ: false # false | true
169171
IMPORT:
170172
# This db connection is used to import messages from whatsapp to chatwoot database
171173
DATABASE:

src/whatsapp/models/message.model.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ChatwootMessage {
1515
inboxId?: number;
1616
conversationId?: number;
1717
contactInbox?: { sourceId: string };
18+
isRead?: boolean;
1819
}
1920

2021
export class MessageRaw {
@@ -36,8 +37,9 @@ export class MessageRaw {
3637
type MessageRawBoolean<T> = {
3738
[P in keyof T]?: 0 | 1;
3839
};
39-
export type MessageRawSelect = Omit<MessageRawBoolean<MessageRaw>, 'key'> & {
40+
export type MessageRawSelect = Omit<Omit<MessageRawBoolean<MessageRaw>, 'key'>, 'chatwoot'> & {
4041
key?: MessageRawBoolean<Key>;
42+
chatwoot?: MessageRawBoolean<ChatwootMessage>;
4143
};
4244

4345
const messageSchema = new Schema<MessageRaw>({
@@ -60,6 +62,7 @@ const messageSchema = new Schema<MessageRaw>({
6062
inboxId: { type: Number },
6163
conversationId: { type: Number },
6264
contactInbox: { type: Object },
65+
isRead: { type: Boolean },
6366
},
6467
});
6568

src/whatsapp/services/chatwoot.service.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,37 @@ export class ChatwootService {
13541354
);
13551355
}
13561356
}
1357+
1358+
const chatwootRead = this.configService.get<Chatwoot>('CHATWOOT').MESSAGE_READ;
1359+
if (chatwootRead) {
1360+
const lastMessage = await this.repository.message.find({
1361+
where: {
1362+
key: {
1363+
fromMe: false,
1364+
},
1365+
owner: instance.instanceName,
1366+
},
1367+
limit: 1,
1368+
});
1369+
if (lastMessage.length > 0 && !lastMessage[0].chatwoot?.isRead) {
1370+
waInstance?.markMessageAsRead({
1371+
read_messages: lastMessage.map((msg) => ({
1372+
id: msg.key?.id,
1373+
fromMe: msg.key?.fromMe,
1374+
remoteJid: msg.key?.remoteJid,
1375+
})),
1376+
});
1377+
const updateMessage = lastMessage.map((msg) => ({
1378+
key: msg.key,
1379+
owner: msg.owner,
1380+
chatwoot: {
1381+
...msg.chatwoot,
1382+
isRead: true,
1383+
},
1384+
}));
1385+
this.repository.message.update(updateMessage, instance.instanceName, true);
1386+
}
1387+
}
13571388
}
13581389

13591390
if (body.message_type === 'template' && body.event === 'message_created') {

0 commit comments

Comments
 (0)