Skip to content

Commit fe9803b

Browse files
committed
feat: Now you can register several typebots with triggers
1 parent 56df0ca commit fe9803b

File tree

19 files changed

+1017
-276
lines changed

19 files changed

+1017
-276
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CLEAN_STORE_CHATS=true
3232
DATABASE_ENABLED=false
3333
DATABASE_PROVIDER=mongodb # postgresql, mysql, mongodb
3434
DATABASE_CONNECTION_URI='mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true'
35-
DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
35+
DATABASE_CONNECTION_CLIENT_NAME=evolution
3636
DATABASE_SAVE_DATA_INSTANCE=true
3737
DATABASE_SAVE_DATA_NEW_MESSAGE=true
3838
DATABASE_SAVE_MESSAGE_UPDATE=true

Docker/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ CLEAN_STORE_CHATS=true
3434
# Permanent data storage
3535
DATABASE_ENABLED=false
3636
DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
37-
DATABASE_CONNECTION_DB_PREFIX_NAME=evdocker
37+
DATABASE_CONNECTION_CLIENT_NAME=evdocker
3838

3939
# Choose the data you want to save in the application's database or store
4040
DATABASE_SAVE_DATA_INSTANCE=false

Docker/evolution-api-all-services/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin &
3737
readPreference=primary &
3838
ssl=false &
3939
directConnection=true
40-
DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
40+
DATABASE_CONNECTION_CLIENT_NAME=evolution
4141

4242
# Choose the data you want to save in the application's database or store
4343
DATABASE_SAVE_DATA_INSTANCE=false

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ENV CLEAN_STORE_CHATS=true
5050

5151
ENV DATABASE_ENABLED=false
5252
ENV DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
53-
ENV DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
53+
ENV DATABASE_CONNECTION_CLIENT_NAME=evolution
5454

5555
ENV DATABASE_SAVE_DATA_INSTANCE=false
5656
ENV DATABASE_SAVE_DATA_NEW_MESSAGE=false

prisma/postgresql-schema.prisma

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ enum TypebotSessionStatus {
3333
paused
3434
}
3535

