Skip to content

Commit 9f16212

Browse files
committed
Merge branch 'release/1.7.0'
2 parents e7c5d33 + 4cd30da commit 9f16212

File tree

9 files changed

+88
-4
lines changed

9 files changed

+88
-4
lines changed

Docker/.env.example

Lines changed: 4 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
@@ -139,3 +141,5 @@ AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
139141
# seconds - 3600s ===1h | zero (0) - never expires
140142
AUTHENTICATION_JWT_EXPIRIN_IN=0
141143
AUTHENTICATION_JWT_SECRET='L=0YWt]b2w[WF>#>:&E`'
144+
145+
LANGUAGE=en # pt-BR, en

src/config/env.config.ts

Lines changed: 3 additions & 1 deletion
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,10 +380,11 @@ 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: {
385-
URI: process.env.CHATWOOT_DATABASE_CONNECTION_URI || '',
387+
URI: process.env.CHATWOOT_IMPORT_DATABASE_CONNECTION_URI || '',
386388
},
387389
},
388390
PLACEHOLDER_MEDIA_MESSAGE: process.env?.CHATWOOT_IMPORT_PLACEHOLDER_MEDIA_MESSAGE === 'true',

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/utils/translations/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
"cw.locationMessage.locationUrl": "URL",
2222
"cw.contactMessage.contact": "Contact",
2323
"cw.contactMessage.name": "Name",
24-
"cw.contactMessage.number": "Number"
24+
"cw.contactMessage.number": "Number",
25+
"cw.message.edited": "Edited Message"
2526
}

src/utils/translations/pt-BR.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
"cw.locationMessage.locationUrl": "URL",
2222
"cw.contactMessage.contact": "Contato",
2323
"cw.contactMessage.name": "Nome",
24-
"cw.contactMessage.number": "Número"
24+
"cw.contactMessage.number": "Número",
25+
"cw.message.edited": "Mensagem editada"
2526
}

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/repository/message.repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export class MessageRepository extends Repository {
118118

119119
return await this.messageModel
120120
.find({ ...query.where })
121+
.select(query.select || {})
121122
.sort({ messageTimestamp: -1 })
122123
.limit(query?.limit ?? 0);
123124
}

src/whatsapp/services/chatwoot.service.ts

Lines changed: 59 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') {
@@ -2047,6 +2078,34 @@ export class ChatwootService {
20472078
}
20482079
}
20492080

2081+
if (event === 'messages.edit') {
2082+
const editedText = `${
2083+
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
2084+
}\n\n_\`${i18next.t('cw.message.edited')}.\`_`;
2085+
const message = await this.getMessageByKeyId(instance, body?.key?.id);
2086+
const messageType = message.key?.fromMe ? 'outgoing' : 'incoming';
2087+
2088+
if (message && message.chatwoot?.conversationId) {
2089+
const send = await this.createMessage(
2090+
instance,
2091+
message.chatwoot.conversationId,
2092+
editedText,
2093+
messageType,
2094+
false,
2095+
[],
2096+
{
2097+
message: { extendedTextMessage: { contextInfo: { stanzaId: message.key.id } } },
2098+
},
2099+
'WAID:' + body.key.id,
2100+
);
2101+
if (!send) {
2102+
this.logger.warn('edited message not sent');
2103+
return;
2104+
}
2105+
}
2106+
return;
2107+
}
2108+
20502109
if (event === 'messages.read') {
20512110
this.logger.verbose('read message from instance: ' + instance.instanceName);
20522111

src/whatsapp/services/whatsapp.baileys.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,17 @@ export class BaileysStartupService extends WAStartupService {
937937
try {
938938
this.logger.verbose('Event received: messages.upsert');
939939
for (const received of messages) {
940+
if (
941+
this.localChatwoot.enabled &&
942+
(received.message?.protocolMessage?.editedMessage || received.message?.editedMessage?.message)
943+
) {
944+
const editedMessage =
945+
received.message?.protocolMessage || received.message?.editedMessage?.message?.protocolMessage;
946+
if (editedMessage) {
947+
this.chatwootService.eventWhatsapp('messages.edit', { instanceName: this.instance.name }, editedMessage);
948+
}
949+
}
950+
940951
if (
941952
(type !== 'notify' && type !== 'append') ||
942953
received.message?.protocolMessage ||

0 commit comments

Comments
 (0)