Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 80 additions & 25 deletions src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class ChatwootService {

private provider: any;

// Cache para deduplicação de orderMessage (evita mensagens duplicadas)
private processedOrderIds: Map<string, number> = new Map();
private readonly ORDER_CACHE_TTL_MS = 30000; // 30 segundos

constructor(
private readonly waMonitor: WAMonitoringService,
private readonly configService: ConfigService,
Expand Down Expand Up @@ -1758,41 +1762,92 @@ export class ChatwootService {
}

private getTypeMessage(msg: any) {
const types = {
conversation: msg.conversation,
imageMessage: msg.imageMessage?.caption,
videoMessage: msg.videoMessage?.caption,
extendedTextMessage: msg.extendedTextMessage?.text,
messageContextInfo: msg.messageContextInfo?.stanzaId,
stickerMessage: undefined,
documentMessage: msg.documentMessage?.caption,
documentWithCaptionMessage: msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
audioMessage: msg.audioMessage ? (msg.audioMessage.caption ?? '') : undefined,
contactMessage: msg.contactMessage?.vcard,
contactsArrayMessage: msg.contactsArrayMessage,
locationMessage: msg.locationMessage,
liveLocationMessage: msg.liveLocationMessage,
listMessage: msg.listMessage,
listResponseMessage: msg.listResponseMessage,
viewOnceMessageV2:
msg?.message?.viewOnceMessageV2?.message?.imageMessage?.url ||
msg?.message?.viewOnceMessageV2?.message?.videoMessage?.url ||
msg?.message?.viewOnceMessageV2?.message?.audioMessage?.url,
};

return types;
}
const types = {
conversation: msg.conversation,
imageMessage: msg.imageMessage?.caption,
videoMessage: msg.videoMessage?.caption,
extendedTextMessage: msg.extendedTextMessage?.text,
messageContextInfo: msg.messageContextInfo?.stanzaId,
stickerMessage: undefined,
documentMessage: msg.documentMessage?.caption,
documentWithCaptionMessage: msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
audioMessage: msg.audioMessage ? (msg.audioMessage.caption ?? '') : undefined,
contactMessage: msg.contactMessage?.vcard,
contactsArrayMessage: msg.contactsArrayMessage,
locationMessage: msg.locationMessage,
liveLocationMessage: msg.liveLocationMessage,
listMessage: msg.listMessage,
listResponseMessage: msg.listResponseMessage,
orderMessage: msg.orderMessage,
viewOnceMessageV2:
msg?.message?.viewOnceMessageV2?.message?.imageMessage?.url ||
msg?.message?.viewOnceMessageV2?.message?.videoMessage?.url ||
msg?.message?.viewOnceMessageV2?.message?.audioMessage?.url,
};

return types;
}

private getMessageContent(types: any) {
const typeKey = Object.keys(types).find((key) => types[key] !== undefined);

let result = typeKey ? types[typeKey] : undefined;

// Remove externalAdReplyBody| in Chatwoot (Already Have)
// Remove externalAdReplyBody| in Chatwoot
if (result && typeof result === 'string' && result.includes('externalAdReplyBody|')) {
result = result.split('externalAdReplyBody|').filter(Boolean).join('');
}

// Tratamento de Pedidos do Catálogo (WhatsApp Business Catalog)
if (typeKey === 'orderMessage' && result.orderId) {
const now = Date.now();
// Limpa entradas antigas do cache
this.processedOrderIds.forEach((timestamp, id) => {
if (now - timestamp > this.ORDER_CACHE_TTL_MS) {
this.processedOrderIds.delete(id);
}
});
// Verifica se já processou este orderId
if (this.processedOrderIds.has(result.orderId)) {
return undefined; // Ignora duplicado
}
this.processedOrderIds.set(result.orderId, now);
}
if (typeKey === 'orderMessage') {
// Extrai o valor - pode ser Long, objeto {low, high}, ou número direto
let rawPrice = 0;
const amount = result.totalAmount1000;

if (Long.isLong(amount)) {
rawPrice = amount.toNumber();
} else if (amount && typeof amount === 'object' && 'low' in amount) {
// Formato {low: number, high: number, unsigned: boolean}
rawPrice = Long.fromValue(amount).toNumber();
} else if (typeof amount === 'number') {
rawPrice = amount;
}

const price = (rawPrice / 1000).toLocaleString('pt-BR', {
style: 'currency',
currency: result.totalCurrencyCode || 'BRL',
});

const itemCount = result.itemCount || 1;
const orderTitle = result.orderTitle || 'Produto do catálogo';
const orderId = result.orderId || 'N/A';

return (
`🛒 *NOVO PEDIDO NO CATÁLOGO*\n` +
`━━━━━━━━━━━━━━━━━━━━━\n` +
`📦 *Produto:* ${orderTitle}\n` +
`📊 *Quantidade:* ${itemCount}\n` +
`💰 *Total:* ${price}\n` +
`🆔 *Pedido:* #${orderId}\n` +
`━━━━━━━━━━━━━━━━━━━━━\n` +
`_Responda para atender este pedido!_`
);
}

if (typeKey === 'locationMessage' || typeKey === 'liveLocationMessage') {
const latitude = result.degreesLatitude;
const longitude = result.degreesLongitude;
Expand Down