@@ -31,22 +31,23 @@ function isEmoji(str: string) {
3131export class SendMessageController {
3232 constructor ( private readonly waMonitor : WAMonitoringService ) { }
3333
34- private getInstance ( instanceName : string ) {
35- const instance = this . waMonitor . waInstances [ instanceName ] ;
34+ private async getInstance ( instanceName : string ) {
35+ // Try to get from memory or load from database
36+ const instance = await this . waMonitor . getOrLoadInstance ( instanceName ) ;
3637 if ( ! instance ) {
37- throw new NotFoundException (
38- `Instance "${ instanceName } " not found or not connected. Please connect the instance first.` ,
39- ) ;
38+ throw new NotFoundException ( `Instance "${ instanceName } " not found. Please create the instance first.` ) ;
4039 }
4140 return instance ;
4241 }
4342
4443 public async sendTemplate ( { instanceName } : InstanceDto , data : SendTemplateDto ) {
45- return await this . getInstance ( instanceName ) . templateMessage ( data ) ;
44+ const instance = await this . getInstance ( instanceName ) ;
45+ return await instance . templateMessage ( data ) ;
4646 }
4747
4848 public async sendText ( { instanceName } : InstanceDto , data : SendTextDto ) {
49- return await this . getInstance ( instanceName ) . textMessage ( data ) ;
49+ const instance = await this . getInstance ( instanceName ) ;
50+ return await instance . textMessage ( data ) ;
5051 }
5152
5253 public async sendMedia ( { instanceName } : InstanceDto , data : SendMediaDto , file ?: any ) {
@@ -55,62 +56,73 @@ export class SendMessageController {
5556 }
5657
5758 if ( file || isURL ( data ?. media ) || isBase64 ( data ?. media ) ) {
58- return await this . getInstance ( instanceName ) . mediaMessage ( data , file ) ;
59+ const instance = await this . getInstance ( instanceName ) ;
60+ return await instance . mediaMessage ( data , file ) ;
5961 }
6062 throw new BadRequestException ( 'Owned media must be a url or base64' ) ;
6163 }
6264
6365 public async sendPtv ( { instanceName } : InstanceDto , data : SendPtvDto , file ?: any ) {
6466 if ( file || isURL ( data ?. video ) || isBase64 ( data ?. video ) ) {
65- return await this . getInstance ( instanceName ) . ptvMessage ( data , file ) ;
67+ const instance = await this . getInstance ( instanceName ) ;
68+ return await instance . ptvMessage ( data , file ) ;
6669 }
6770 throw new BadRequestException ( 'Owned media must be a url or base64' ) ;
6871 }
6972
7073 public async sendSticker ( { instanceName } : InstanceDto , data : SendStickerDto , file ?: any ) {
7174 if ( file || isURL ( data . sticker ) || isBase64 ( data . sticker ) ) {
72- return await this . getInstance ( instanceName ) . mediaSticker ( data , file ) ;
75+ const instance = await this . getInstance ( instanceName ) ;
76+ return await instance . mediaSticker ( data , file ) ;
7377 }
7478 throw new BadRequestException ( 'Owned media must be a url or base64' ) ;
7579 }
7680
7781 public async sendWhatsAppAudio ( { instanceName } : InstanceDto , data : SendAudioDto , file ?: any ) {
7882 if ( file ?. buffer || isURL ( data . audio ) || isBase64 ( data . audio ) ) {
79- return await this . getInstance ( instanceName ) . audioWhatsapp ( data , file ) ;
83+ const instance = await this . getInstance ( instanceName ) ;
84+ return await instance . audioWhatsapp ( data , file ) ;
8085 } else {
8186 console . error ( 'El archivo no tiene buffer o el audio no es una URL o Base64 válida' ) ;
8287 throw new BadRequestException ( 'Owned media must be a url, base64, or valid file with buffer' ) ;
8388 }
8489 }
8590
8691 public async sendButtons ( { instanceName } : InstanceDto , data : SendButtonsDto ) {
87- return await this . getInstance ( instanceName ) . buttonMessage ( data ) ;
92+ const instance = await this . getInstance ( instanceName ) ;
93+ return await instance . buttonMessage ( data ) ;
8894 }
8995
9096 public async sendLocation ( { instanceName } : InstanceDto , data : SendLocationDto ) {
91- return await this . getInstance ( instanceName ) . locationMessage ( data ) ;
97+ const instance = await this . getInstance ( instanceName ) ;
98+ return await instance . locationMessage ( data ) ;
9299 }
93100
94101 public async sendList ( { instanceName } : InstanceDto , data : SendListDto ) {
95- return await this . getInstance ( instanceName ) . listMessage ( data ) ;
102+ const instance = await this . getInstance ( instanceName ) ;
103+ return await instance . listMessage ( data ) ;
96104 }
97105
98106 public async sendContact ( { instanceName } : InstanceDto , data : SendContactDto ) {
99- return await this . getInstance ( instanceName ) . contactMessage ( data ) ;
107+ const instance = await this . getInstance ( instanceName ) ;
108+ return await instance . contactMessage ( data ) ;
100109 }
101110
102111 public async sendReaction ( { instanceName } : InstanceDto , data : SendReactionDto ) {
103112 if ( ! isEmoji ( data . reaction ) ) {
104113 throw new BadRequestException ( 'Reaction must be a single emoji or empty string' ) ;
105114 }
106- return await this . getInstance ( instanceName ) . reactionMessage ( data ) ;
115+ const instance = await this . getInstance ( instanceName ) ;
116+ return await instance . reactionMessage ( data ) ;
107117 }
108118
109119 public async sendPoll ( { instanceName } : InstanceDto , data : SendPollDto ) {
110- return await this . getInstance ( instanceName ) . pollMessage ( data ) ;
120+ const instance = await this . getInstance ( instanceName ) ;
121+ return await instance . pollMessage ( data ) ;
111122 }
112123
113124 public async sendStatus ( { instanceName } : InstanceDto , data : SendStatusDto , file ?: any ) {
114- return await this . getInstance ( instanceName ) . statusMessage ( data , file ) ;
125+ const instance = await this . getInstance ( instanceName ) ;
126+ return await instance . statusMessage ( data , file ) ;
115127 }
116128}
0 commit comments