mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-23 04:22:02 -06:00
feat(chatwoot): read last message on WhatsApp when a message is sent from Chatwoot
This commit is contained in:
parent
901954de33
commit
072171da18
@ -122,6 +122,8 @@ TYPEBOT_KEEP_OPEN=false
|
||||
#Chatwoot
|
||||
# If you leave this option as false, when deleting the message for everyone on WhatsApp, it will not be deleted on Chatwoot.
|
||||
CHATWOOT_MESSAGE_DELETE=false # false | true
|
||||
# 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.
|
||||
CHATWOOT_MESSAGE_READ=false # false | true
|
||||
# This db connection is used to import messages from whatsapp to chatwoot database
|
||||
CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgres://user:password@hostname:port/dbname
|
||||
CHATWOOT_IMPORT_DATABASE_PLACEHOLDER_MEDIA_MESSAGE=true
|
||||
|
@ -164,6 +164,7 @@ export type QrCode = { LIMIT: number; COLOR: string };
|
||||
export type Typebot = { API_VERSION: string; KEEP_OPEN: boolean };
|
||||
export type Chatwoot = {
|
||||
MESSAGE_DELETE: boolean;
|
||||
MESSAGE_READ: boolean;
|
||||
IMPORT: {
|
||||
DATABASE: {
|
||||
CONNECTION: {
|
||||
@ -379,6 +380,7 @@ export class ConfigService {
|
||||
},
|
||||
CHATWOOT: {
|
||||
MESSAGE_DELETE: process.env.CHATWOOT_MESSAGE_DELETE === 'false',
|
||||
MESSAGE_READ: process.env.CHATWOOT_MESSAGE_READ === 'false',
|
||||
IMPORT: {
|
||||
DATABASE: {
|
||||
CONNECTION: {
|
||||
|
@ -166,6 +166,8 @@ TYPEBOT:
|
||||
CHATWOOT:
|
||||
# If you leave this option as false, when deleting the message for everyone on WhatsApp, it will not be deleted on Chatwoot.
|
||||
MESSAGE_DELETE: true # false | true
|
||||
# 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.
|
||||
MESSAGE_READ: false # false | true
|
||||
IMPORT:
|
||||
# This db connection is used to import messages from whatsapp to chatwoot database
|
||||
DATABASE:
|
||||
|
@ -15,6 +15,7 @@ class ChatwootMessage {
|
||||
inboxId?: number;
|
||||
conversationId?: number;
|
||||
contactInbox?: { sourceId: string };
|
||||
isRead?: boolean;
|
||||
}
|
||||
|
||||
export class MessageRaw {
|
||||
@ -36,8 +37,9 @@ export class MessageRaw {
|
||||
type MessageRawBoolean<T> = {
|
||||
[P in keyof T]?: 0 | 1;
|
||||
};
|
||||
export type MessageRawSelect = Omit<MessageRawBoolean<MessageRaw>, 'key'> & {
|
||||
export type MessageRawSelect = Omit<Omit<MessageRawBoolean<MessageRaw>, 'key'>, 'chatwoot'> & {
|
||||
key?: MessageRawBoolean<Key>;
|
||||
chatwoot?: MessageRawBoolean<ChatwootMessage>;
|
||||
};
|
||||
|
||||
const messageSchema = new Schema<MessageRaw>({
|
||||
@ -60,6 +62,7 @@ const messageSchema = new Schema<MessageRaw>({
|
||||
inboxId: { type: Number },
|
||||
conversationId: { type: Number },
|
||||
contactInbox: { type: Object },
|
||||
isRead: { type: Boolean },
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -1354,6 +1354,37 @@ export class ChatwootService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const chatwootRead = this.configService.get<Chatwoot>('CHATWOOT').MESSAGE_READ;
|
||||
if (chatwootRead) {
|
||||
const lastMessage = await this.repository.message.find({
|
||||
where: {
|
||||
key: {
|
||||
fromMe: false,
|
||||
},
|
||||
owner: instance.instanceName,
|
||||
},
|
||||
limit: 1,
|
||||
});
|
||||
if (lastMessage.length > 0 && !lastMessage[0].chatwoot?.isRead) {
|
||||
waInstance?.markMessageAsRead({
|
||||
read_messages: lastMessage.map((msg) => ({
|
||||
id: msg.key?.id,
|
||||
fromMe: msg.key?.fromMe,
|
||||
remoteJid: msg.key?.remoteJid,
|
||||
})),
|
||||
});
|
||||
const updateMessage = lastMessage.map((msg) => ({
|
||||
key: msg.key,
|
||||
owner: msg.owner,
|
||||
chatwoot: {
|
||||
...msg.chatwoot,
|
||||
isRead: true,
|
||||
},
|
||||
}));
|
||||
this.repository.message.update(updateMessage, instance.instanceName, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (body.message_type === 'template' && body.event === 'message_created') {
|
||||
|
Loading…
Reference in New Issue
Block a user