Skip to content

Commit 3f43330

Browse files
authored
Start Typebot com opção de ativar chatbot ou não
Foi criada a variável enabled_typebot (não obrigatória) enabled_typebot igual a true ou vazio: o comportamento do "startTypebot" atua como a nova funcionalidadede sessões persistentes (v1.5.2) onde o flow do Typebot disparado atua como chatbot. enabled_typebot igual a false: o comportamento do "startTypebot" atua como era na Evolution v.1.5.1 onde o flow do Typebot disparado atua como mensagem simples, nao ativa o chatbot e funciona apenas como mensagens simples. Obs1: Se setada como true ou se for omitida essa variável no comando start Typebot, enabled_typebot será assumida como true, ou seja, start typebot aciona o flow com o chatbot ativado, imediatamente após o envio, aguardando interação do contato.. Se, antes do acionamento do start typebot, tiver um outro chatbot ativo, o mesmo será substituído pelo novo, ora enviado. Obs2: Se setada como false, dispara apenas como notificação e não ativa o bot. se tiver outro bot ativo na instancia o mesmo continuará ativo, após disparada a mensagem
1 parent 5c74cbf commit 3f43330

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/whatsapp/services/typebot.service.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class TypebotService {
9999
const remoteJid = data.remoteJid;
100100
const url = data.url;
101101
const typebot = data.typebot;
102+
const enabled_typebot = data.enabled_typebot;
102103
const variables = data.variables;
103104
const findTypebot = await this.find(instance);
104105
const sessions = (findTypebot.sessions as Session[]) ?? [];
@@ -116,8 +117,13 @@ export class TypebotService {
116117
prefilledVariables[variable.name] = variable.value;
117118
});
118119

119-
const response = await this.createNewSession(instance, {
120+
if (enabled_typebot !== false ) {
121+
let enabled_typebot = true;
122+
123+
124+
const response = await this.createNewSession(instance, {
120125
url: url,
126+
enabled_typebot: enabled_typebot,
121127
typebot: typebot,
122128
remoteJid: remoteJid,
123129
expire: expire,
@@ -141,18 +147,53 @@ export class TypebotService {
141147
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
142148
remoteJid: remoteJid,
143149
url: url,
150+
enabled_typebot: enabled_typebot,
144151
typebot: typebot,
145152
prefilledVariables: prefilledVariables,
146153
sessionId: `${response.sessionId}`,
147154
});
148155
} else {
149156
throw new Error("Session ID not found in response");
150157
}
158+
159+
} else {
160+
161+
const id = Math.floor(Math.random() * 10000000000).toString();
162+
163+
const reqData = {
164+
sessionId: id,
165+
startParams: {
166+
typebot: data.typebot,
167+
prefilledVariables: prefilledVariables,
168+
},
169+
};
170+
171+
const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);
172+
173+
await this.sendWAMessage(
174+
instance,
175+
remoteJid,
176+
request.data.messages,
177+
request.data.input,
178+
request.data.clientSideActions,
179+
);
180+
181+
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
182+
remoteJid: remoteJid,
183+
url: url,
184+
typebot: typebot,
185+
variables: variables,
186+
sessionId: id,
187+
});
188+
189+
190+
}
151191

152192
return {
153193
typebot: {
154194
...instance,
155195
typebot: {
196+
enabled_typebot: enabled_typebot,
156197
url: url,
157198
remoteJid: remoteJid,
158199
typebot: typebot,
@@ -504,7 +545,6 @@ export class TypebotService {
504545
sessions.splice(sessions.indexOf(session), 1);
505546

506547
const typebotData = {
507-
enabled: true,
508548
url: url,
509549
typebot: typebot,
510550
expire: expire,

0 commit comments

Comments
 (0)