Skip to content
Open
Changes from 1 commit
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
93 changes: 75 additions & 18 deletions src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@ export class ChatwootService {
messageBody?: any,
sourceId?: string,
quotedMsg?: MessageModel,
referral?: Record<string, string>,
) {
if (sourceId && this.isImportHistoryAvailable()) {
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId], conversationId);
Expand All @@ -1079,17 +1080,24 @@ export class ChatwootService {

const sourceReplyId = quotedMsg?.chatwootMessageId || null;

let contentAttributes: Record<string, unknown> = {};

if (messageBody && instance) {
const replyToIds = await this.getReplyToIds(messageBody, instance);

if (replyToIds.in_reply_to || replyToIds.in_reply_to_external_id) {
const content = JSON.stringify({
...replyToIds,
});
data.append('content_attributes', content);
contentAttributes = { ...contentAttributes, ...replyToIds };
}
}

if (referral) {
contentAttributes = { ...contentAttributes, referral };
}

if (Object.keys(contentAttributes).length > 0) {
data.append('content_attributes', JSON.stringify(contentAttributes));
}

if (sourceReplyId) {
data.append('source_reply_id', sourceReplyId.toString());
}
Expand Down Expand Up @@ -1727,21 +1735,55 @@ export class ChatwootService {
body: string;
thumbnailUrl: string;
sourceUrl: string;
sourceId?: string;
sourceType?: string;
mediaType?: string;
mediaUrl?: string;
}

const externalAdReply =
msg.extendedTextMessage?.contextInfo?.externalAdReply ||
msg.contextInfo?.externalAdReply ||
msg.message?.extendedTextMessage?.contextInfo?.externalAdReply ||
msg.message?.contextInfo?.externalAdReply;

const adsMessage: AdsMessage | undefined = {
title: msg.extendedTextMessage?.contextInfo?.externalAdReply?.title || msg.contextInfo?.externalAdReply?.title,
body: msg.extendedTextMessage?.contextInfo?.externalAdReply?.body || msg.contextInfo?.externalAdReply?.body,
thumbnailUrl:
msg.extendedTextMessage?.contextInfo?.externalAdReply?.thumbnailUrl ||
msg.contextInfo?.externalAdReply?.thumbnailUrl,
sourceUrl:
msg.extendedTextMessage?.contextInfo?.externalAdReply?.sourceUrl || msg.contextInfo?.externalAdReply?.sourceUrl,
title: externalAdReply?.title,
body: externalAdReply?.body,
thumbnailUrl: externalAdReply?.thumbnailUrl,
sourceUrl: externalAdReply?.sourceUrl,
sourceId: externalAdReply?.sourceId,
sourceType: externalAdReply?.sourceType,
mediaType: externalAdReply?.mediaType,
mediaUrl: externalAdReply?.mediaUrl,
};

return adsMessage;
}

private buildReferralAttributes(adsMessage: {
title?: string;
body?: string;
thumbnailUrl?: string;
sourceUrl?: string;
sourceId?: string;
sourceType?: string;
mediaType?: string;
mediaUrl?: string;
}) {
const referral: Record<string, string> = {};

if (adsMessage.sourceId) referral.source_id = adsMessage.sourceId;
if (adsMessage.sourceType) referral.source_type = adsMessage.sourceType;
if (adsMessage.sourceUrl) referral.source_url = adsMessage.sourceUrl;
if (adsMessage.title) referral.headline = adsMessage.title;
if (adsMessage.body) referral.body = adsMessage.body;
if (adsMessage.mediaType) referral.media_type = adsMessage.mediaType;
if (adsMessage.thumbnailUrl) referral.image_url = adsMessage.thumbnailUrl;

return Object.keys(referral).length > 0 ? referral : undefined;
}

private getReactionMessage(msg: any) {
interface ReactionMessage {
key: {
Expand Down Expand Up @@ -2212,7 +2254,13 @@ export class ChatwootService {

const isAdsMessage = (adsMessage && adsMessage.title) || adsMessage.body || adsMessage.thumbnailUrl;
if (isAdsMessage) {
const imgBuffer = await axios.get(adsMessage.thumbnailUrl, { responseType: 'arraybuffer' });
let imgBuffer;
try {
imgBuffer = await axios.get(adsMessage.thumbnailUrl, { responseType: 'arraybuffer' });
} catch (error) {
this.logger.warn(`Failed to download ads thumbnail: ${error?.message || error}`);
return;
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Outdated
}

const extension = mimeTypes.extension(imgBuffer.headers['content-type']);
const mimeType = extension && mimeTypes.lookup(extension);
Expand All @@ -2226,12 +2274,19 @@ export class ChatwootService {
const nameFile = `${random}.${mimeTypes.extension(mimeType)}`;
const fileData = Buffer.from(imgBuffer.data, 'binary');

const img = await Jimp.read(fileData);
await img.cover({
w: 320,
h: 180,
});
const processedBuffer = await img.getBuffer(JimpMime.png);
let processedBuffer: Buffer = fileData;
try {
const img = await Jimp.read(fileData);
await img.cover({
w: 320,
h: 180,
});
processedBuffer = await img.getBuffer(JimpMime.png);
} catch (error) {
this.logger.warn(
`Failed to process ads thumbnail with Jimp, sending raw image: ${error?.message || error}`,
);
}

const fileStream = new Readable();
fileStream._read = () => {}; // _read is required but you can noop it
Expand All @@ -2256,6 +2311,8 @@ export class ChatwootService {
instance,
body,
'WAID:' + body.key.id,
quotedMsg,
this.buildReferralAttributes(adsMessage),
);

if (!send) {
Expand Down