mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 12:12:55 -06:00
remove whitespaces
This commit is contained in:
parent
d712cbc467
commit
a44aa1f646
@ -64,7 +64,7 @@ export class DifyService {
|
|||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
let endpoint: string = dify.apiUrl;
|
let endpoint: string = dify.apiUrl;
|
||||||
|
|
||||||
if (dify.botType === 'chatBot') {
|
if (dify.botType === 'chatBot') {
|
||||||
endpoint += '/chat-messages';
|
endpoint += '/chat-messages';
|
||||||
const payload: any = {
|
const payload: any = {
|
||||||
@ -80,10 +80,10 @@ export class DifyService {
|
|||||||
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
|
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
|
||||||
user: remoteJid,
|
user: remoteJid,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const contentSplit = content.split('|');
|
||||||
|
|
||||||
payload.files = [
|
payload.files = [
|
||||||
{
|
{
|
||||||
type: 'image',
|
type: 'image',
|
||||||
@ -93,26 +93,26 @@ export class DifyService {
|
|||||||
];
|
];
|
||||||
payload.query = contentSplit[2] || content;
|
payload.query = contentSplit[2] || content;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
await instance.client.presenceSubscribe(remoteJid);
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.post(endpoint, payload, {
|
const response = await axios.post(endpoint, payload, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${dify.apiKey}`,
|
Authorization: `Bearer ${dify.apiKey}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
const message = response?.data?.answer;
|
const message = response?.data?.answer;
|
||||||
const conversationId = response?.data?.conversation_id;
|
const conversationId = response?.data?.conversation_id;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
@ -124,7 +124,7 @@ export class DifyService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dify.botType === 'textGenerator') {
|
if (dify.botType === 'textGenerator') {
|
||||||
endpoint += '/completion-messages';
|
endpoint += '/completion-messages';
|
||||||
const payload: any = {
|
const payload: any = {
|
||||||
@ -140,10 +140,10 @@ export class DifyService {
|
|||||||
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
|
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
|
||||||
user: remoteJid,
|
user: remoteJid,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const contentSplit = content.split('|');
|
||||||
|
|
||||||
payload.files = [
|
payload.files = [
|
||||||
{
|
{
|
||||||
type: 'image',
|
type: 'image',
|
||||||
@ -153,26 +153,26 @@ export class DifyService {
|
|||||||
];
|
];
|
||||||
payload.inputs.query = contentSplit[2] || content;
|
payload.inputs.query = contentSplit[2] || content;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
await instance.client.presenceSubscribe(remoteJid);
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.post(endpoint, payload, {
|
const response = await axios.post(endpoint, payload, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${dify.apiKey}`,
|
Authorization: `Bearer ${dify.apiKey}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
const message = response?.data?.answer;
|
const message = response?.data?.answer;
|
||||||
const conversationId = response?.data?.conversation_id;
|
const conversationId = response?.data?.conversation_id;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
@ -184,7 +184,7 @@ export class DifyService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dify.botType === 'agent') {
|
if (dify.botType === 'agent') {
|
||||||
endpoint += '/chat-messages';
|
endpoint += '/chat-messages';
|
||||||
const payload: any = {
|
const payload: any = {
|
||||||
@ -200,10 +200,10 @@ export class DifyService {
|
|||||||
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
|
conversation_id: session.sessionId === remoteJid ? undefined : session.sessionId,
|
||||||
user: remoteJid,
|
user: remoteJid,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const contentSplit = content.split('|');
|
||||||
|
|
||||||
payload.files = [
|
payload.files = [
|
||||||
{
|
{
|
||||||
type: 'image',
|
type: 'image',
|
||||||
@ -213,25 +213,25 @@ export class DifyService {
|
|||||||
];
|
];
|
||||||
payload.query = contentSplit[2] || content;
|
payload.query = contentSplit[2] || content;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
await instance.client.presenceSubscribe(remoteJid);
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.post(endpoint, payload, {
|
const response = await axios.post(endpoint, payload, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${dify.apiKey}`,
|
Authorization: `Bearer ${dify.apiKey}`,
|
||||||
},
|
},
|
||||||
responseType: 'stream',
|
responseType: 'stream',
|
||||||
});
|
});
|
||||||
|
|
||||||
let conversationId;
|
let conversationId;
|
||||||
let answer = '';
|
let answer = '';
|
||||||
|
|
||||||
const stream = response.data;
|
const stream = response.data;
|
||||||
const reader = new Readable().wrap(stream);
|
const reader = new Readable().wrap(stream);
|
||||||
|
|
||||||
reader.on('data', (chunk) => {
|
reader.on('data', (chunk) => {
|
||||||
const data = chunk.toString().replace(/data:\s*/g, '');
|
const data = chunk.toString().replace(/data:\s*/g, '');
|
||||||
|
|
||||||
@ -257,15 +257,15 @@ export class DifyService {
|
|||||||
console.error('Error parsing stream data:', error);
|
console.error('Error parsing stream data:', error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
reader.on('end', async () => {
|
reader.on('end', async () => {
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
const message = answer;
|
const message = answer;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
@ -277,14 +277,14 @@ export class DifyService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
reader.on('error', (error) => {
|
reader.on('error', (error) => {
|
||||||
console.error('Error reading stream:', error);
|
console.error('Error reading stream:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dify.botType === 'workflow') {
|
if (dify.botType === 'workflow') {
|
||||||
endpoint += '/workflows/run';
|
endpoint += '/workflows/run';
|
||||||
const payload: any = {
|
const payload: any = {
|
||||||
@ -299,10 +299,10 @@ export class DifyService {
|
|||||||
response_mode: 'blocking',
|
response_mode: 'blocking',
|
||||||
user: remoteJid,
|
user: remoteJid,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const contentSplit = content.split('|');
|
||||||
|
|
||||||
payload.files = [
|
payload.files = [
|
||||||
{
|
{
|
||||||
type: 'image',
|
type: 'image',
|
||||||
@ -312,25 +312,25 @@ export class DifyService {
|
|||||||
];
|
];
|
||||||
payload.inputs.query = contentSplit[2] || content;
|
payload.inputs.query = contentSplit[2] || content;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
await instance.client.presenceSubscribe(remoteJid);
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.post(endpoint, payload, {
|
const response = await axios.post(endpoint, payload, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${dify.apiKey}`,
|
Authorization: `Bearer ${dify.apiKey}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
if (instance.integration === Integration.WHATSAPP_BAILEYS)
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
const message = response?.data?.data.outputs.text;
|
const message = response?.data?.data.outputs.text;
|
||||||
|
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
@ -340,14 +340,14 @@ export class DifyService {
|
|||||||
awaitUser: true,
|
awaitUser: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error.response?.data || error);
|
this.logger.error(error.response?.data || error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendMessageWhatsApp(instance: any, remoteJid: string, message: string, settings: DifySetting) {
|
private async sendMessageWhatsApp(instance: any, remoteJid: string, message: string, settings: DifySetting) {
|
||||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
||||||
|
Loading…
Reference in New Issue
Block a user