Skip to content

Commit c656bd6

Browse files
committed
Enhancement of Session Initialization in "StartTypebot" Function
Enhanced "StartTypebot" to initialize sessionIDs during the initial interaction, improving user experience on WhatsApp and preventing unexpected bot restarts.
1 parent 4dfe6bd commit c656bd6

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

src/whatsapp/dto/typebot.dto.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ export class Session {
44
status?: string;
55
createdAt?: number;
66
updateAt?: number;
7+
prefilledVariables?: PrefilledVariables;
8+
}
9+
10+
export class PrefilledVariables {
11+
remoteJid?: string;
12+
pushName?: string;
13+
additionalData?: { [key: string]: any };
714
}
815

916
export class TypebotDto {

src/whatsapp/models/typebot.model.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class Session {
88
status?: string;
99
createdAt?: number;
1010
updateAt?: number;
11+
prefilledVariables?: {
12+
remoteJid?: string;
13+
pushName?: string;
14+
additionalData?: { [key: string]: any };
15+
};
1116
}
1217

1318
export class TypebotRaw {
@@ -40,6 +45,11 @@ const typebotSchema = new Schema<TypebotRaw>({
4045
status: { type: String, required: true },
4146
createdAt: { type: Number, required: true },
4247
updateAt: { type: Number, required: true },
48+
prefilledVariables: {
49+
remoteJid: { type: String, required: false },
50+
pushName: { type: String, required: false },
51+
additionalData: { type: Schema.Types.Mixed, required: false }
52+
},
4353
},
4454
],
4555
});

src/whatsapp/services/typebot.service.ts

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ export class TypebotService {
100100
const url = data.url;
101101
const typebot = data.typebot;
102102
const variables = data.variables;
103+
const findTypebot = await this.find(instance);
104+
const sessions = (findTypebot.sessions as Session[]) ?? [];
105+
const expire = findTypebot.expire;
106+
const keyword_finish = findTypebot.keyword_finish;
107+
const delay_message = findTypebot.delay_message;
108+
const unknown_message = findTypebot.unknown_message;
109+
const listening_from_me = findTypebot.listening_from_me;
103110

104111
const prefilledVariables = {
105112
remoteJid: remoteJid,
@@ -109,42 +116,47 @@ export class TypebotService {
109116
prefilledVariables[variable.name] = variable.value;
110117
});
111118

112-
const id = Math.floor(Math.random() * 10000000000).toString();
113-
114-
const reqData = {
115-
sessionId: id,
116-
startParams: {
117-
typebot: data.typebot,
118-
prefilledVariables: prefilledVariables,
119-
},
120-
};
121-
122-
const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);
123-
124-
await this.sendWAMessage(
125-
instance,
126-
remoteJid,
127-
request.data.messages,
128-
request.data.input,
129-
request.data.clientSideActions,
130-
);
131-
132-
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
133-
remoteJid: remoteJid,
119+
const response = await this.createNewSession(instance, {
134120
url: url,
135121
typebot: typebot,
136-
variables: variables,
137-
sessionId: id,
122+
remoteJid: remoteJid,
123+
expire: expire,
124+
keyword_finish: keyword_finish,
125+
delay_message: delay_message,
126+
unknown_message: unknown_message,
127+
listening_from_me: listening_from_me,
128+
sessions: sessions,
129+
prefilledVariables: prefilledVariables,
138130
});
139131

132+
if (response.sessionId) {
133+
await this.sendWAMessage(
134+
instance,
135+
remoteJid,
136+
response.messages,
137+
response.input,
138+
response.clientSideActions,
139+
);
140+
141+
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
142+
remoteJid: remoteJid,
143+
url: url,
144+
typebot: typebot,
145+
prefilledVariables: prefilledVariables,
146+
sessionId: `${response.sessionId}`,
147+
});
148+
} else {
149+
throw new Error("Session ID not found in response");
150+
}
151+
140152
return {
141153
typebot: {
142154
...instance,
143155
typebot: {
144156
url: url,
145157
remoteJid: remoteJid,
146158
typebot: typebot,
147-
variables: variables,
159+
prefilledVariables: prefilledVariables,
148160
},
149161
},
150162
};
@@ -194,8 +206,9 @@ export class TypebotService {
194206
typebot: data.typebot,
195207
prefilledVariables: {
196208
remoteJid: data.remoteJid,
197-
pushName: data.pushName,
209+
pushName: data.pushName || 'Default Name',
198210
instanceName: instance.instanceName,
211+
...data.prefilledVariables,
199212
},
200213
},
201214
};
@@ -209,6 +222,12 @@ export class TypebotService {
209222
status: 'opened',
210223
createdAt: Date.now(),
211224
updateAt: Date.now(),
225+
prefilledVariables: {
226+
...data.prefilledVariables,
227+
remoteJid: data.remoteJid,
228+
pushName: data.pushName || 'Default Name',
229+
instanceName: instance.instanceName,
230+
}
212231
});
213232

214233
const typebotData = {
@@ -390,6 +409,7 @@ export class TypebotService {
390409
const listening_from_me = findTypebot.listening_from_me;
391410

392411
const session = sessions.find((session) => session.remoteJid === remoteJid);
412+
session.prefilledVariables.pushName = msg.pushName;
393413

394414
if (session && expire && expire > 0) {
395415
const now = Date.now();

0 commit comments

Comments
 (0)