Skip to content

Commit 407d254

Browse files
committed
refactor(chatbot): streamline media message handling across chatbot services
- Removed redundant instance name references in EvolutionStartupService to enhance data consistency. - Updated media message processing in various chatbot services to utilize base64 and mediaUrl more effectively, ensuring better handling of image messages. - Improved overall code readability and maintainability by simplifying media handling logic.
1 parent 5f44da6 commit 407d254

File tree

7 files changed

+217
-98
lines changed

7 files changed

+217
-98
lines changed

src/api/integrations/channel/evolution/evolution.channel.service.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ export class EvolutionStartupService extends ChannelStartupService {
331331
messageTimestamp: Math.round(new Date().getTime() / 1000),
332332
webhookUrl,
333333
source: 'unknown',
334-
instanceName: this.instance.name,
335334
instanceId: this.instanceId,
336335
};
337336
} else if (message?.mediaType === 'video') {
@@ -346,7 +345,6 @@ export class EvolutionStartupService extends ChannelStartupService {
346345
messageTimestamp: Math.round(new Date().getTime() / 1000),
347346
webhookUrl,
348347
source: 'unknown',
349-
instanceName: this.instance.name,
350348
instanceId: this.instanceId,
351349
};
352350
} else if (message?.mediaType === 'audio') {
@@ -361,7 +359,6 @@ export class EvolutionStartupService extends ChannelStartupService {
361359
messageTimestamp: Math.round(new Date().getTime() / 1000),
362360
webhookUrl,
363361
source: 'unknown',
364-
instanceName: this.instance.name,
365362
instanceId: this.instanceId,
366363
};
367364

@@ -383,7 +380,6 @@ export class EvolutionStartupService extends ChannelStartupService {
383380
messageTimestamp: Math.round(new Date().getTime() / 1000),
384381
webhookUrl,
385382
source: 'unknown',
386-
instanceName: this.instance.name,
387383
instanceId: this.instanceId,
388384
};
389385
} else if (message.buttonMessage) {
@@ -400,7 +396,6 @@ export class EvolutionStartupService extends ChannelStartupService {
400396
messageTimestamp: Math.round(new Date().getTime() / 1000),
401397
webhookUrl,
402398
source: 'unknown',
403-
instanceName: this.instance.name,
404399
instanceId: this.instanceId,
405400
};
406401
} else if (message.listMessage) {
@@ -414,7 +409,6 @@ export class EvolutionStartupService extends ChannelStartupService {
414409
messageTimestamp: Math.round(new Date().getTime() / 1000),
415410
webhookUrl,
416411
source: 'unknown',
417-
instanceName: this.instance.name,
418412
instanceId: this.instanceId,
419413
};
420414
} else {
@@ -428,7 +422,6 @@ export class EvolutionStartupService extends ChannelStartupService {
428422
messageTimestamp: Math.round(new Date().getTime() / 1000),
429423
webhookUrl,
430424
source: 'unknown',
431-
instanceName: this.instance.name,
432425
instanceId: this.instanceId,
433426
};
434427
}

src/api/integrations/chatbot/chatbot.controller.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,19 @@ export class ChatbotController {
9191
pushName,
9292
isIntegration,
9393
};
94-
await evolutionBotController.emit(emitData);
94+
evolutionBotController.emit(emitData);
9595

96-
await typebotController.emit(emitData);
96+
typebotController.emit(emitData);
9797

98-
await openaiController.emit(emitData);
98+
openaiController.emit(emitData);
9999

100-
await difyController.emit(emitData);
100+
difyController.emit(emitData);
101101

102-
await n8nController.emit(emitData);
102+
n8nController.emit(emitData);
103103

104-
await evoaiController.emit(emitData);
104+
evoaiController.emit(emitData);
105105

106-
await flowiseController.emit(emitData);
106+
flowiseController.emit(emitData);
107107
}
108108

109109
public processDebounce(

src/api/integrations/chatbot/dify/services/dify.service.ts

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Integration } from '@api/types/wa.types';
44
import { ConfigService, HttpServer } from '@config/env.config';
55
import { Dify, DifySetting, IntegrationSession } from '@prisma/client';
66
import axios from 'axios';
7+
import { isURL } from 'class-validator';
78

89
import { BaseChatbotService } from '../../base-chatbot.service';
910
import { OpenaiService } from '../../openai/services/openai.service';
@@ -78,15 +79,35 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
7879

