Skip to content

Commit 8c7b069

Browse files
committed
feat: Added apiKey in webhook and serverUrl in fetchInstance if EXPOSE_IN_FETCH_INSTANCES: true
1 parent 69e1622 commit 8c7b069

File tree

3 files changed

+81
-66
lines changed

3 files changed

+81
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Change Baileys version to: 6.4.0
99
* Send contact in chatwoot
1010
* Send contact array in chatwoot
11+
* Added apiKey in webhook and serverUrl in fetchInstance if EXPOSE_IN_FETCH_INSTANCES: true
1112

1213
### Fixed
1314

src/whatsapp/services/monitor.service.ts

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -99,72 +99,54 @@ export class WAMonitoringService {
9999

100100
if (value.connectionStatus.state === 'open') {
101101
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
102-
let apikey: string;
102+
103+
const instanceData = {
104+
instance: {
105+
instanceName: key,
106+
owner: value.wuid,
107+
profileName: (await value.getProfileName()) || 'not loaded',
108+
profilePictureUrl: value.profilePictureUrl,
109+
profileStatus: (await value.getProfileStatus()) || '',
110+
status: value.connectionStatus.state,
111+
},
112+
};
113+
103114
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
104-
this.logger.verbose(
105-
'instance: ' + key + ' - hash exposed in fetch instances',
106-
);
107-
const tokenStore = await this.repository.auth.find(key);
108-
apikey = tokenStore.apikey || 'Apikey not found';
109-
110-
instances.push({
111-
instance: {
112-
instanceName: key,
113-
owner: value.wuid,
114-
profileName: (await value.getProfileName()) || 'not loaded',
115-
profilePictureUrl: value.profilePictureUrl,
116-
profileStatus: (await value.getProfileStatus()) || '',
117-
status: value.connectionStatus.state,
118-
apikey,
119-
chatwoot,
120-
},
121-
});
122-
} else {
123-
this.logger.verbose(
124-
'instance: ' + key + ' - hash not exposed in fetch instances',
125-
);
126-
instances.push({
127-
instance: {
128-
instanceName: key,
129-
owner: value.wuid,
130-
profileName: (await value.getProfileName()) || 'not loaded',
131-
profilePictureUrl: value.profilePictureUrl,
132-
profileStatus: (await value.getProfileStatus()) || '',
133-
status: value.connectionStatus.state,
134-
},
135-
});
115+
instanceData.instance['serverUrl'] =
116+
this.configService.get<HttpServer>('SERVER').URL;
117+
118+
instanceData.instance['apikey'] = (
119+
await this.repository.auth.find(key)
120+
).apikey;
121+
122+
instanceData.instance['chatwoot'] = chatwoot;
136123
}
124+
125+
instances.push(instanceData);
137126
} else {
138127
this.logger.verbose(
139128
'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state,
140129
);
141-
let apikey: string;
130+
131+
const instanceData = {
132+
instance: {
133+
instanceName: key,
134+
status: value.connectionStatus.state,
135+
},
136+
};
137+
142138
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
143-
this.logger.verbose(
144-
'instance: ' + key + ' - hash exposed in fetch instances',
145-
);
146-
const tokenStore = await this.repository.auth.find(key);
147-
apikey = tokenStore.apikey || 'Apikey not found';
148-
149-
instances.push({
150-
instance: {
151-
instanceName: key,
152-
status: value.connectionStatus.state,
153-
apikey,
154-
chatwoot,
155-
},
156-
});
157-
} else {
158-
this.logger.verbose(
159-
'instance: ' + key + ' - hash not exposed in fetch instances',
160-
);
161-
instances.push({
162-
instance: {
163-
instanceName: key,
164-
status: value.connectionStatus.state,
165-
},
166-
});
139+
instanceData.instance['serverUrl'] =
140+
this.configService.get<HttpServer>('SERVER').URL;
141+
142+
instanceData.instance['apikey'] = (
143+
await this.repository.auth.find(key)
144+
).apikey;
145+
146+
instanceData.instance['chatwoot'] = chatwoot;
167147
}
148+
149+
instances.push(instanceData);
168150
}
169151
}
170152
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ export class WAStartupService {
348348
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
349349
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
350350

351+
const expose =
352+
this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
353+
const tokenStore = await this.repository.auth.find(this.instanceName);
354+
const instanceApikey = tokenStore.apikey || 'Apikey not found';
355+
356+
const globalApiKey = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
357+
351358
if (local && instance.MODE !== 'container') {
352359
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
353360
this.logger.verbose('Sending data to webhook local');
@@ -360,27 +367,40 @@ export class WAStartupService {
360367
}
361368

362369
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
363-
this.logger.log({
370+
const logData = {
364371
local: WAStartupService.name + '.sendDataWebhook-local',
365372
url: baseURL,
366373
event,
367374
instance: this.instance.name,
368375
data,
369376
destination: this.localWebhook.url,
370377
server_url: serverUrl,
371-
});
378+
apikey: (expose && instanceApikey) || null,
379+
};
380+
381+
if (expose && instanceApikey) {
382+
logData['apikey'] = instanceApikey;
383+
}
384+
385+
this.logger.log(logData);
372386
}
373387

374388
try {
375389
if (this.localWebhook.enabled && isURL(this.localWebhook.url)) {
376390
const httpService = axios.create({ baseURL });
377-
await httpService.post('', {
391+
const postData = {
378392
event,
379393
instance: this.instance.name,
380394
data,
381395
destination: this.localWebhook.url,
382396
server_url: serverUrl,
383-
});
397+
};
398+
399+
if (expose && instanceApikey) {
400+
postData['apikey'] = instanceApikey;
401+
}
402+
403+
await httpService.post('', postData);
384404
}
385405
} catch (error) {
386406
this.logger.error({
@@ -421,27 +441,39 @@ export class WAStartupService {
421441
}
422442

423443
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
424-
this.logger.log({
444+
const logData = {
425445
local: WAStartupService.name + '.sendDataWebhook-global',
426446
url: globalURL,
427447
event,
428448
instance: this.instance.name,
429449
data,
430450
destination: localUrl,
431451
server_url: serverUrl,
432-
});
452+
};
453+
454+
if (expose && globalApiKey) {
455+
logData['apikey'] = globalApiKey;
456+
}
457+
458+
this.logger.log(logData);
433459
}
434460

435461
try {
436462
if (globalWebhook && globalWebhook?.ENABLED && isURL(globalURL)) {
437463
const httpService = axios.create({ baseURL: globalURL });
438-
await httpService.post('', {
464+
const postData = {
439465
event,
440466
instance: this.instance.name,
441467
data,
442468
destination: localUrl,
443469
server_url: serverUrl,
444-
});
470+
};
471+
472+
if (expose && globalApiKey) {
473+
postData['apikey'] = globalApiKey;
474+
}
475+
476+
await httpService.post('', postData);
445477
}
446478
} catch (error) {
447479
this.logger.error({

0 commit comments

Comments
 (0)