diff --git a/CHANGELOG.md b/CHANGELOG.md index 296673ec..5976b8f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ -# 2.1.2 (develop) +# 2.1.3 (develop) + +### Fixed + +* Fixed prefilledVariables in startTypebot + +# 2.1.2 (2024-10-06 10:09) ### Features diff --git a/prisma/mysql-schema.prisma b/prisma/mysql-schema.prisma index 2576b04f..cfa9d735 100644 --- a/prisma/mysql-schema.prisma +++ b/prisma/mysql-schema.prisma @@ -159,7 +159,7 @@ model Message { MessageUpdate MessageUpdate[] Media Media? webhookUrl String? @db.VarChar(500) - status Int? @db.Int + status String? @db.VarChar(30) sessionId String? session IntegrationSession? @relation(fields: [sessionId], references: [id]) diff --git a/prisma/postgresql-migrations/20241006130306_alter_status_on_message_table/migration.sql b/prisma/postgresql-migrations/20241006130306_alter_status_on_message_table/migration.sql new file mode 100644 index 00000000..ac923c98 --- /dev/null +++ b/prisma/postgresql-migrations/20241006130306_alter_status_on_message_table/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Message" ALTER COLUMN "status" SET DATA TYPE VARCHAR(30); diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index 4f061adb..857c4426 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -158,7 +158,7 @@ model Message { MessageUpdate MessageUpdate[] Media Media? webhookUrl String? @db.VarChar(500) - status Int? @db.Integer + status String? @db.VarChar(30) sessionId String? session IntegrationSession? @relation(fields: [sessionId], references: [id]) diff --git a/src/api/controllers/sendMessage.controller.ts b/src/api/controllers/sendMessage.controller.ts index 201d359c..8f22685e 100644 --- a/src/api/controllers/sendMessage.controller.ts +++ b/src/api/controllers/sendMessage.controller.ts @@ -48,11 +48,14 @@ export class SendMessageController { } public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) { - if (file || isURL(data.audio) || isBase64(data.audio)) { - return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file); + if (file?.buffer || isURL(data.audio) || isBase64(data.audio)) { + // Si file existe y tiene buffer, o si es una URL o Base64, continúa + return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file); + } else { + console.error('El archivo no tiene buffer o el audio no es una URL o Base64 válida'); + throw new BadRequestException('Owned media must be a url, base64, or valid file with buffer'); } - throw new BadRequestException('Owned media must be a url or base64'); - } +} public async sendButtons({ instanceName }: InstanceDto, data: SendButtonDto) { return await this.waMonitor.waInstances[instanceName].buttonMessage(data); diff --git a/src/api/integrations/channel/evolution/evolution.channel.service.ts b/src/api/integrations/channel/evolution/evolution.channel.service.ts index c3f3f4d6..10b28a4d 100644 --- a/src/api/integrations/channel/evolution/evolution.channel.service.ts +++ b/src/api/integrations/channel/evolution/evolution.channel.service.ts @@ -7,6 +7,7 @@ import { ChannelStartupService } from '@api/services/channel.service'; import { Events, wa } from '@api/types/wa.types'; import { Chatwoot, ConfigService, Openai } from '@config/env.config'; import { BadRequestException, InternalServerErrorException } from '@exceptions'; +import { status } from '@utils/renderStatus'; import { isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; import mime from 'mime'; @@ -273,72 +274,59 @@ export class EvolutionStartupService extends ChannelStartupService { const messageId = v4(); - let messageRaw: any; + let messageRaw: any = { + key: { fromMe: true, id: messageId, remoteJid: number }, + messageTimestamp: Math.round(new Date().getTime() / 1000), + webhookUrl, + source: 'unknown', + instanceId: this.instanceId, + status: status[1], + }; if (message?.mediaType === 'image') { messageRaw = { - key: { fromMe: true, id: messageId, remoteJid: number }, + ...messageRaw, message: { mediaUrl: message.media, quoted, }, messageType: 'imageMessage', - messageTimestamp: Math.round(new Date().getTime() / 1000), - webhookUrl, - source: 'unknown', - instanceId: this.instanceId, }; } else if (message?.mediaType === 'video') { messageRaw = { - key: { fromMe: true, id: messageId, remoteJid: number }, + ...messageRaw, message: { mediaUrl: message.media, quoted, }, messageType: 'videoMessage', - messageTimestamp: Math.round(new Date().getTime() / 1000), - webhookUrl, - source: 'unknown', - instanceId: this.instanceId, }; } else if (message?.mediaType === 'audio') { messageRaw = { - key: { fromMe: true, id: messageId, remoteJid: number }, + ...messageRaw, message: { mediaUrl: message.media, quoted, }, messageType: 'audioMessage', - messageTimestamp: Math.round(new Date().getTime() / 1000), - webhookUrl, - source: 'unknown', - instanceId: this.instanceId, }; } else if (message?.mediaType === 'document') { messageRaw = { - key: { fromMe: true, id: messageId, remoteJid: number }, + ...messageRaw, message: { mediaUrl: message.media, quoted, }, messageType: 'documentMessage', - messageTimestamp: Math.round(new Date().getTime() / 1000), - webhookUrl, - source: 'unknown', - instanceId: this.instanceId, }; } else { messageRaw = { - key: { fromMe: true, id: messageId, remoteJid: number }, + ...messageRaw, message: { ...message, quoted, }, messageType: 'conversation', - messageTimestamp: Math.round(new Date().getTime() / 1000), - webhookUrl, - source: 'unknown', - instanceId: this.instanceId, }; } @@ -483,7 +471,12 @@ export class EvolutionStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = file.buffer.toString('base64'); + if (file?.buffer) { + mediaData.audio = file.buffer.toString('base64'); + } else { + console.error('El archivo o buffer no est� definido correctamente.'); + throw new Error('File or buffer is undefined.'); + } const message = await this.processAudio(mediaData.audio, data.number); diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index f7eef675..3e163975 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -22,6 +22,7 @@ import { ChannelStartupService } from '@api/services/channel.service'; import { Events, wa } from '@api/types/wa.types'; import { Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@config/env.config'; import { BadRequestException, InternalServerErrorException } from '@exceptions'; +import { status } from '@utils/renderStatus'; import axios from 'axios'; import { arrayUnique, isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; @@ -895,12 +896,12 @@ export class BusinessStartupService extends ChannelStartupService { const messageRaw: any = { key: { fromMe: true, id: messageSent?.messages[0]?.id, remoteJid: this.createJid(number) }, - //pushName: messageSent.pushName, message: this.convertMessageToRaw(message, content), messageType: this.renderMessageType(content.type), messageTimestamp: (messageSent?.messages[0]?.timestamp as number) || Math.round(new Date().getTime() / 1000), instanceId: this.instanceId, webhookUrl, + status: status[1], source: 'unknown', }; @@ -1081,7 +1082,12 @@ export class BusinessStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = file.buffer.toString('base64'); + if (file?.buffer) { + mediaData.audio = file.buffer.toString('base64'); + } else { + console.error('El archivo no tiene buffer o file es undefined'); + throw new Error('File or buffer is undefined'); + } const message = await this.processAudio(mediaData.audio, data.number); diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 1b84b81e..58a729ea 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -73,6 +73,7 @@ import { Boom } from '@hapi/boom'; import { Instance } from '@prisma/client'; import { makeProxyAgent } from '@utils/makeProxyAgent'; import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache'; +import { status } from '@utils/renderStatus'; import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma'; import { AuthStateProvider } from '@utils/use-multi-file-auth-state-provider-files'; import { useMultiFileAuthStateRedisDb } from '@utils/use-multi-file-auth-state-redis-db'; @@ -570,7 +571,6 @@ export class BaileysStartupService extends ChannelStartupService { const isGroupJid = this.localSettings.groupsIgnore && isJidGroup(jid); const isBroadcast = !this.localSettings.readStatus && isJidBroadcast(jid); const isNewsletter = isJidNewsletter(jid); - // const isNewsletter = jid && jid.includes('newsletter'); return isGroupJid || isBroadcast || isNewsletter; }, @@ -1231,14 +1231,6 @@ export class BaileysStartupService extends ChannelStartupService { }, 'messages.update': async (args: WAMessageUpdate[], settings: any) => { - const status: Record = { - 0: 'ERROR', - 1: 'PENDING', - 2: 'SERVER_ACK', - 3: 'DELIVERY_ACK', - 4: 'READ', - 5: 'PLAYED', - }; for await (const { key, update } of args) { if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) { return; @@ -2590,7 +2582,12 @@ export class BaileysStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = file.buffer.toString('base64'); + if (file?.buffer) { + mediaData.audio = file.buffer.toString('base64'); + } else if (!isURL(data.audio) && !isBase64(data.audio)) { + console.error('Invalid file or audio source'); + throw new BadRequestException('File buffer, URL, or base64 audio is required'); + } if (!data?.encoding && data?.encoding !== false) { data.encoding = true; @@ -3670,7 +3667,7 @@ export class BaileysStartupService extends ChannelStartupService { const messageRaw = { key: message.key, pushName: message.pushName, - status: message.status, + status: status[message.status], message: { ...message.message }, contextInfo: contentMsg?.contextInfo, messageType: contentType || 'unknown', diff --git a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts index 1f03197f..e0818a68 100644 --- a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts +++ b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts @@ -5,7 +5,7 @@ import { TypebotService } from '@api/integrations/chatbot/typebot/services/typeb import { PrismaRepository } from '@api/repository/repository.service'; import { WAMonitoringService } from '@api/services/monitor.service'; import { Events } from '@api/types/wa.types'; -import { Auth, configService, HttpServer, Typebot } from '@config/env.config'; +import { configService, Typebot } from '@config/env.config'; import { Logger } from '@config/logger.config'; import { BadRequestException } from '@exceptions'; import { Typebot as TypebotModel } from '@prisma/client'; @@ -609,13 +609,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr } } - const prefilledVariables = { - remoteJid: remoteJid, - instanceName: instance.instanceName, - serverUrl: configService.get('SERVER').URL, - apiKey: configService.get('AUTHENTICATION').API_KEY.KEY, - ownerJid: instanceData.number, - }; + const prefilledVariables: any = {}; if (variables?.length) { variables.forEach((variable: { name: string | number; value: string }) => { @@ -674,6 +668,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr stopBotFromMe, keepOpen, 'init', + prefilledVariables, ); // const response = await this.typebotService.createNewSession(instanceData, { diff --git a/src/api/integrations/chatbot/typebot/services/typebot.service.ts b/src/api/integrations/chatbot/typebot/services/typebot.service.ts index b6de1b8d..2f30e091 100644 --- a/src/api/integrations/chatbot/typebot/services/typebot.service.ts +++ b/src/api/integrations/chatbot/typebot/services/typebot.service.ts @@ -356,6 +356,7 @@ export class TypebotService { stopBotFromMe: boolean, keepOpen: boolean, content: string, + prefilledVariables?: any, ) { if (session && expire && expire > 0) { const now = Date.now(); @@ -397,6 +398,7 @@ export class TypebotService { remoteJid: remoteJid, pushName: msg.pushName, botId: findTypebot.id, + prefilledVariables: prefilledVariables, }); if (data.session) { @@ -524,6 +526,7 @@ export class TypebotService { remoteJid: remoteJid, pushName: msg?.pushName, botId: findTypebot.id, + prefilledVariables: prefilledVariables, }); if (data?.session) { diff --git a/src/utils/renderStatus.ts b/src/utils/renderStatus.ts new file mode 100644 index 00000000..e57b1224 --- /dev/null +++ b/src/utils/renderStatus.ts @@ -0,0 +1,10 @@ +import { wa } from '@api/types/wa.types'; + +export const status: Record = { + 0: 'ERROR', + 1: 'PENDING', + 2: 'SERVER_ACK', + 3: 'DELIVERY_ACK', + 4: 'READ', + 5: 'PLAYED', +};