diff --git a/CHANGELOG.md b/CHANGELOG.md index 133dc37c..c2279292 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Change Baileys version to: 6.4.0 * Send contact in chatwoot * Send contact array in chatwoot +* Added apiKey in webhook and serverUrl in fetchInstance if EXPOSE_IN_FETCH_INSTANCES: true ### Fixed diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 462c9f96..8b347c21 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -99,72 +99,54 @@ export class WAMonitoringService { if (value.connectionStatus.state === 'open') { this.logger.verbose('instance: ' + key + ' - connectionStatus: open'); - let apikey: string; - if (this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { - this.logger.verbose( - 'instance: ' + key + ' - hash exposed in fetch instances', - ); - const tokenStore = await this.repository.auth.find(key); - apikey = tokenStore.apikey || 'Apikey not found'; - instances.push({ - instance: { - instanceName: key, - owner: value.wuid, - profileName: (await value.getProfileName()) || 'not loaded', - profilePictureUrl: value.profilePictureUrl, - profileStatus: (await value.getProfileStatus()) || '', - status: value.connectionStatus.state, - apikey, - chatwoot, - }, - }); - } else { - this.logger.verbose( - 'instance: ' + key + ' - hash not exposed in fetch instances', - ); - instances.push({ - instance: { - instanceName: key, - owner: value.wuid, - profileName: (await value.getProfileName()) || 'not loaded', - profilePictureUrl: value.profilePictureUrl, - profileStatus: (await value.getProfileStatus()) || '', - status: value.connectionStatus.state, - }, - }); + const instanceData = { + instance: { + instanceName: key, + owner: value.wuid, + profileName: (await value.getProfileName()) || 'not loaded', + profilePictureUrl: value.profilePictureUrl, + profileStatus: (await value.getProfileStatus()) || '', + status: value.connectionStatus.state, + }, + }; + + if (this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { + instanceData.instance['serverUrl'] = + this.configService.get('SERVER').URL; + + instanceData.instance['apikey'] = ( + await this.repository.auth.find(key) + ).apikey; + + instanceData.instance['chatwoot'] = chatwoot; } + + instances.push(instanceData); } else { this.logger.verbose( 'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state, ); - let apikey: string; - if (this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { - this.logger.verbose( - 'instance: ' + key + ' - hash exposed in fetch instances', - ); - const tokenStore = await this.repository.auth.find(key); - apikey = tokenStore.apikey || 'Apikey not found'; - instances.push({ - instance: { - instanceName: key, - status: value.connectionStatus.state, - apikey, - chatwoot, - }, - }); - } else { - this.logger.verbose( - 'instance: ' + key + ' - hash not exposed in fetch instances', - ); - instances.push({ - instance: { - instanceName: key, - status: value.connectionStatus.state, - }, - }); + const instanceData = { + instance: { + instanceName: key, + status: value.connectionStatus.state, + }, + }; + + if (this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { + instanceData.instance['serverUrl'] = + this.configService.get('SERVER').URL; + + instanceData.instance['apikey'] = ( + await this.repository.auth.find(key) + ).apikey; + + instanceData.instance['chatwoot'] = chatwoot; } + + instances.push(instanceData); } } } diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 0acca202..bc9d8355 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -348,6 +348,13 @@ export class WAStartupService { const transformedWe = we.replace(/_/gm, '-').toLowerCase(); const instance = this.configService.get('AUTHENTICATION').INSTANCE; + const expose = + this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES; + const tokenStore = await this.repository.auth.find(this.instanceName); + const instanceApikey = tokenStore.apikey || 'Apikey not found'; + + const globalApiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY; + if (local && instance.MODE !== 'container') { if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) { this.logger.verbose('Sending data to webhook local'); @@ -360,7 +367,7 @@ export class WAStartupService { } if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { - this.logger.log({ + const logData = { local: WAStartupService.name + '.sendDataWebhook-local', url: baseURL, event, @@ -368,19 +375,32 @@ export class WAStartupService { data, destination: this.localWebhook.url, server_url: serverUrl, - }); + apikey: (expose && instanceApikey) || null, + }; + + if (expose && instanceApikey) { + logData['apikey'] = instanceApikey; + } + + this.logger.log(logData); } try { if (this.localWebhook.enabled && isURL(this.localWebhook.url)) { const httpService = axios.create({ baseURL }); - await httpService.post('', { + const postData = { event, instance: this.instance.name, data, destination: this.localWebhook.url, server_url: serverUrl, - }); + }; + + if (expose && instanceApikey) { + postData['apikey'] = instanceApikey; + } + + await httpService.post('', postData); } } catch (error) { this.logger.error({ @@ -421,7 +441,7 @@ export class WAStartupService { } if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { - this.logger.log({ + const logData = { local: WAStartupService.name + '.sendDataWebhook-global', url: globalURL, event, @@ -429,19 +449,31 @@ export class WAStartupService { data, destination: localUrl, server_url: serverUrl, - }); + }; + + if (expose && globalApiKey) { + logData['apikey'] = globalApiKey; + } + + this.logger.log(logData); } try { if (globalWebhook && globalWebhook?.ENABLED && isURL(globalURL)) { const httpService = axios.create({ baseURL: globalURL }); - await httpService.post('', { + const postData = { event, instance: this.instance.name, data, destination: localUrl, server_url: serverUrl, - }); + }; + + if (expose && globalApiKey) { + postData['apikey'] = globalApiKey; + } + + await httpService.post('', postData); } } catch (error) { this.logger.error({