7980
// Handle image messages
8081
if (this.isImageMessage(content)) {
81-
const contentSplit = content.split('|');
82-
payload.files = [
83-
{
84-
type: 'image',
85-
transfer_method: 'remote_url',
86-
url: contentSplit[1].split('?')[0],
87-
},
88-
];
89-
payload.query = contentSplit[2] || content;
82+
const media = content.split('|');
83+
84+
if (msg.message.mediaUrl || msg.message.base64) {
85+
let mediaBase64 = msg.message.base64 || null;
86+
87+
if (msg.message.mediaUrl && isURL(msg.message.mediaUrl)) {
88+
const result = await axios.get(msg.message.mediaUrl, { responseType: 'arraybuffer' });
89+
mediaBase64 = Buffer.from(result.data).toString('base64');
90+
}
91+
92+
if (mediaBase64) {
93+
payload.files = [
94+
{
95+
type: 'image',
96+
transfer_method: 'remote_url',
97+
url: mediaBase64,
98+
},
99+
];
100+
}
101+
} else {
102+
payload.files = [
103+
{
104+
type: 'image',
105+
transfer_method: 'remote_url',
106+
url: media[1].split('?')[0],
107+
},
108+
];
109+
}
110+
payload.query = media[2] || content;
90111
}
91112

92113
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
@@ -140,15 +161,35 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
140161

141162
// Handle image messages
142163
if (this.isImageMessage(content)) {
143-
const contentSplit = content.split('|');
144-
payload.files = [
145-
{
146-
type: 'image',
147-
transfer_method: 'remote_url',
148-
url: contentSplit[1].split('?')[0],
149-
},
150-
];
151-
payload.inputs.query = contentSplit[2] || content;
164+
const media = content.split('|');
165+
166+
if (msg.message.mediaUrl || msg.message.base64) {
167+
let mediaBase64 = msg.message.base64 || null;
168+
169+
if (msg.message.mediaUrl && isURL(msg.message.mediaUrl)) {
170+
const result = await axios.get(msg.message.mediaUrl, { responseType: 'arraybuffer' });
171+
mediaBase64 = Buffer.from(result.data).toString('base64');
172+
}
173+
174+
if (mediaBase64) {
175+
payload.files = [
176+
{
177+
type: 'image',
178+
transfer_method: 'remote_url',
179+
url: mediaBase64,
180+
},
181+
];
182+
}
183+
} else {
184+
payload.files = [
185+
{
186+
type: 'image',
187+
transfer_method: 'remote_url',
188+
url: media[1].split('?')[0],
189+
},
190+
];
191+
payload.inputs.query = media[2] || content;
192+
}
152193
}
153194

154195
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
@@ -202,15 +243,26 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
202243

203244
// Handle image messages
204245
if (this.isImageMessage(content)) {
205-
const contentSplit = content.split('|');
206-
payload.files = [
207-
{
208-
type: 'image',
209-
transfer_method: 'remote_url',
210-
url: contentSplit[1].split('?')[0],
211-
},
212-
];
213-
payload.query = contentSplit[2] || content;
246+
const media = content.split('|');
247+
248+
if (msg.message.mediaUrl || msg.message.base64) {
249+
payload.files = [
250+
{
251+
type: 'image',
252+
transfer_method: 'remote_url',
253+
url: msg.message.mediaUrl || msg.message.base64,
254+
},
255+
];
256+
} else {
257+
payload.files = [
258+
{
259+
type: 'image',
260+
transfer_method: 'remote_url',
261+
url: media[1].split('?')[0],
262+
},
263+
];
264+
payload.query = media[2] || content;
265+
}
214266
}
215267

216268
if (instance.integration === Integration.WHATSAPP_BAILEYS) {

src/api/integrations/chatbot/evoai/services/evoai.service.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ConfigService, HttpServer } from '@config/env.config';
55
import { Evoai, EvoaiSetting, IntegrationSession } from '@prisma/client';
66
import axios from 'axios';
77
import { downloadMediaMessage } from 'baileys';
8+
import { isURL } from 'class-validator';
89
import { v4 as uuidv4 } from 'uuid';
910

1011
import { BaseChatbotService } from '../../base-chatbot.service';
@@ -82,23 +83,43 @@ export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
8283

