mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
fix: dify agent integration
This commit is contained in:
parent
e9f4477d11
commit
2196f65b7a
@ -11,6 +11,7 @@
|
||||
### Fixed
|
||||
|
||||
* Refactor integrations structure for modular system
|
||||
* Fixed dify agent integration
|
||||
|
||||
# 2.0.10 (2024-08-16 16:23)
|
||||
|
||||
|
@ -41,6 +41,15 @@ export class DifyService {
|
||||
return content.includes('imageMessage');
|
||||
}
|
||||
|
||||
private isJSON(str: string): boolean {
|
||||
try {
|
||||
JSON.parse(str);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async sendMessageToBot(
|
||||
instance: any,
|
||||
session: IntegrationSession,
|
||||
@ -50,6 +59,7 @@ export class DifyService {
|
||||
pushName: string,
|
||||
content: string,
|
||||
) {
|
||||
try {
|
||||
let endpoint: string = dify.apiUrl;
|
||||
|
||||
if (dify.botType === 'chatBot') {
|
||||
@ -94,8 +104,20 @@ export class DifyService {
|
||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||
|
||||
const message = response?.data?.answer;
|
||||
const conversationId = response?.data?.conversation_id;
|
||||
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
where: {
|
||||
id: session.id,
|
||||
},
|
||||
data: {
|
||||
status: 'opened',
|
||||
awaitUser: true,
|
||||
sessionId: session.sessionId === remoteJid ? conversationId : session.sessionId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (dify.botType === 'textGenerator') {
|
||||
@ -140,8 +162,20 @@ export class DifyService {
|
||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||
|
||||
const message = response?.data?.answer;
|
||||
const conversationId = response?.data?.conversation_id;
|
||||
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
where: {
|
||||
id: session.id,
|
||||
},
|
||||
data: {
|
||||
status: 'opened',
|
||||
awaitUser: true,
|
||||
sessionId: session.sessionId === remoteJid ? conversationId : session.sessionId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (dify.botType === 'agent') {
|
||||
@ -185,6 +219,7 @@ export class DifyService {
|
||||
});
|
||||
|
||||
let conversationId;
|
||||
let answer = '';
|
||||
|
||||
const stream = response.data;
|
||||
const reader = new Readable().wrap(stream);
|
||||
@ -193,9 +228,14 @@ export class DifyService {
|
||||
const data = chunk.toString();
|
||||
|
||||
try {
|
||||
const event = JSON.parse(data);
|
||||
if (event.event === 'agent_message') {
|
||||
const cleanedData = data.replace(/^data:\s*/, '');
|
||||
|
||||
const event = JSON.parse(cleanedData);
|
||||
|
||||
if (event?.event === 'agent_message') {
|
||||
console.log('event:', event);
|
||||
conversationId = conversationId ?? event?.conversation_id;
|
||||
answer += event?.answer;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing stream data:', error);
|
||||
@ -205,9 +245,21 @@ export class DifyService {
|
||||
reader.on('end', async () => {
|
||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||
|
||||
const message = response?.data?.answer;
|
||||
const message = answer;
|
||||
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
||||
console.log('message:', answer);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
where: {
|
||||
id: session.id,
|
||||
},
|
||||
data: {
|
||||
status: 'opened',
|
||||
awaitUser: true,
|
||||
sessionId: conversationId,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
reader.on('error', (error) => {
|
||||
@ -259,19 +311,27 @@ export class DifyService {
|
||||
|
||||
const message = response?.data?.data.outputs.text;
|
||||
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, session, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
where: {
|
||||
id: session.id,
|
||||
},
|
||||
data: {
|
||||
status: 'opened',
|
||||
awaitUser: true,
|
||||
},
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(error.response?.data || error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async sendMessageWhatsApp(
|
||||
instance: any,
|
||||
remoteJid: string,
|
||||
message: string,
|
||||
session: IntegrationSession,
|
||||
settings: DifySetting,
|
||||
) {
|
||||
private async sendMessageWhatsApp(instance: any, remoteJid: string, message: string, settings: DifySetting) {
|
||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
||||
|
||||
const result = [];
|
||||
@ -318,23 +378,6 @@ export class DifyService {
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.keepOpen) {
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
where: {
|
||||
id: session.id,
|
||||
},
|
||||
data: {
|
||||
status: 'closed',
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await this.prismaRepository.integrationSession.delete({
|
||||
where: {
|
||||
id: session.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
sendTelemetry('/message/sendText');
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user