-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Add functionality to mark conversations as read in Chatwoot #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use event in consumer name - so it'd be |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { Processor } from '@nestjs/bullmq'; | ||
| import { JOB_CONCURRENCY } from '@waha/apps/app_sdk/constants'; | ||
| import { QueueName } from '@waha/apps/chatwoot/consumers/QueueName'; | ||
| import { EventData } from '@waha/apps/chatwoot/consumers/types'; | ||
| import { | ||
| ChatWootWAHABaseConsumer, | ||
| IMessageInfo, | ||
| } from '@waha/apps/chatwoot/consumers/waha/base'; | ||
| import { WAHASessionAPI } from '@waha/apps/chatwoot/session/WAHASelf'; | ||
| import { WhatsAppContactInfo } from '@waha/apps/chatwoot/contacts/WhatsAppContactInfo'; | ||
| import { SessionManager } from '@waha/core/abc/manager.abc'; | ||
| import { RMutexService } from '@waha/modules/rmutex/rmutex.service'; | ||
| import { WAHAEvents } from '@waha/structures/enums.dto'; | ||
| import { WAHAWebhookMessageAck } from '@waha/structures/webhooks.dto'; | ||
| import { Job } from 'bullmq'; | ||
| import { PinoLogger } from 'nestjs-pino'; | ||
|
|
||
| @Processor(QueueName.WAHA_MESSAGE_READ, { concurrency: JOB_CONCURRENCY }) | ||
| export class WAHAMessageReadConsumer extends ChatWootWAHABaseConsumer { | ||
| constructor( | ||
| protected readonly manager: SessionManager, | ||
| log: PinoLogger, | ||
| rmutex: RMutexService, | ||
| ) { | ||
| super(manager, log, rmutex, 'WAHAMessageReadConsumer'); | ||
| } | ||
|
|
||
| GetChatId(event: WAHAWebhookMessageAck): string { | ||
| return event.payload.from; | ||
| } | ||
|
|
||
| async Process( | ||
| job: Job<EventData, any, WAHAEvents>, | ||
| info: IMessageInfo, | ||
| ): Promise<any> { | ||
| const container = await this.DIContainer(job, job.data.app); | ||
| const event: WAHAWebhookMessageAck = job.data.event as any; | ||
| const session = new WAHASessionAPI(event.session, container.WAHASelf()); | ||
| const handler = new MessageReadHandler( | ||
| job, | ||
| container.ContactConversationService(), | ||
| container.Logger(), | ||
| info, | ||
| session, | ||
| container.Locale(), | ||
| ); | ||
| return await handler.handle(event); | ||
| } | ||
| } | ||
|
|
||
| class MessageReadHandler { | ||
| constructor( | ||
| private job: Job, | ||
| private contactConversationService: any, | ||
| private logger: any, | ||
| private info: IMessageInfo, | ||
| private session: WAHASessionAPI, | ||
| private l: any, | ||
| ) {} | ||
|
|
||
| async handle(event: WAHAWebhookMessageAck) { | ||
| const payload = event.payload; | ||
|
|
||
| // Only process READ acknowledgments | ||
| if (payload.ack !== 3) { // WAMessageAck.READ = 3 | ||
| this.logger.debug(`Ignoring non-READ ack: ${payload.ack} for message ${payload.id}`); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Create contact info from the chat ID | ||
| const contactInfo = WhatsAppContactInfo(this.session, payload.from, this.l); | ||
|
|
||
| // Get or create conversation for this contact | ||
| const conversation = await this.contactConversationService.ConversationByContact(contactInfo); | ||
|
|
||
| // Get the sourceId by searching for the contact | ||
| const sourceId = await this.contactConversationService.getSourceIdByChatId(payload.from); | ||
| if (!sourceId) { | ||
| this.logger.error(`Contact not found for chatId: ${payload.from}`); | ||
| return; | ||
| } | ||
|
|
||
| // Mark the conversation as read in Chatwoot | ||
| await this.contactConversationService.markConversationAsRead(conversation.conversationId, sourceId); | ||
|
|
||
| this.logger.info( | ||
| `Marked conversation ${conversation.conversationId} as read for chatId: ${payload.from} (message: ${payload.id}, sourceId: ${sourceId})`, | ||
| ); | ||
| } catch (err) { | ||
| this.logger.error( | ||
| `Error marking conversation as read for chatId ${payload.from}: ${err.message}`, | ||
| ); | ||
| throw err; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no such field :(
The only
message_updatedwe need - when agent clicks on "Retry" button in CHatWoot message.Found a bit weird way to identify it, hopefully it'll work π€