Skip to content

Commit 34769e2

Browse files
committed
fix: receive medias on chatwoot
1 parent 5401ecd commit 34769e2

File tree

2 files changed

+68
-58
lines changed

2 files changed

+68
-58
lines changed

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

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,11 @@ export class ChatwootService {
933933
) {
934934
if (sourceId && this.isImportHistoryAvailable()) {
935935
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId]);
936-
if (messageAlreadySaved.size > 0) {
937-
this.logger.warn('Message already saved on chatwoot');
938-
return null;
936+
if (messageAlreadySaved) {
937+
if (messageAlreadySaved.size > 0) {
938+
this.logger.warn('Message already saved on chatwoot');
939+
return null;
940+
}
939941
}
940942
}
941943
const data = new FormData();
@@ -2442,57 +2444,61 @@ export class ChatwootService {
24422444
chatwootConfig: ChatwootDto,
24432445
prepareMessage: (message: any) => any,
24442446
) {
2445-
if (!this.isImportHistoryAvailable()) {
2446-
return;
2447-
}
2448-
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
2449-
return;
2450-
}
2447+
try {
2448+
if (!this.isImportHistoryAvailable()) {
2449+
return;
2450+
}
2451+
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
2452+
return;
2453+
}
24512454

2452-
const inbox = await this.getInbox(instance);
2455+
const inbox = await this.getInbox(instance);
24532456

2454-
const sqlMessages = `select * from messages m
2455-
where account_id = ${chatwootConfig.accountId}
2456-
and inbox_id = ${inbox.id}
2457-
and created_at >= now() - interval '6h'
2458-
order by created_at desc`;
2457+
const sqlMessages = `select * from messages m
2458+
where account_id = ${chatwootConfig.accountId}
2459+
and inbox_id = ${inbox.id}
2460+
and created_at >= now() - interval '6h'
2461+
order by created_at desc`;
24592462

2460-
const messagesData = (await this.pgClient.query(sqlMessages))?.rows;
2461-
const ids: string[] = messagesData
2462-
.filter((message) => !!message.source_id)
2463-
.map((message) => message.source_id.replace('WAID:', ''));
2463+
const messagesData = (await this.pgClient.query(sqlMessages))?.rows;
2464+
const ids: string[] = messagesData
2465+
.filter((message) => !!message.source_id)
2466+
.map((message) => message.source_id.replace('WAID:', ''));
24642467

2465-
const savedMessages = await this.prismaRepository.message.findMany({
2466-
where: {
2467-
Instance: { name: instance.instanceName },
2468-
messageTimestamp: { gte: dayjs().subtract(6, 'hours').unix() },
2469-
AND: ids.map((id) => ({ key: { path: ['id'], not: id } })),
2470-
},
2471-
});
2468+
const savedMessages = await this.prismaRepository.message.findMany({
2469+
where: {
2470+
Instance: { name: instance.instanceName },
2471+
messageTimestamp: { gte: dayjs().subtract(6, 'hours').unix() },
2472+
AND: ids.map((id) => ({ key: { path: ['id'], not: id } })),
2473+
},
2474+
});
24722475

2473-
const filteredMessages = savedMessages.filter(
2474-
(msg: any) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid),
2475-
);
2476-
const messagesRaw: any[] = [];
2477-
for (const m of filteredMessages) {
2478-
if (!m.message || !m.key || !m.messageTimestamp) {
2479-
continue;
2480-
}
2476+
const filteredMessages = savedMessages.filter(
2477+
(msg: any) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid),
2478+
);
2479+
const messagesRaw: any[] = [];
2480+
for (const m of filteredMessages) {
2481+
if (!m.message || !m.key || !m.messageTimestamp) {
2482+
continue;
2483+
}
24812484

2482-
if (Long.isLong(m?.messageTimestamp)) {
2483-
m.messageTimestamp = m.messageTimestamp?.toNumber();
2484-
}
2485+
if (Long.isLong(m?.messageTimestamp)) {
2486+
m.messageTimestamp = m.messageTimestamp?.toNumber();
2487+
}
24852488

2486-
messagesRaw.push(prepareMessage(m as any));
2487-
}
2489+
messagesRaw.push(prepareMessage(m as any));
2490+
}
24882491

2489-
this.addHistoryMessages(
2490-
instance,
2491-
messagesRaw.filter((msg) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid)),
2492-
);
2492+
this.addHistoryMessages(
2493+
instance,
2494+
messagesRaw.filter((msg) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid)),
2495+
);
24932496

2494-
await chatwootImport.importHistoryMessages(instance, this, inbox, this.provider);
2495-
const waInstance = this.waMonitor.waInstances[instance.instanceName];
2496-
waInstance.clearCacheChatwoot();
2497+
await chatwootImport.importHistoryMessages(instance, this, inbox, this.provider);
2498+
const waInstance = this.waMonitor.waInstances[instance.instanceName];
2499+
waInstance.clearCacheChatwoot();
2500+
} catch (error) {
2501+
return;
2502+
}
24972503
}
24982504
}

src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,26 @@ class ChatwootImport {
170170
}
171171

172172
public async getExistingSourceIds(sourceIds: string[]): Promise<Set<string>> {
173-
const existingSourceIdsSet = new Set<string>();
173+
try {
174+
const existingSourceIdsSet = new Set<string>();
174175

175-
if (sourceIds.length === 0) {
176-
return existingSourceIdsSet;
177-
}
176+
if (sourceIds.length === 0) {
177+
return existingSourceIdsSet;
178+
}
178179

179-
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`); // Make sure the sourceId is always formatted as WAID:1234567890
180-
const query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
181-
const pgClient = postgresClient.getChatwootConnection();
182-
const result = await pgClient.query(query, [formattedSourceIds]);
180+
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`); // Make sure the sourceId is always formatted as WAID:1234567890
181+
const query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
182+
const pgClient = postgresClient.getChatwootConnection();
183+
const result = await pgClient.query(query, [formattedSourceIds]);
183184

184-
for (const row of result.rows) {
185-
existingSourceIdsSet.add(row.source_id);
186-
}
185+
for (const row of result.rows) {
186+
existingSourceIdsSet.add(row.source_id);
187+
}
187188

188-
return existingSourceIdsSet;
189+
return existingSourceIdsSet;
190+
} catch (error) {
191+
return null;
192+
}
189193
}
190194

191195
public async importHistoryMessages(

0 commit comments

Comments
 (0)