8384
// Handle image message if present
8485
if (this.isImageMessage(content) && msg) {
85-
const contentSplit = content.split('|');
86-
parts[0].text = contentSplit[2] || content;
86+
const media = content.split('|');
87+
parts[0].text = media[2] || content;
8788

8889
try {
89-
// Download the image
90-
const mediaBuffer = await downloadMediaMessage(msg, 'buffer', {});
91-
const fileContent = Buffer.from(mediaBuffer).toString('base64');
92-
const fileName = contentSplit[2] || `${msg.key?.id || 'image'}.jpg`;
93-
94-
parts.push({
95-
type: 'file',
96-
file: {
97-
name: fileName,
98-
mimeType: 'image/jpeg',
99-
bytes: fileContent,
100-
},
101-
} as any);
90+
if (msg.message.mediaUrl || msg.message.base64) {
91+
let mediaBase64 = msg.message.base64 || null;
92+
93+
if (msg.message.mediaUrl && isURL(msg.message.mediaUrl)) {
94+
const result = await axios.get(msg.message.mediaUrl, { responseType: 'arraybuffer' });
95+
mediaBase64 = Buffer.from(result.data).toString('base64');
96+
}
97+
98+
if (mediaBase64) {
99+
parts.push({
100+
type: 'file',
101+
file: {
102+
name: msg.key.id + '.jpeg',
103+
mimeType: 'image/jpeg',
104+
bytes: mediaBase64,
105+
},
106+
} as any);
107+
}
108+
} else {
109+
// Download the image
110+
const mediaBuffer = await downloadMediaMessage(msg, 'buffer', {});
111+
const fileContent = Buffer.from(mediaBuffer).toString('base64');
112+
const fileName = media[2] || `${msg.key?.id || 'image'}.jpg`;
113+
114+
parts.push({
115+
type: 'file',
116+
file: {
117+
name: fileName,
118+
mimeType: 'image/jpeg',
119+
bytes: fileContent,
120+
},
121+
} as any);
122+
}
102123
} catch (fileErr) {
103124
this.logger.error(`[EvoAI] Failed to process image: ${fileErr}`);
104125
}

src/api/integrations/chatbot/evolutionBot/services/evolutionBot.service.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ConfigService, HttpServer } from '@config/env.config';
66
import { EvolutionBot, EvolutionBotSetting, IntegrationSession } from '@prisma/client';
77
import { sendTelemetry } from '@utils/sendTelemetry';
88
import axios from 'axios';
9+
import { isURL } from 'class-validator';
910

1011
import { BaseChatbotService } from '../../base-chatbot.service';
1112
import { OpenaiService } from '../../openai/services/openai.service';
@@ -71,16 +72,26 @@ export class EvolutionBotService extends BaseChatbotService<EvolutionBot, Evolut
7172
}
7273
}
7374

74-
if (this.isImageMessage(content)) {
75-
const contentSplit = content.split('|');
75+
if (this.isImageMessage(content) && msg) {
76+
const media = content.split('|');
77+
78+
if (msg.message.mediaUrl || msg.message.base64) {
79+
payload.files = [
80+
{
81+
type: 'image',
82+
url: msg.message.base64 || msg.message.mediaUrl,
83+
},
84+
];
85+
} else {
86+
payload.files = [
87+
{
88+
type: 'image',
89+
url: media[1].split('?')[0],
90+
},
91+
];
92+
}
7693

77-
payload.files = [
78-
{
79-
type: 'image',
80-
url: contentSplit[1].split('?')[0],
81-
},
82-
];
83-
payload.query = contentSplit[2] || content;
94+
payload.query = media[2] || content;
8495
}
8596

8697
if (instance.integration === Integration.WHATSAPP_BAILEYS) {

src/api/integrations/chatbot/flowise/services/flowise.service.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Integration } from '@api/types/wa.types';
55
import { ConfigService, HttpServer } from '@config/env.config';
66
import { Flowise as FlowiseModel, IntegrationSession } from '@prisma/client';
77
import axios from 'axios';
8+
import { isURL } from 'class-validator';
89

910
import { BaseChatbotService } from '../../base-chatbot.service';
1011
import { OpenaiService } from '../../openai/services/openai.service';
@@ -82,17 +83,28 @@ export class FlowiseService extends BaseChatbotService<FlowiseModel> {
8283
}
8384

8485
if (this.isImageMessage(content)) {
85-
const contentSplit = content.split('|');
86-
87-
payload.uploads = [
88-
{
89-
data: contentSplit[1].split('?')[0],
90-
type: 'url',
91-
name: 'Flowise.png',
92-
mime: 'image/png',
93-
},
94-
];
95-
payload.question = contentSplit[2] || content;
86+
const media = content.split('|');
87+
88+
if (msg.message.mediaUrl || msg.message.base64) {
89+
payload.uploads = [
90+
{
91+
data: msg.message.base64 || msg.message.mediaUrl,
92+
type: 'url',
93+
name: 'Flowise.png',
94+
mime: 'image/png',
95+
},
96+
];
97+
} else {
98+
payload.uploads = [
99+
{
100+
data: media[1].split('?')[0],
101+
type: 'url',
102+
name: 'Flowise.png',
103+
mime: 'image/png',
104+
},
105+
];
106+
payload.question = media[2] || content;
107+
}
96108
}
97109

98110
if (instance.integration === Integration.WHATSAPP_BAILEYS) {

0 commit comments

Comments
 (0)