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
* Send contact in chatwoot
* Send contact array in chatwoot
* Added apiKey in webhook and serverUrl in fetchInstance if EXPOSE_IN_FETCH_INSTANCES: true
### Fixed

View File

@ -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<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({
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<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] =
this.configService.get<HttpServer>('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<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({
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<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] =
this.configService.get<HttpServer>('SERVER').URL;
instanceData.instance['apikey'] = (
await this.repository.auth.find(key)
).apikey;
instanceData.instance['chatwoot'] = chatwoot;
}
instances.push(instanceData);
}
}
}

View File

@ -348,6 +348,13 @@ export class WAStartupService {
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
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 (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>('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>('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({