36+
enum TriggerType {
37+
all
38+
keyword
39+
}
40+
41+
enum TriggerOperator {
42+
contains
43+
equals
44+
startsWith
45+
endsWith
46+
}
47+
3648
model Instance {
3749
id String @id @default(cuid())
3850
name String @unique @db.VarChar(255)
@@ -42,6 +54,7 @@ model Instance {
4254
integration String? @db.VarChar(100)
4355
number String? @db.VarChar(100)
4456
token String? @unique @db.VarChar(255)
57+
clientName String? @db.VarChar(100)
4558
createdAt DateTime? @default(now()) @db.Timestamp
4659
updatedAt DateTime? @updatedAt @db.Timestamp
4760
Chat Chat[]
@@ -55,22 +68,23 @@ model Instance {
5568
Rabbitmq Rabbitmq?
5669
Sqs Sqs?
5770
Websocket Websocket?
58-
Typebot Typebot?
71+
Typebot Typebot[]
5972
Session Session?
6073
MessageUpdate MessageUpdate[]
6174
TypebotSession TypebotSession[]
75+
TypebotSetting TypebotSetting?
6276
}
6377

6478
model Session {
65-
id Int @id @unique @default(autoincrement())
79+
id String @id @default(cuid())
6680
sessionId String @unique
6781
creds String? @db.Text
6882
createdAt DateTime @default(now()) @db.Timestamp
6983
Instance Instance @relation(fields: [sessionId], references: [id], onDelete: Cascade)
7084
}
7185

7286
model Chat {
73-
id Int @id @default(autoincrement())
87+
id String @id @default(cuid())
7488
remoteJid String @db.VarChar(100)
7589
labels Json? @db.JsonB
7690
createdAt DateTime? @default(now()) @db.Timestamp
@@ -80,7 +94,7 @@ model Chat {
8094
}
8195

8296
model Contact {
83-
id Int @id @default(autoincrement())
97+
id String @id @default(cuid())
8498
remoteJid String @db.VarChar(100)
8599
pushName String? @db.VarChar(100)
86100
profilePicUrl String? @db.VarChar(500)
@@ -91,7 +105,7 @@ model Contact {
91105
}
92106

93107
model Message {
94-
id Int @id @default(autoincrement())
108+
id String @id @default(cuid())
95109
key Json @db.JsonB
96110
pushName String? @db.VarChar(100)
97111
participant String? @db.VarChar(100)
@@ -107,27 +121,27 @@ model Message {
107121
chatwootIsRead Boolean? @db.Boolean
108122
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
109123
instanceId String
110-
typebotSessionId Int?
124+
typebotSessionId String?
111125
MessageUpdate MessageUpdate[]
112126
TypebotSession TypebotSession? @relation(fields: [typebotSessionId], references: [id])
113127
}
114128

115129
model MessageUpdate {
116-
id Int @id @default(autoincrement())
130+
id String @id @default(cuid())
117131
keyId String @db.VarChar(100)
118132
remoteJid String @db.VarChar(100)
119133
fromMe Boolean @db.Boolean
120134
participant String? @db.VarChar(100)
121135
pollUpdates Json? @db.JsonB
122136
status String @db.VarChar(30)
123137
Message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
124-
messageId Int
138+
messageId String
125139
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
126140
instanceId String
127141
}
128142

129143
model Webhook {
130-
id Int @id @default(autoincrement())
144+
id String @id @default(cuid())
131145
url String @db.VarChar(500)
132146
enabled Boolean? @default(true) @db.Boolean
133147
events Json? @db.JsonB
@@ -140,7 +154,7 @@ model Webhook {
140154
}
141155

142156
model Chatwoot {
143-
id Int @id @default(autoincrement())
157+
id String @id @default(cuid())
144158
enabled Boolean? @default(true) @db.Boolean
145159
accountId String? @db.VarChar(100)
146160
token String? @db.VarChar(100)
@@ -162,7 +176,7 @@ model Chatwoot {
162176
}
163177

164178
model Label {
165-
id Int @id @default(autoincrement())
179+
id String @id @default(cuid())
166180
labelId String? @unique @db.VarChar(100)
167181
name String @db.VarChar(100)
168182
color String @db.VarChar(100)
@@ -174,7 +188,7 @@ model Label {
174188
}
175189

176190
model Proxy {
177-
id Int @id @default(autoincrement())
191+
id String @id @default(cuid())
178192
enabled Boolean @default(false) @db.Boolean
179193
host String @db.VarChar(100)
180194
port String @db.VarChar(100)
@@ -188,7 +202,7 @@ model Proxy {
188202
}
189203

190204
model Setting {
191-
id Int @id @default(autoincrement())
205+
id String @id @default(cuid())
192206
rejectCall Boolean @default(false) @db.Boolean
193207
msgCall String? @db.VarChar(100)
194208
groupsIgnore Boolean @default(false) @db.Boolean
@@ -203,7 +217,7 @@ model Setting {
203217
}
204218

205219
model Rabbitmq {
206-
id Int @id @default(autoincrement())
220+
id String @id @default(cuid())
207221
enabled Boolean @default(false) @db.Boolean
208222
events Json @db.JsonB
209223
createdAt DateTime? @default(now()) @db.Timestamp
@@ -213,7 +227,7 @@ model Rabbitmq {
213227
}
214228

215229
model Sqs {
216-
id Int @id @default(autoincrement())
230+
id String @id @default(cuid())
217231
enabled Boolean @default(false) @db.Boolean
218232
events Json @db.JsonB
219233
createdAt DateTime? @default(now()) @db.Timestamp
@@ -223,7 +237,7 @@ model Sqs {
223237
}
224238

225239
model Websocket {
226-
id Int @id @default(autoincrement())
240+
id String @id @default(cuid())
227241
enabled Boolean @default(false) @db.Boolean
228242
events Json @db.JsonB
229243
createdAt DateTime? @default(now()) @db.Timestamp
@@ -233,24 +247,29 @@ model Websocket {
233247
}
234248

235249
model Typebot {
236-
id Int @id @default(autoincrement())
250+
id String @id @default(cuid())
237251
enabled Boolean @default(true) @db.Boolean
238252
url String @db.VarChar(500)
239253
typebot String @db.VarChar(100)
240-
expire Int @default(0) @db.Integer
254+
expire Int? @default(0) @db.Integer
241255
keywordFinish String? @db.VarChar(100)
242256
delayMessage Int? @db.Integer
243257
unknownMessage String? @db.VarChar(100)
244-
listeningFromMe Boolean @default(false) @db.Boolean
258+
listeningFromMe Boolean? @default(false) @db.Boolean
259+
stopBotFromMe Boolean? @default(false) @db.Boolean
260+
keepOpen Boolean? @default(false) @db.Boolean
245261
createdAt DateTime? @default(now()) @db.Timestamp
246262
updatedAt DateTime? @updatedAt @db.Timestamp
263+
triggerType TriggerType?
264+
triggerOperator TriggerOperator?
265+
triggerValue String?
247266
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
248-
instanceId String @unique
267+
instanceId String
249268
sessions TypebotSession[]
250269
}
251270

252271
model TypebotSession {
253-
id Int @id @default(autoincrement())
272+
id String @id @default(cuid())
254273
remoteJid String @db.VarChar(100)
255274
pushName String? @db.VarChar(100)
256275
sessionId String @db.VarChar(100)
@@ -260,8 +279,23 @@ model TypebotSession {
260279
createdAt DateTime? @default(now()) @db.Timestamp
261280
updatedAt DateTime @updatedAt @db.Timestamp
262281
Typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
263-
typebotId Int
282+
typebotId String
264283
Message Message[]
265284
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
266285
instanceId String
267286
}
287+
288+
model TypebotSetting {
289+
id String @id @default(cuid())
290+
expire Int? @default(0) @db.Integer
291+
keywordFinish String? @db.VarChar(100)
292+
delayMessage Int? @db.Integer
293+
unknownMessage String? @db.VarChar(100)
294+
listeningFromMe Boolean? @default(false) @db.Boolean
295+
stopBotFromMe Boolean? @default(false) @db.Boolean
296+
keepOpen Boolean? @default(false) @db.Boolean
297+
createdAt DateTime? @default(now()) @db.Timestamp
298+
updatedAt DateTime @updatedAt @db.Timestamp
299+
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
300+
instanceId String @unique
301+
}

src/api/guards/instance.guard.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { NextFunction, Request, Response } from 'express';
2-
import { existsSync } from 'fs';
3-
import { join } from 'path';
42

53
import { CacheConf, configService, Database } from '../../config/env.config';
6-
import { INSTANCE_DIR } from '../../config/path.config';
74
import {
85
BadRequestException,
96
ForbiddenException,
@@ -33,7 +30,7 @@ async function getInstance(instanceName: string) {
3330
return exists || (await prisma.instance.findMany({ where: { name: instanceName } })).length > 0;
3431
}
3532

36-
return exists || existsSync(join(INSTANCE_DIR, instanceName));
33+
return false;
3734
} catch (error) {
3835
throw new InternalServerErrorException(error?.toString());
3936
}
@@ -65,6 +62,7 @@ export async function instanceLoggedGuard(req: Request, _: Response, next: NextF
6562

6663
if (waMonitor.waInstances[instance.instanceName]) {
6764
waMonitor.waInstances[instance.instanceName]?.removeRabbitmqQueues();
65+
waMonitor.waInstances[instance.instanceName]?.removeSqsQueues();
6866
delete waMonitor.waInstances[instance.instanceName];
6967
}
7068
}

src/api/integrations/chatwoot/services/chatwoot.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,14 +2198,14 @@ export class ChatwootService {
21982198
where: {
21992199
instanceId: instance.instanceId,
22002200
id: {
2201-
in: recentContacts.map((contact) => Number(contact.identifier)),
2201+
in: recentContacts.map((contact) => contact.identifier),
22022202
},
22032203
profilePicUrl: {
22042204
not: null,
22052205
},
22062206
},
22072207
})
2208-
).reduce((acc: Map<number, ContactModel>, contact: ContactModel) => acc.set(contact.id, contact), new Map());
2208+
).reduce((acc: Map<string, ContactModel>, contact: ContactModel) => acc.set(contact.id, contact), new Map());
22092209

22102210
recentContacts.forEach(async (contact) => {
22112211
if (contactsWithProfilePicture.has(contact.identifier)) {

src/api/integrations/typebot/controllers/typebot.controller.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ export class TypebotController {
1010
public async createTypebot(instance: InstanceDto, data: TypebotDto) {
1111
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
1212

13-
if (!data.enabled) {
14-
data.url = '';
15-
data.typebot = '';
16-
data.expire = 0;
17-
data.sessions = [];
18-
} else {
19-
const saveData = await this.typebotService.find(instance);
20-
21-
if (saveData?.typebot?.enabled) {
22-
data.sessions = saveData?.sessions;
23-
}
24-
}
25-
2613
return this.typebotService.create(instance, data);
2714
}
2815

@@ -32,15 +19,51 @@ export class TypebotController {
3219
return this.typebotService.find(instance);
3320
}
3421

35-
public async changeStatus(instance: InstanceDto, data: any) {
22+
public async fetchTypebot(instance: InstanceDto, typebotId: string) {
3623
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
3724

38-
return this.typebotService.changeStatus(instance, data);
25+
return this.typebotService.fetch(instance, typebotId);
26+
}
27+
28+
public async updateTypebot(instance: InstanceDto, typebotId: string, data: TypebotDto) {
29+
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
30+
31+
return this.typebotService.update(instance, typebotId, data);
32+
}
33+
34+
public async deleteTypebot(instance: InstanceDto, typebotId: string) {
35+
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
36+
37+
return this.typebotService.delete(instance, typebotId);
3938
}
4039

4140
public async startTypebot(instance: InstanceDto, data: any) {
4241
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
4342

4443
return this.typebotService.startTypebot(instance, data);
4544
}
45+
46+
public async settings(instance: InstanceDto, data: any) {
47+
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
48+
49+
return this.typebotService.setDefaultSettings(instance, data);
50+
}
51+
52+
public async fetchSettings(instance: InstanceDto) {
53+
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
54+
55+
return this.typebotService.fetchDefaultSettings(instance);
56+
}
57+
58+
public async changeStatus(instance: InstanceDto, data: any) {
59+
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
60+
61+
return this.typebotService.changeStatus(instance, data);
62+
}
63+
64+
public async fetchSessions(instance: InstanceDto, typebotId: string) {
65+
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
66+
67+
return this.typebotService.fetchSessions(instance, typebotId);
68+
}
4669
}

0 commit comments

Comments
 (0)