Refatory send message to typebox

This commit is contained in:
Leandro Gonçalves 2023-12-07 18:45:05 -03:00
parent 8fe75cd210
commit 457fcde572
5 changed files with 288 additions and 212 deletions

View File

@ -106,7 +106,7 @@ QRCODE_COLOR=#198754
AUTHENTICATION_TYPE=apikey
## Define a global apikey to access all instances.
### OBS: This key must be inserted in the request header to create an instance.
AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976
AUTHENTICATION_API_KEY=xxx
AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
## Set the secret key to encrypt and decrypt your token and its expiration time
# seconds - 3600s ===1h | zero (0) - never expires

View File

@ -24,12 +24,12 @@ ENV LOG_LEVEL=ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK,WEBHOOKS
ENV LOG_COLOR=true
ENV LOG_BAILEYS=error
ENV DEL_INSTANCE=false
ENV DEL_INSTANCE=true
ENV STORE_MESSAGES=true
ENV STORE_MESSAGE_UP=true
ENV STORE_CONTACTS=true
ENV STORE_CHATS=true
ENV STORE_CONTACTS=false
ENV STORE_CHATS=false
ENV CLEAN_STORE_CLEANING_INTERVAL=7200
ENV CLEAN_STORE_MESSAGES=true
@ -37,22 +37,22 @@ ENV CLEAN_STORE_MESSAGE_UP=true
ENV CLEAN_STORE_CONTACTS=true
ENV CLEAN_STORE_CHATS=true
ENV DATABASE_ENABLED=false
ENV DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
ENV DATABASE_ENABLED=true
ENV DATABASE_CONNECTION_URI=
ENV DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
ENV DATABASE_SAVE_DATA_INSTANCE=false
ENV DATABASE_SAVE_DATA_NEW_MESSAGE=false
ENV DATABASE_SAVE_MESSAGE_UPDATE=false
ENV DATABASE_SAVE_DATA_CONTACTS=false
ENV DATABASE_SAVE_DATA_CHATS=false
ENV DATABASE_SAVE_DATA_INSTANCE=true
ENV DATABASE_SAVE_DATA_NEW_MESSAGE=true
ENV DATABASE_SAVE_MESSAGE_UPDATE=true
ENV DATABASE_SAVE_DATA_CONTACTS=true
ENV DATABASE_SAVE_DATA_CHATS=true
ENV REDIS_ENABLED=false
ENV REDIS_URI=redis://redis:6379
ENV REDIS_URI=
ENV REDIS_PREFIX_KEY=evolution
ENV RABBITMQ_ENABLED=false
ENV RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672
ENV RABBITMQ_URI=
ENV WEBSOCKET_ENABLED=false
@ -100,7 +100,7 @@ ENV QRCODE_COLOR=#198754
ENV AUTHENTICATION_TYPE=apikey
ENV AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976
ENV AUTHENTICATION_API_KEY=
ENV AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
ENV AUTHENTICATION_JWT_EXPIRIN_IN=0

4
captain-definition Normal file
View File

@ -0,0 +1,4 @@
{
"schemaVersion": 2,
"dockerfilePath": "./Dockerfile"
}

View File

@ -44,6 +44,7 @@ export class TypebotService {
if (session) {
if (status === 'closed') {
this.logger.verbose(`============== Closing Typebot session to Jid ${remoteJid}! =================`);
findData.sessions.splice(findData.sessions.indexOf(session), 1);
const typebotData = {
@ -65,6 +66,7 @@ export class TypebotService {
findData.sessions.map((session) => {
if (session.remoteJid === remoteJid) {
this.logger.verbose(`============== Updating Typebot session to Jid ${remoteJid}! =================`);
session.status = status;
}
});
@ -120,8 +122,7 @@ export class TypebotService {
});
}
if (startSession) {
const response = await this.createNewSession(instance, {
const createSessionData = {
url: url,
typebot: typebot,
remoteJid: remoteJid,
@ -132,7 +133,10 @@ export class TypebotService {
listening_from_me: listening_from_me,
sessions: sessions,
prefilledVariables: prefilledVariables,
});
};
if (startSession) {
const response = await this.createNewSession(instance, createSessionData);
if (response.sessionId) {
await this.sendWAMessage(instance, remoteJid, response.messages, response.input, response.clientSideActions);
@ -157,6 +161,8 @@ export class TypebotService {
},
};
try{
const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);
await this.sendWAMessage(
@ -174,6 +180,14 @@ export class TypebotService {
variables: variables,
sessionId: id,
});
}catch(e) {
this.logger.verbose('============== Erro Sending Typebot message! =================');
this.logger.verbose(e);
await this.recreateTypebot(instance, remoteJid, createSessionData, url, typebot, prefilledVariables);
}
}
return {
@ -239,6 +253,7 @@ export class TypebotService {
},
};
try {
const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);
if (request.data.sessionId) {
@ -272,6 +287,11 @@ export class TypebotService {
}
return request.data;
}catch (e) {
this.logger.verbose('============== Erro Trying create new Typebot session! =================');
this.logger.verbose(e);
}
}
public async sendWAMessage(
@ -434,8 +454,26 @@ export class TypebotService {
const unknown_message = findTypebot.unknown_message;
const listening_from_me = findTypebot.listening_from_me;
const session = sessions.find((session) => session.remoteJid === remoteJid);
const prefilledVariables = {
remoteJid: remoteJid,
instanceName: instance.instanceName,
};
const createSessionData = {
url: url,
typebot: typebot,
remoteJid: remoteJid,
expire: expire,
keyword_finish: keyword_finish,
delay_message: delay_message,
unknown_message: unknown_message,
listening_from_me: listening_from_me,
sessions: sessions,
prefilledVariables: prefilledVariables,
};
const session = sessions.find((session) => session.remoteJid === remoteJid);
try {
if (session && expire && expire > 0) {
const now = Date.now();
@ -522,6 +560,8 @@ export class TypebotService {
}
if (session && session.status !== 'opened') {
this.logger.verbose('====== Typebot session not opened =====');
this.logger.verbose(session)
return;
}
@ -671,7 +711,39 @@ export class TypebotService {
request.data.input,
request.data.clientSideActions,
);
} catch(e) {
this.logger.verbose('============== Erro Sending message to Typebot! =================');
this.logger.verbose(e);
await this.recreateTypebot(instance, remoteJid, createSessionData, url, typebot, prefilledVariables)
}
return;
}
public async recreateTypebot(instance, remoteJid, createSessionData, url, typebot, prefilledVariables) {
this.logger.verbose('============== Trying recreate session! =================');
try{
await this.changeStatus(instance, {remoteJid: remoteJid, status: "closed"});
const response = await this.createNewSession(instance, createSessionData);
if (response.sessionId) {
await this.sendWAMessage(instance, remoteJid, response.messages, response.input, response.clientSideActions);
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
remoteJid: remoteJid,
url: url,
typebot: typebot,
prefilledVariables: prefilledVariables,
sessionId: `${response.sessionId}`,
});
this.logger.verbose('============== Session recreated! =================');
} else {
throw new Error('Session ID not found in response');
}
} catch (e) {
this.logger.verbose('============== Erro trying recreate typebot session! =================');
this.logger.verbose(e);
}
}
}

View File

@ -11,7 +11,7 @@
<body>
<iframe src="https://manager.evolution-api.com" frameborder="0" style="width: 100%; height: 100vh;"></iframe>
<iframe src="https://app.appsmith.com/app/manager/home-6563bdd6451fcc25a150d2bc" frameborder="0" style="width: 100%; height: 100vh;"></iframe>
</body>