feat: Added apiKey in webhook and serverUrl in fetchInstance if EXPOSE_IN_FETCH_INSTANCES: true

This commit is contained in:
Davidson Gomes 2023-07-18 14:30:40 -03:00
parent 69e1622e82
commit 8c7b0698f3
3 changed files with 81 additions and 66 deletions

View File

@ -8,6 +8,7 @@
* Change Baileys version to: 6.4.0 * Change Baileys version to: 6.4.0
* Send contact in chatwoot * Send contact in chatwoot
* Send contact array in chatwoot * Send contact array in chatwoot
* Added apiKey in webhook and serverUrl in fetchInstance if EXPOSE_IN_FETCH_INSTANCES: true
### Fixed ### Fixed

View File

@ -99,72 +99,54 @@ export class WAMonitoringService {
if (value.connectionStatus.state === 'open') { if (value.connectionStatus.state === 'open') {
this.logger.verbose('instance: ' + key + ' - connectionStatus: open'); this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
let apikey: string;
if (this.configService.get<Auth>('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({ const instanceData = {
instance: { instance: {
instanceName: key, instanceName: key,
owner: value.wuid, owner: value.wuid,
profileName: (await value.getProfileName()) || 'not loaded', profileName: (await value.getProfileName()) || 'not loaded',
profilePictureUrl: value.profilePictureUrl, profilePictureUrl: value.profilePictureUrl,
profileStatus: (await value.getProfileStatus()) || '', profileStatus: (await value.getProfileStatus()) || '',
status: value.connectionStatus.state, status: value.connectionStatus.state,
apikey, },
chatwoot, };
},
}); if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
} else { instanceData.instance['serverUrl'] =
this.logger.verbose( this.configService.get<HttpServer>('SERVER').URL;
'instance: ' + key + ' - hash not exposed in fetch instances',
); instanceData.instance['apikey'] = (
instances.push({ await this.repository.auth.find(key)
instance: { ).apikey;
instanceName: key,
owner: value.wuid, instanceData.instance['chatwoot'] = chatwoot;
profileName: (await value.getProfileName()) || 'not loaded',
profilePictureUrl: value.profilePictureUrl,
profileStatus: (await value.getProfileStatus()) || '',
status: value.connectionStatus.state,
},
});
} }
instances.push(instanceData);
} else { } else {
this.logger.verbose( this.logger.verbose(
'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state, 'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state,
); );
let apikey: string;
if (this.configService.get<Auth>('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({ const instanceData = {
instance: { instance: {
instanceName: key, instanceName: key,
status: value.connectionStatus.state, status: value.connectionStatus.state,
apikey, },
chatwoot, };
},
}); if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
} else { instanceData.instance['serverUrl'] =
this.logger.verbose( this.configService.get<HttpServer>('SERVER').URL;
'instance: ' + key + ' - hash not exposed in fetch instances',
); instanceData.instance['apikey'] = (
instances.push({ await this.repository.auth.find(key)
instance: { ).apikey;
instanceName: key,
status: value.connectionStatus.state, instanceData.instance['chatwoot'] = chatwoot;
},
});
} }
instances.push(instanceData);
} }
} }
} }

View File

@ -348,6 +348,13 @@ export class WAStartupService {
const transformedWe = we.replace(/_/gm, '-').toLowerCase(); const transformedWe = we.replace(/_/gm, '-').toLowerCase();
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE; const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
const expose =
this.configService.get<Auth>('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<Auth>('AUTHENTICATION').API_KEY.KEY;
if (local && instance.MODE !== 'container') { if (local && instance.MODE !== 'container') {
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) { if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
this.logger.verbose('Sending data to webhook local'); this.logger.verbose('Sending data to webhook local');
@ -360,7 +367,7 @@ export class WAStartupService {
} }
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) { if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
this.logger.log({ const logData = {
local: WAStartupService.name + '.sendDataWebhook-local', local: WAStartupService.name + '.sendDataWebhook-local',
url: baseURL, url: baseURL,
event, event,
@ -368,19 +375,32 @@ export class WAStartupService {
data, data,
destination: this.localWebhook.url, destination: this.localWebhook.url,
server_url: serverUrl, server_url: serverUrl,
}); apikey: (expose && instanceApikey) || null,
};
if (expose && instanceApikey) {
logData['apikey'] = instanceApikey;
}
this.logger.log(logData);
} }
try { try {
if (this.localWebhook.enabled && isURL(this.localWebhook.url)) { if (this.localWebhook.enabled && isURL(this.localWebhook.url)) {
const httpService = axios.create({ baseURL }); const httpService = axios.create({ baseURL });
await httpService.post('', { const postData = {
event, event,
instance: this.instance.name, instance: this.instance.name,
data, data,
destination: this.localWebhook.url, destination: this.localWebhook.url,
server_url: serverUrl, server_url: serverUrl,
}); };
if (expose && instanceApikey) {
postData['apikey'] = instanceApikey;
}
await httpService.post('', postData);
} }
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({
@ -421,7 +441,7 @@ export class WAStartupService {
} }
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) { if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
this.logger.log({ const logData = {
local: WAStartupService.name + '.sendDataWebhook-global', local: WAStartupService.name + '.sendDataWebhook-global',
url: globalURL, url: globalURL,
event, event,
@ -429,19 +449,31 @@ export class WAStartupService {
data, data,
destination: localUrl, destination: localUrl,
server_url: serverUrl, server_url: serverUrl,
}); };
if (expose && globalApiKey) {
logData['apikey'] = globalApiKey;
}
this.logger.log(logData);
} }
try { try {
if (globalWebhook && globalWebhook?.ENABLED && isURL(globalURL)) { if (globalWebhook && globalWebhook?.ENABLED && isURL(globalURL)) {
const httpService = axios.create({ baseURL: globalURL }); const httpService = axios.create({ baseURL: globalURL });
await httpService.post('', { const postData = {
event, event,
instance: this.instance.name, instance: this.instance.name,
data, data,
destination: localUrl, destination: localUrl,
server_url: serverUrl, server_url: serverUrl,
}); };
if (expose && globalApiKey) {
postData['apikey'] = globalApiKey;
}
await httpService.post('', postData);
} }
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({