mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-25 06:37:45 -06:00
feat(chatwoot): read last message on WhatsApp when a message is sent from Chatwoot
This commit is contained in:
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user