feat: added compatibility with typebot v2

This commit is contained in:
Davidson Gomes
2023-11-30 17:13:05 -03:00
parent a1d13f8ff3
commit 876320b849
8 changed files with 47 additions and 8 deletions

View File

@@ -132,6 +132,7 @@ export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook };
export type ConfigSessionPhone = { CLIENT: string; NAME: string };
export type QrCode = { LIMIT: number; COLOR: string };
export type Typebot = { API_VERSION: string };
export type Production = boolean;
export interface Env {
@@ -150,6 +151,7 @@ export interface Env {
WEBHOOK: Webhook;
CONFIG_SESSION_PHONE: ConfigSessionPhone;
QRCODE: QrCode;
TYPEBOT: Typebot;
AUTHENTICATION: Auth;
PRODUCTION?: Production;
CHATWOOT?: Chatwoot;
@@ -305,6 +307,9 @@ export class ConfigService {
LIMIT: Number.parseInt(process.env.QRCODE_LIMIT) || 30,
COLOR: process.env.QRCODE_COLOR || '#198754',
},
TYPEBOT: {
API_VERSION: process.env?.TYPEBOT_API_VERSION || 'v1',
},
AUTHENTICATION: {
TYPE: process.env.AUTHENTICATION_TYPE as 'apikey',
API_KEY: {

View File

@@ -83,6 +83,13 @@ RABBITMQ:
ENABLED: false
URI: "amqp://guest:guest@localhost:5672"
SQS:
ENABLED: true
ACCESS_KEY_ID: ""
SECRET_ACCESS_KEY: ""
ACCOUNT_ID: ""
REGION: "us-east-1"
WEBSOCKET:
ENABLED: false
@@ -139,6 +146,9 @@ QRCODE:
LIMIT: 30
COLOR: "#198754"
TYPEBOT:
API_VERSION: 'v1' # v1 | v2
# Defines an authentication type for the api
# We recommend using the apikey because it will allow you to use a custom token,
# if you use jwt, a random token will be generated and may be expired and you will have to generate a new token

View File

@@ -1,5 +1,6 @@
import axios from 'axios';
import { ConfigService, Typebot } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { InstanceDto } from '../dto/instance.dto';
import { Session, TypebotDto } from '../dto/typebot.dto';
@@ -8,7 +9,7 @@ import { Events } from '../types/wa.types';
import { WAMonitoringService } from './monitor.service';
export class TypebotService {
constructor(private readonly waMonitor: WAMonitoringService) {}
constructor(private readonly waMonitor: WAMonitoringService, private readonly configService: ConfigService) {}
private readonly logger = new Logger(TypebotService.name);
@@ -162,7 +163,8 @@ export class TypebotService {
};
try {
const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData);
await this.sendWAMessage(
instance,
@@ -251,7 +253,8 @@ export class TypebotService {
};
try {
const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData);
if (request?.data?.sessionId) {
data.sessions.push({
@@ -554,7 +557,8 @@ export class TypebotService {
};
try {
const request = await axios.post(url + '/api/v1/sendMessage', reqData);
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData);
await this.sendWAMessage(
instance,
@@ -640,7 +644,9 @@ export class TypebotService {
let request: any;
try {
request = await axios.post(url + '/api/v1/sendMessage', reqData);
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
request = await axios.post(`${data.url}/api/${version}/sendMessage`, reqData);
await this.sendWAMessage(
instance,
remoteJid,
@@ -719,7 +725,8 @@ export class TypebotService {
sessionId: session.sessionId.split('-')[1],
};
const request = await axios.post(url + '/api/v1/sendMessage', reqData);
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
const request = await axios.post(`${url}/api/${version}/sendMessage`, reqData);
await this.sendWAMessage(
instance,

View File

@@ -166,7 +166,7 @@ export class WAStartupService {
private chatwootService = new ChatwootService(waMonitor, this.configService);
private typebotService = new TypebotService(waMonitor);
private typebotService = new TypebotService(waMonitor, this.configService);
private chamaaiService = new ChamaaiService(waMonitor, this.configService);

View File

@@ -102,7 +102,7 @@ export const waMonitor = new WAMonitoringService(eventEmitter, configService, re
const authService = new AuthService(configService, waMonitor, repository);
const typebotService = new TypebotService(waMonitor);
const typebotService = new TypebotService(waMonitor, configService);
export const typebotController = new TypebotController(typebotService);