Skip to content

Commit

Permalink
feat: add error handling and reaction for message action execution
Browse files Browse the repository at this point in the history
  • Loading branch information
binsarjr committed Jan 25, 2025
1 parent fabbb50 commit 394833e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 26 additions & 8 deletions libs/whatsapp/src/core/whatsapp-message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ import {
WhatsappMessageActionMetadataKey,
} from '@app/whatsapp/constants';
import type { WhatsappMessageActionOptions } from '@app/whatsapp/decorators/whatsapp-message.decorator';
import type { WhatsappMessageAction } from '@app/whatsapp/interfaces/whatsapp.interface';
import {
Emoji,
type WhatsappMessageAction,
} from '@app/whatsapp/interfaces/whatsapp.interface';
import { patternsAndTextIsMatch } from '@app/whatsapp/supports/flag.support';
import { getMessageCaption } from '@app/whatsapp/supports/message.support';
import {
type DiscoveredClassWithMeta,
getMessageCaption,
react,
} from '@app/whatsapp/supports/message.support';
import {
DiscoveryService,
type DiscoveredClassWithMeta,
} from '@golevelup/nestjs-discovery';
import { Injectable } from '@nestjs/common';
import type {
BaileysEventMap,
WAMessage,
WASocket,
import {
jidNormalizedUser,
type BaileysEventMap,
type WAMessage,
type WASocket,
} from '@whiskeysockets/baileys';

@Injectable()
Expand Down Expand Up @@ -51,7 +58,18 @@ export class WhatsappMessageService {

if (!isEligible) continue;

instance.execute(socket, message);
try {
await instance.execute(socket, message);
} catch (error) {
await react(socket, Emoji.Failed, message);
await socket.sendMessage(
jidNormalizedUser(socket.user.id),
{
text: error.toString(),
},
{ quoted: message },
);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/whatsapp/src/interfaces/whatsapp.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WAMessage, WASocket } from '@whiskeysockets/baileys';
import { react } from '../supports/message.support';

enum Emoji {
export enum Emoji {
Processing = '⏳',
Done = '✅',
Failed = '❌',
Expand Down

0 comments on commit 394833e

Please sign in to comment.