Skip to content

Commit fb24b7e

Browse files
Merge branch 'develop' into chatwoot-format
2 parents b4ce45b + da79634 commit fb24b7e

File tree

8 files changed

+126
-10
lines changed

8 files changed

+126
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
# 1.6.1 (develop)
22

3-
### Feature
4-
5-
* New env `TYPEBOT_KEEP_OPEN` to keep the session open after the end of the flow
6-
* Add `sign_delimiter` to chatwoot set to change the delimiter of the signature
7-
83
### Fixed
94

105
* Fixed Lid Messages
116
* Fixed sending variables to typebot
127
* Fixed sending variables from typebot
138
* Correction sending s3/minio media to chatwoot and typebot
14-
* Fixed message formatting in chatwoot (bold, italic, underline, monospace)
9+
* Fixed the problem with typebot closing at the end of the flow, now this is optional with the TYPEBOT_KEEP_OPEN variable
10+
* Fixed chatwoot Bold, Italic and Underline formatting using Regex
11+
* Added the sign_delimiter property to the Chatwoot configuration, allowing you to set a different delimiter for the signature. Default when not defined \n
12+
* Include instance Id field in the instance configuration
13+
1514

1615
# 1.6.0 (2023-12-12 17:24)
1716

docker-compose.yaml.example.dockerhub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '3.3'
33
services:
44
api:
55
container_name: evolution_api
6-
image: davidsongomes/evolution-api:latest
6+
image: atendai/evolution-api:latest
77
restart: always
88
ports:
99
- 8080:8080

src/whatsapp/controllers/instance.controller.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { delay } from '@whiskeysockets/baileys';
22
import { isURL } from 'class-validator';
33
import EventEmitter2 from 'eventemitter2';
4+
import { v4 } from 'uuid';
45

