-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat: add BaileysMessageProcessor for improved message handling #1726
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { Logger } from '@config/logger.config'; | ||
| import { BaileysEventMap, MessageUpsertType, proto } from 'baileys'; | ||
| import { catchError, concatMap, delay, EMPTY, from, retryWhen, Subject, Subscription, take, tap } from 'rxjs'; | ||
|
|
||
| type MessageUpsertPayload = BaileysEventMap['messages.upsert']; | ||
| type MountProps = { | ||
| onMessageReceive: (payload: MessageUpsertPayload, settings: any) => Promise<void>; | ||
| }; | ||
|
|
||
| export class BaileysMessageProcessor { | ||
| private processorLogs = new Logger('BaileysMessageProcessor'); | ||
| private subscription?: Subscription; | ||
|
|
||
| protected messageSubject = new Subject<{ | ||
| messages: proto.IWebMessageInfo[]; | ||
| type: MessageUpsertType; | ||
| requestId?: string; | ||
| settings: any; | ||
| }>(); | ||
|
|
||
| mount({ onMessageReceive }: MountProps) { | ||
| this.subscription = this.messageSubject | ||
| .pipe( | ||
| tap(({ messages }) => { | ||
| this.processorLogs.log(`Processing batch of ${messages.length} messages`); | ||
| }), | ||
| concatMap(({ messages, type, requestId, settings }) => | ||
| from(onMessageReceive({ messages, type, requestId }, settings)).pipe( | ||
| retryWhen((errors) => | ||
| errors.pipe( | ||
| tap((error) => this.processorLogs.warn(`Retrying message batch due to error: ${error.message}`)), | ||
| delay(1000), // 1 segundo de delay | ||
| take(3), // Máximo 3 tentativas | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| catchError((error) => { | ||
| this.processorLogs.error(`Error processing message batch: ${error}`); | ||
| return EMPTY; | ||
| }), | ||
| ) | ||
| .subscribe({ | ||
| error: (error) => { | ||
| this.processorLogs.error(`Message stream error: ${error}`); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| processMessage(payload: MessageUpsertPayload, settings: any) { | ||
| const { messages, type, requestId } = payload; | ||
| this.messageSubject.next({ messages, type, requestId, settings }); | ||
| } | ||
|
|
||
| onDestroy() { | ||
| this.subscription?.unsubscribe(); | ||
| this.messageSubject.complete(); | ||
|
Comment on lines
+56
to
+57
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. suggestion (bug_risk): Unsubscribing and completing the Subject may cause issues if processMessage is called after onDestroy. Calling processMessage after onDestroy will throw an error due to the completed Subject. Please guard processMessage or document its usage constraints. |
||
| } | ||
| } | ||
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.
suggestion (performance): No backpressure or queue size limit is enforced on messageSubject.
This could cause memory issues if many messages are processed rapidly. Consider implementing a queue size limit or backpressure mechanism.
Suggested implementation: