feat: Variables passed to the input in dify

This commit is contained in:
Davidson Gomes 2024-08-08 17:51:12 -03:00
parent d82945a735
commit 59edf4ffb7
5 changed files with 118 additions and 59 deletions

View File

@ -1,3 +1,10 @@
# 2.0.8-rc (release candidate)
### Features
* Variables passed to the input in dify
* OwnerJid passed to typebot
# 2.0.7-rc (2024-08-03 14:04) # 2.0.7-rc (2024-08-03 14:04)
### Fixed ### Fixed

View File

@ -90,9 +90,7 @@ Join our Evolution Pro community for expert support and a weekly call to answer
#### Buy me coffe - PIX #### Buy me coffe - PIX
<div align="center"> <div align="center">
<a href="https://bmc.link/evolutionapi" target="_blank" rel="noopener noreferrer"> <img src="./public/images/qrcode-pix.png" style="width: 50% !important;">
<img src="./public/images/qrcode-pix.png" style="width: 50% !important;">
</a>
<p><b>CHAVE PIX (Telefone):</b> (74)99987-9409</p> <p><b>CHAVE PIX (Telefone):</b> (74)99987-9409</p>
</div> </div>

View File

@ -1,6 +1,6 @@
{ {
"name": "evolution-api", "name": "evolution-api",
"version": "2.0.6-rc", "version": "2.0.7-rc",
"description": "Rest api for communication with WhatsApp", "description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js", "main": "./dist/src/main.js",
"scripts": { "scripts": {

View File

@ -2,7 +2,7 @@ import { Dify, DifySession, DifySetting, Message } from '@prisma/client';
import axios from 'axios'; import axios from 'axios';
import { Readable } from 'stream'; import { Readable } from 'stream';
import { ConfigService, S3 } from '../../../../config/env.config'; import { Auth, ConfigService, HttpServer, S3 } from '../../../../config/env.config';
import { Logger } from '../../../../config/logger.config'; import { Logger } from '../../../../config/logger.config';
import { sendTelemetry } from '../../../../utils/sendTelemetry'; import { sendTelemetry } from '../../../../utils/sendTelemetry';
import { InstanceDto } from '../../../dto/instance.dto'; import { InstanceDto } from '../../../dto/instance.dto';
@ -999,6 +999,7 @@ export class DifyService {
session, session,
settings, settings,
debouncedContent, debouncedContent,
msg?.pushName,
); );
}); });
} else { } else {
@ -1009,6 +1010,7 @@ export class DifyService {
session, session,
settings, settings,
content, content,
msg?.pushName,
); );
} }
@ -1046,6 +1048,7 @@ export class DifyService {
settings: DifySetting, settings: DifySetting,
session: DifySession, session: DifySession,
content: string, content: string,
pushName?: string,
) { ) {
const data = await this.createNewSession(instance, { const data = await this.createNewSession(instance, {
remoteJid, remoteJid,
@ -1061,7 +1064,13 @@ export class DifyService {
if (dify.botType === 'chatBot') { if (dify.botType === 'chatBot') {
endpoint += '/chat-messages'; endpoint += '/chat-messages';
const payload = { const payload = {
inputs: {}, inputs: {
remoteJid: remoteJid,
pushName: pushName,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
},
query: content, query: content,
response_mode: 'blocking', response_mode: 'blocking',
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId, conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
@ -1112,6 +1121,11 @@ export class DifyService {
const payload = { const payload = {
inputs: { inputs: {
query: content, query: content,
pushName: pushName,
remoteJid: remoteJid,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
}, },
response_mode: 'blocking', response_mode: 'blocking',
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId, conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
@ -1160,7 +1174,13 @@ export class DifyService {
if (dify.botType === 'agent') { if (dify.botType === 'agent') {
endpoint += '/chat-messages'; endpoint += '/chat-messages';
const payload = { const payload = {
inputs: {}, inputs: {
remoteJid: remoteJid,
pushName: pushName,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
},
query: content, query: content,
response_mode: 'streaming', response_mode: 'streaming',
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId, conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
@ -1238,6 +1258,11 @@ export class DifyService {
const payload = { const payload = {
inputs: { inputs: {
query: content, query: content,
remoteJid: remoteJid,
pushName: pushName,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
}, },
response_mode: 'blocking', response_mode: 'blocking',
user: remoteJid, user: remoteJid,
@ -1298,6 +1323,7 @@ export class DifyService {
session: DifySession, session: DifySession,
settings: DifySetting, settings: DifySetting,
content: string, content: string,
pushName?: string,
) { ) {
if (session && session.status !== 'opened') { if (session && session.status !== 'opened') {
return; return;
@ -1331,13 +1357,13 @@ export class DifyService {
}); });
} }
await this.initNewSession(instance, remoteJid, dify, settings, session, content); await this.initNewSession(instance, remoteJid, dify, settings, session, content, pushName);
return; return;
} }
} }
if (!session) { if (!session) {
await this.initNewSession(instance, remoteJid, dify, settings, session, content); await this.initNewSession(instance, remoteJid, dify, settings, session, content, pushName);
return; return;
} }
@ -1393,7 +1419,13 @@ export class DifyService {
if (dify.botType === 'chatBot') { if (dify.botType === 'chatBot') {
endpoint += '/chat-messages'; endpoint += '/chat-messages';
const payload = { const payload = {
inputs: {}, inputs: {
remoteJid: remoteJid,
pushName: pushName,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
},
query: content, query: content,
response_mode: 'blocking', response_mode: 'blocking',
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId, conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
@ -1444,6 +1476,11 @@ export class DifyService {
const payload = { const payload = {
inputs: { inputs: {
query: content, query: content,
remoteJid: remoteJid,
pushName: pushName,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
}, },
response_mode: 'blocking', response_mode: 'blocking',
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId, conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
@ -1492,7 +1529,13 @@ export class DifyService {
if (dify.botType === 'agent') { if (dify.botType === 'agent') {
endpoint += '/chat-messages'; endpoint += '/chat-messages';
const payload = { const payload = {
inputs: {}, inputs: {
remoteJid: remoteJid,
pushName: pushName,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
},
query: content, query: content,
response_mode: 'streaming', response_mode: 'streaming',
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId, conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
@ -1572,6 +1615,11 @@ export class DifyService {
const payload = { const payload = {
inputs: { inputs: {
query: content, query: content,
remoteJid: remoteJid,
pushName: pushName,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
}, },
response_mode: 'blocking', response_mode: 'blocking',
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId, conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,

View File

@ -1,4 +1,4 @@
import { Message, Typebot as TypebotModel, TypebotSession } from '@prisma/client'; import { Instance, Message, Typebot as TypebotModel, TypebotSession } from '@prisma/client';
import axios from 'axios'; import axios from 'axios';
import { Auth, ConfigService, HttpServer, S3, Typebot } from '../../../../config/env.config'; import { Auth, ConfigService, HttpServer, S3, Typebot } from '../../../../config/env.config';
@ -659,13 +659,13 @@ export class TypebotService {
public async startTypebot(instance: InstanceDto, data: any) { public async startTypebot(instance: InstanceDto, data: any) {
if (data.remoteJid === 'status@broadcast') return; if (data.remoteJid === 'status@broadcast') return;
const instanceId = await this.prismaRepository.instance const instanceData = await this.prismaRepository.instance.findFirst({
.findFirst({ where: {
where: { name: instance.instanceName,
name: instance.instanceName, },
}, });
})
.then((instance) => instance.id); if (!instanceData) throw new Error('Instance not found');
const remoteJid = data.remoteJid; const remoteJid = data.remoteJid;
const url = data.url; const url = data.url;
@ -682,7 +682,7 @@ export class TypebotService {
const defaultSettingCheck = await this.prismaRepository.typebotSetting.findFirst({ const defaultSettingCheck = await this.prismaRepository.typebotSetting.findFirst({
where: { where: {
instanceId, instanceId: instanceData.id,
}, },
}); });
@ -751,6 +751,7 @@ export class TypebotService {
instanceName: instance.instanceName, instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL, serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY, apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
ownerJid: instanceData.number,
}; };
if (variables?.length) { if (variables?.length) {
@ -764,7 +765,7 @@ export class TypebotService {
where: { where: {
url: url, url: url,
typebot: typebot, typebot: typebot,
instanceId, instanceId: instanceData.id,
}, },
}); });
@ -782,7 +783,7 @@ export class TypebotService {
listeningFromMe: listeningFromMe, listeningFromMe: listeningFromMe,
stopBotFromMe: stopBotFromMe, stopBotFromMe: stopBotFromMe,
keepOpen: keepOpen, keepOpen: keepOpen,
instanceId, instanceId: instanceData.id,
}, },
}); });
} }
@ -790,35 +791,29 @@ export class TypebotService {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.typebotSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
instanceId, instanceId: instanceData.id,
}, },
}); });
const response = await this.createNewSession( const response = await this.createNewSession(instanceData, {
{ enabled: true,
instanceName: instance.instanceName, url: url,
instanceId: instanceId, typebot: typebot,
}, remoteJid: remoteJid,
{ expire: expire,
enabled: true, keywordFinish: keywordFinish,
url: url, delayMessage: delayMessage,
typebot: typebot, unknownMessage: unknownMessage,
remoteJid: remoteJid, listeningFromMe: listeningFromMe,
expire: expire, stopBotFromMe: stopBotFromMe,
keywordFinish: keywordFinish, keepOpen: keepOpen,
delayMessage: delayMessage, prefilledVariables: prefilledVariables,
unknownMessage: unknownMessage, typebotId: findTypebot.id,
listeningFromMe: listeningFromMe, });
stopBotFromMe: stopBotFromMe,
keepOpen: keepOpen,
prefilledVariables: prefilledVariables,
typebotId: findTypebot.id,
},
);
if (response.sessionId) { if (response.sessionId) {
await this.sendWAMessage( await this.sendWAMessage(
instance, instanceData,
response.session, response.session,
{ {
expire: expire, expire: expire,
@ -871,7 +866,7 @@ export class TypebotService {
const request = await axios.post(url, reqData); const request = await axios.post(url, reqData);
await this.sendWAMessage( await this.sendWAMessage(
instance, instanceData,
null, null,
{ {
expire: expire, expire: expire,
@ -966,7 +961,7 @@ export class TypebotService {
return messageContent; return messageContent;
} }
public async createNewSession(instance: InstanceDto, data: any) { public async createNewSession(instance: Instance, data: any) {
if (data.remoteJid === 'status@broadcast') return; if (data.remoteJid === 'status@broadcast') return;
const id = Math.floor(Math.random() * 10000000000).toString(); const id = Math.floor(Math.random() * 10000000000).toString();
@ -982,9 +977,10 @@ export class TypebotService {
...data.prefilledVariables, ...data.prefilledVariables,
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
pushName: data.pushName || data.prefilledVariables?.pushName || '', pushName: data.pushName || data.prefilledVariables?.pushName || '',
instanceName: instance.instanceName, instanceName: instance.name,
serverUrl: this.configService.get<HttpServer>('SERVER').URL, serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY, apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
ownerJid: instance.number,
}, },
}; };
} else { } else {
@ -997,9 +993,10 @@ export class TypebotService {
...data.prefilledVariables, ...data.prefilledVariables,
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
pushName: data.pushName || data.prefilledVariables?.pushName || '', pushName: data.pushName || data.prefilledVariables?.pushName || '',
instanceName: instance.instanceName, instanceName: instance.name,
serverUrl: this.configService.get<HttpServer>('SERVER').URL, serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY, apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
ownerJid: instance.number,
}, },
}, },
}; };
@ -1018,13 +1015,14 @@ export class TypebotService {
...data.prefilledVariables, ...data.prefilledVariables,
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
pushName: data.pushName || '', pushName: data.pushName || '',
instanceName: instance.instanceName, instanceName: instance.name,
serverUrl: this.configService.get<HttpServer>('SERVER').URL, serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY, apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
ownerJid: instance.number,
}, },
awaitUser: false, awaitUser: false,
typebotId: data.typebotId, typebotId: data.typebotId,
instanceId: instance.instanceId, instanceId: instance.id,
}, },
}); });
} }
@ -1036,7 +1034,7 @@ export class TypebotService {
} }
public async sendWAMessage( public async sendWAMessage(
instance: InstanceDto, instance: Instance,
session: TypebotSession, session: TypebotSession,
settings: { settings: {
expire: number; expire: number;
@ -1053,7 +1051,7 @@ export class TypebotService {
clientSideActions: any, clientSideActions: any,
) { ) {
processMessages( processMessages(
this.waMonitor.waInstances[instance.instanceName], this.waMonitor.waInstances[instance.name],
session, session,
settings, settings,
messages, messages,
@ -1436,6 +1434,14 @@ export class TypebotService {
public async sendTypebot(instance: InstanceDto, remoteJid: string, msg: Message) { public async sendTypebot(instance: InstanceDto, remoteJid: string, msg: Message) {
try { try {
const instanceData = await this.prismaRepository.instance.findFirst({
where: {
name: instance.instanceName,
},
});
if (!instanceData) throw new Error('Instance not found');
const settings = await this.prismaRepository.typebotSetting.findFirst({ const settings = await this.prismaRepository.typebotSetting.findFirst({
where: { where: {
instanceId: instance.instanceId, instanceId: instance.instanceId,
@ -1569,7 +1575,7 @@ export class TypebotService {
if (debounceTime && debounceTime > 0) { if (debounceTime && debounceTime > 0) {
this.processDebounce(content, remoteJid, debounceTime, async (debouncedContent) => { this.processDebounce(content, remoteJid, debounceTime, async (debouncedContent) => {
await this.processTypebot( await this.processTypebot(
instance, instanceData,
remoteJid, remoteJid,
msg, msg,
session, session,
@ -1588,7 +1594,7 @@ export class TypebotService {
}); });
} else { } else {
await this.processTypebot( await this.processTypebot(
instance, instanceData,
remoteJid, remoteJid,
msg, msg,
session, session,
@ -1614,7 +1620,7 @@ export class TypebotService {
} }
private async processTypebot( private async processTypebot(
instance: InstanceDto, instance: Instance,
remoteJid: string, remoteJid: string,
msg: Message, msg: Message,
session: TypebotSession, session: TypebotSession,
@ -1699,7 +1705,7 @@ export class TypebotService {
if (!content) { if (!content) {
if (unknownMessage) { if (unknownMessage) {
this.waMonitor.waInstances[instance.instanceName].textMessage( this.waMonitor.waInstances[instance.name].textMessage(
{ {
number: remoteJid.split('@')[0], number: remoteJid.split('@')[0],
delay: delayMessage || 1000, delay: delayMessage || 1000,
@ -1824,7 +1830,7 @@ export class TypebotService {
if (data.messages.length === 0) { if (data.messages.length === 0) {
if (!content) { if (!content) {
if (unknownMessage) { if (unknownMessage) {
this.waMonitor.waInstances[instance.instanceName].textMessage( this.waMonitor.waInstances[instance.name].textMessage(
{ {
number: remoteJid.split('@')[0], number: remoteJid.split('@')[0],
delay: delayMessage || 1000, delay: delayMessage || 1000,
@ -1916,7 +1922,7 @@ export class TypebotService {
if (!content) { if (!content) {
if (unknownMessage) { if (unknownMessage) {
this.waMonitor.waInstances[instance.instanceName].textMessage( this.waMonitor.waInstances[instance.name].textMessage(
{ {
number: remoteJid.split('@')[0], number: remoteJid.split('@')[0],
delay: delayMessage || 1000, delay: delayMessage || 1000,