Skip to content

Commit 1be58c8

Browse files
committed
refactor: improve linkPreview implementation based on PR feedback
- Default linkPreview to true when not specified for backward compatibility - Validate linkPreview is boolean before passing to textMessage - Consolidate debug logs and remove sensitive data from logging - Sanitize API keys in debug output ([REDACTED]) - Reduce log verbosity while maintaining debugging capability - Ensure robust fallback behavior for malformed responses Addresses PR feedback regarding: - Backward compatibility preservation - Security considerations in logging - Input validation and error handling
1 parent ceddace commit 1be58c8

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,49 +106,57 @@ export class EvolutionBotService extends BaseChatbotService<EvolutionBot, Evolut
106106
};
107107
}
108108

109+
// Sanitize payload for logging (remove sensitive data)
110+
const sanitizedPayload = {
111+
...payload,
112+
inputs: {
113+
...payload.inputs,
114+
apiKey: payload.inputs.apiKey ? '[REDACTED]' : undefined,
115+
},
116+
};
117+
109118
this.logger.debug(`[EvolutionBot] Sending request to endpoint: ${endpoint}`);
110-
this.logger.debug(`[EvolutionBot] Payload being sent: ${JSON.stringify(payload, null, 2)}`);
111-
this.logger.debug(`[EvolutionBot] Headers being sent: ${JSON.stringify(headers, null, 2)}`);
119+
this.logger.debug(`[EvolutionBot] Request payload: ${JSON.stringify(sanitizedPayload, null, 2)}`);
112120

113121
const response = await axios.post(endpoint, payload, {
114122
headers,
115123
});
116124

117-
this.logger.debug(`[EvolutionBot] Received response status: ${response.status}`);
118-
this.logger.debug(`[EvolutionBot] Received response data: ${JSON.stringify(response.data, null, 2)}`);
125+
this.logger.debug(`[EvolutionBot] Response received - Status: ${response.status}`);
119126

120127
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
121128
await instance.client.sendPresenceUpdate('paused', remoteJid);
122129
}
123130

124131
let message = response?.data?.message;
125-
const linkPreview = response?.data?.linkPreview; // Extract linkPreview from n8n response
132+
const rawLinkPreview = response?.data?.linkPreview;
133+
134+
// Validate linkPreview is boolean and default to true for backward compatibility
135+
const linkPreview = typeof rawLinkPreview === 'boolean' ? rawLinkPreview : true;
126136

127-
this.logger.debug(`[EvolutionBot] Raw message from response: ${JSON.stringify(message)}`);
128-
this.logger.debug(`[EvolutionBot] LinkPreview setting from response: ${linkPreview}`);
137+
this.logger.debug(
138+
`[EvolutionBot] Processing response - Message length: ${message?.length || 0}, LinkPreview: ${linkPreview}`,
139+
);
129140

130141
if (message && typeof message === 'string' && message.startsWith("'") && message.endsWith("'")) {
131142
const innerContent = message.slice(1, -1);
132143
if (!innerContent.includes("'")) {
133144
message = innerContent;
134-
this.logger.debug(`[EvolutionBot] Message cleaned (removed quotes): ${message}`);
135145
}
136146
}
137147

138148
if (message) {
139-
this.logger.debug(`[EvolutionBot] Sending message to WhatsApp: ${message}`);
140-
this.logger.debug(`[EvolutionBot] Using linkPreview: ${linkPreview}`);
141-
// Send message directly with linkPreview option
149+
// Send message directly with validated linkPreview option
142150
await instance.textMessage(
143151
{
144152
number: remoteJid.split('@')[0],
145153
delay: settings?.delayMessage || 1000,
146154
text: message,
147-
linkPreview: linkPreview, // Use linkPreview from n8n response
155+
linkPreview: linkPreview, // Always boolean, defaults to true
148156
},
149157
false,
150158
);
151-
this.logger.debug(`[EvolutionBot] Message sent successfully to WhatsApp`);
159+
this.logger.debug(`[EvolutionBot] Message sent successfully with linkPreview: ${linkPreview}`);
152160
} else {
153161
this.logger.warn(`[EvolutionBot] No message content received from bot response`);
154162
}

0 commit comments

Comments
 (0)