56
import { ConfigService, HttpServer } from '../../config/env.config';
67
import { Logger } from '../../config/logger.config';
@@ -87,8 +88,11 @@ export class InstanceController {
8788
const instance = new WAStartupService(this.configService, this.eventEmitter, this.repository, this.cache);
8889
instance.instanceName = instanceName;
8990

91+
const instanceId = v4();
92+
9093
instance.sendDataWebhook(Events.INSTANCE_CREATE, {
9194
instanceName,
95+
instanceId: instanceId,
9296
});
9397

9498
this.logger.verbose('instance: ' + instance.instanceName + ' created');
@@ -100,6 +104,7 @@ export class InstanceController {
100104
const hash = await this.authService.generateHash(
101105
{
102106
instanceName: instance.instanceName,
107+
instanceId: instanceId,
103108
},
104109
token,
105110
);
@@ -367,6 +372,7 @@ export class InstanceController {
367372
const result = {
368373
instance: {
369374
instanceName: instance.instanceName,
375+
instanceId: instanceId,
370376
status: 'created',
371377
},
372378
hash,
@@ -459,6 +465,7 @@ export class InstanceController {
459465
return {
460466
instance: {
461467
instanceName: instance.instanceName,
468+
instanceId: instanceId,
462469
status: 'created',
463470
},
464471
hash,
@@ -584,11 +591,13 @@ export class InstanceController {
584591
};
585592
}
586593

587-
public async fetchInstances({ instanceName }: InstanceDto) {
594+
public async fetchInstances({ instanceName, instanceId }: InstanceDto) {
588595
if (instanceName) {
589596
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
590597
this.logger.verbose('instanceName: ' + instanceName);
591598
return this.waMonitor.instanceInfo(instanceName);
599+
} else if (instanceId) {
600+
return this.waMonitor.instanceInfoById(instanceId);
592601
}
593602

594603
this.logger.verbose('requested fetchInstances (all instances)');
@@ -636,6 +645,7 @@ export class InstanceController {
636645

637646
this.waMonitor.waInstances[instanceName].sendDataWebhook(Events.INSTANCE_DELETE, {
638647
instanceName,
648+
instanceId: (await this.repository.auth.find(instanceName))?.instanceId,
639649
});
640650
delete this.waMonitor.waInstances[instanceName];
641651
this.eventEmitter.emit('remove.instance', instanceName, 'inner');

src/whatsapp/dto/instance.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export class InstanceDto {
22
instanceName: string;
3+
instanceId?: string;
34
qrcode?: boolean;
45
number?: string;
56
token?: string;

src/whatsapp/models/auth.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ export class AuthRaw {
66
_id?: string;
77
jwt?: string;
88
apikey?: string;
9+
instanceId?: string;
910
}
1011

1112
const authSchema = new Schema<AuthRaw>({
1213
_id: { type: String, _id: true },
1314
jwt: { type: String, minlength: 1 },
1415
apikey: { type: String, minlength: 1 },
16+
instanceId: { type: String, minlength: 1 },
1517
});
1618

1719
export const AuthModel = dbserver?.model(AuthRaw.name, authSchema, 'authentication');

src/whatsapp/repository/auth.repository.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class AuthRepository extends Repository {
1919
public async create(data: AuthRaw, instance: string): Promise<IInsert> {
2020
try {
2121
this.logger.verbose('creating auth');
22+
2223
if (this.dbSettings.ENABLED) {
2324
this.logger.verbose('saving auth to db');
2425
const insert = await this.authModel.replaceOne({ _id: instance }, { ...data }, { upsert: true });
@@ -62,4 +63,20 @@ export class AuthRepository extends Repository {
6263
return {};
6364
}
6465
}
66+
67+
public async findInstanceNameById(instanceId: string): Promise<string | null> {
68+
try {
69+
this.logger.verbose('finding auth by instanceId');
70+
if (this.dbSettings.ENABLED) {
71+
this.logger.verbose('finding auth in db');
72+
const response = await this.authModel.findOne({ instanceId });
73+
74+
return response._id;
75+
}
76+
77+
this.logger.verbose('finding auth in store is not supported');
78+
} catch (error) {
79+
return null;
80+
}
81+
}
6582
}

src/whatsapp/services/auth.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ export class AuthService {
4646

4747
this.logger.verbose('JWT token created: ' + token);
4848

49-
const auth = await this.repository.auth.create({ jwt: token }, instance.instanceName);
49+
const auth = await this.repository.auth.create(
50+
{ jwt: token, instanceId: instance.instanceId },
51+
instance.instanceName,
52+
);
5053

5154
this.logger.verbose('JWT token saved in database');
5255

@@ -66,7 +69,7 @@ export class AuthService {
6669

6770
this.logger.verbose(token ? 'APIKEY defined: ' + apikey : 'APIKEY created: ' + apikey);
6871

69-
const auth = await this.repository.auth.create({ apikey }, instance.instanceName);
72+
const auth = await this.repository.auth.create({ apikey, instanceId: instance.instanceId }, instance.instanceName);
7073

7174
this.logger.verbose('APIKEY saved in database');
7275

src/whatsapp/services/monitor.service.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export class WAMonitoringService {
112112
const instanceData = {
113113
instance: {
114114
instanceName: key,
115+
instanceId: (await this.repository.auth.find(key))?.instanceId,
115116
owner: value.wuid,
116117
profileName: (await value.getProfileName()) || 'not loaded',
117118
profilePictureUrl: value.profilePictureUrl,
@@ -135,6 +136,89 @@ export class WAMonitoringService {
135136
const instanceData = {
136137
instance: {
137138
instanceName: key,
139+
instanceId: (await this.repository.auth.find(key))?.instanceId,
140+
status: value.connectionStatus.state,
141+
},
142+
};
143+
144+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
145+
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
146+
147+
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
148+
149+
instanceData.instance['chatwoot'] = chatwoot;
150+
}
151+
152+
instances.push(instanceData);
153+
}
154+
}
155+
}
156+
157+
this.logger.verbose('return instance info: ' + instances.length);
158+
159+
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
160+
}
161+
162+
public async instanceInfoById(instanceId?: string) {
163+
this.logger.verbose('get instance info');
164+
const instanceName = await this.repository.auth.findInstanceNameById(instanceId);
165+
if (!instanceName) {
166+
throw new NotFoundException(`Instance "${instanceId}" not found`);
167+
}
168+
169+
if (instanceName && !this.waInstances[instanceName]) {
170+
throw new NotFoundException(`Instance "${instanceName}" not found`);
171+
}
172+
173+
const instances: any[] = [];
174+
175+
for await (const [key, value] of Object.entries(this.waInstances)) {
176+
if (value) {
177+
this.logger.verbose('get instance info: ' + key);
178+
let chatwoot: any;
179+
180+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
181+
182+
const findChatwoot = await this.waInstances[key].findChatwoot();
183+
184+
if (findChatwoot && findChatwoot.enabled) {
185+
chatwoot = {
186+
...findChatwoot,
187+
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
188+
};
189+
}
190+
191+
if (value.connectionStatus.state === 'open') {
192+
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
193+
194+
const instanceData = {
195+
instance: {
196+
instanceName: key,
197+
instanceId: (await this.repository.auth.find(key))?.instanceId,
198+
owner: value.wuid,
199+
profileName: (await value.getProfileName()) || 'not loaded',
200+
profilePictureUrl: value.profilePictureUrl,
201+
profileStatus: (await value.getProfileStatus()) || '',
202+
status: value.connectionStatus.state,
203+
},
204+
};
205+
206+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
207+
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
208+
209+
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
210+
211+
instanceData.instance['chatwoot'] = chatwoot;
212+
}
213+
214+
instances.push(instanceData);
215+
} else {
216+
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);
217+
218+
const instanceData = {
219+
instance: {
220+
instanceName: key,
221+
instanceId: (await this.repository.auth.find(key))?.instanceId,
138222
status: value.connectionStatus.state,
139223
},
140224
};

0 commit comments

Comments
 (0)