feat: Added support for new typebot API

This commit is contained in:
Davidson Gomes 2023-12-12 16:55:46 -03:00
parent ade3952016
commit ff06cd7643
5 changed files with 67 additions and 26 deletions

View File

@ -2,7 +2,7 @@
### Feature ### Feature
* Added AWS SQS Integration * Added AWS SQS Integration
* Added compatibility with typebot v2 * Added support for new typebot API
* Added endpoint sendPresence * Added endpoint sendPresence
* New Instance Manager * New Instance Manager
* Added auto_create to the chatwoot set to create the inbox automatically or not * Added auto_create to the chatwoot set to create the inbox automatically or not

View File

@ -105,7 +105,8 @@ CONFIG_SESSION_PHONE_NAME=Chrome
QRCODE_LIMIT=30 QRCODE_LIMIT=30
QRCODE_COLOR=#198754 QRCODE_COLOR=#198754
TYPEBOT_API_VERSION=v1 # old | latest
TYPEBOT_API_VERSION=latest
# Defines an authentication type for the api # Defines an authentication type for the api
# We recommend using the apikey because it will allow you to use a custom token, # We recommend using the apikey because it will allow you to use a custom token,

View File

@ -104,7 +104,7 @@ ENV CONFIG_SESSION_PHONE_NAME=Chrome
ENV QRCODE_LIMIT=30 ENV QRCODE_LIMIT=30
ENV QRCODE_COLOR=#198754 ENV QRCODE_COLOR=#198754
ENV TYPEBOT_API_VERSION=v1 ENV TYPEBOT_API_VERSION=latest
ENV AUTHENTICATION_TYPE=apikey ENV AUTHENTICATION_TYPE=apikey

View File

@ -147,7 +147,7 @@ QRCODE:
COLOR: "#198754" COLOR: "#198754"
TYPEBOT: TYPEBOT:
API_VERSION: 'v1' # v1 | v2 API_VERSION: 'latest' # old | latest
# Defines an authentication type for the api # Defines an authentication type for the api
# We recommend using the apikey because it will allow you to use a custom token, # We recommend using the apikey because it will allow you to use a custom token,

View File

@ -171,14 +171,20 @@ export class TypebotService {
const reqData = { const reqData = {
startParams: { startParams: {
typebot: data.typebot, publicId: data.typebot,
prefilledVariables: prefilledVariables, prefilledVariables: prefilledVariables,
}, },
}; };
try { try {
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION; const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData); let url: string;
if (version === 'latest') {
url = `${data.url}/api/v1/typebots/${data.typebot}/startChat`;
} else {
url = `${data.url}/api/v1/sendMessage`;
}
const request = await axios.post(url, reqData);
await this.sendWAMessage( await this.sendWAMessage(
instance, instance,
@ -256,7 +262,7 @@ export class TypebotService {
const reqData = { const reqData = {
startParams: { startParams: {
typebot: data.typebot, publicId: data.typebot,
prefilledVariables: { prefilledVariables: {
...data.prefilledVariables, ...data.prefilledVariables,
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
@ -268,7 +274,13 @@ export class TypebotService {
try { try {
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION; const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData); let url: string;
if (version === 'latest') {
url = `${data.url}/api/v1/typebots/${data.typebot}/startChat`;
} else {
url = `${data.url}/api/v1/sendMessage`;
}
const request = await axios.post(url, reqData);
if (request?.data?.sessionId) { if (request?.data?.sessionId) {
data.sessions.push({ data.sessions.push({
@ -565,14 +577,24 @@ export class TypebotService {
return; return;
} }
const reqData = {
message: content,
sessionId: data.sessionId,
};
try { try {
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION; const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData); let urlTypebot: string;
let reqData: {};
if (version === 'latest') {
urlTypebot = `${data.url}/api/v1/sessions/${data.sessionId}/continueChat`;
reqData = {
message: content,
};
} else {
urlTypebot = `${data.url}/api/v1/sendMessage`;
reqData = {
message: content,
sessionId: data.sessionId,
};
}
const request = await axios.post(urlTypebot, reqData);
await this.sendWAMessage( await this.sendWAMessage(
instance, instance,
@ -651,15 +673,24 @@ export class TypebotService {
return; return;
} }
const reqData = {
message: content,
sessionId: data.sessionId,
};
let request: any; let request: any;
try { try {
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION; const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData); let urlTypebot: string;
let reqData: {};
if (version === 'latest') {
urlTypebot = `${data.url}/api/v1/sessions/${data.sessionId}/continueChat`;
reqData = {
message: content,
};
} else {
urlTypebot = `${data.url}/api/v1/sendMessage`;
reqData = {
message: content,
sessionId: data.sessionId,
};
}
request = await axios.post(urlTypebot, reqData);
await this.sendWAMessage( await this.sendWAMessage(
instance, instance,
@ -734,13 +765,22 @@ export class TypebotService {
return; return;
} }
const reqData = {
message: content,
sessionId: session.sessionId.split('-')[1],
};
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION; const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${url}/api/${version}/sendMessage`, reqData); let urlTypebot: string;
let reqData: {};
if (version === 'latest') {
urlTypebot = `${url}/api/v1/sessions/${session.sessionId.split('-')[1]}/continueChat`;
reqData = {
message: content,
};
} else {
urlTypebot = `${url}/api/v1/sendMessage`;
reqData = {
message: content,
sessionId: session.sessionId.split('-')[1],
};
}
const request = await axios.post(urlTypebot, reqData);
await this.sendWAMessage( await this.sendWAMessage(
instance, instance,