mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-11 02:49:36 -06:00
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
This commit is contained in:
parent
ceddace915
commit
1be58c8487
@ -106,49 +106,57 @@ export class EvolutionBotService extends BaseChatbotService<EvolutionBot, Evolut
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sanitize payload for logging (remove sensitive data)
|
||||||
|
const sanitizedPayload = {
|
||||||
|
...payload,
|
||||||
|
inputs: {
|
||||||
|
...payload.inputs,
|
||||||
|
apiKey: payload.inputs.apiKey ? '[REDACTED]' : undefined,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
this.logger.debug(`[EvolutionBot] Sending request to endpoint: ${endpoint}`);
|
this.logger.debug(`[EvolutionBot] Sending request to endpoint: ${endpoint}`);
|
||||||
this.logger.debug(`[EvolutionBot] Payload being sent: ${JSON.stringify(payload, null, 2)}`);
|
this.logger.debug(`[EvolutionBot] Request payload: ${JSON.stringify(sanitizedPayload, null, 2)}`);
|
||||||
this.logger.debug(`[EvolutionBot] Headers being sent: ${JSON.stringify(headers, null, 2)}`);
|
|
||||||
|
|
||||||
const response = await axios.post(endpoint, payload, {
|
const response = await axios.post(endpoint, payload, {
|
||||||
headers,
|
headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.logger.debug(`[EvolutionBot] Received response status: ${response.status}`);
|
this.logger.debug(`[EvolutionBot] Response received - Status: ${response.status}`);
|
||||||
this.logger.debug(`[EvolutionBot] Received response data: ${JSON.stringify(response.data, null, 2)}`);
|
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
}
|
}
|
||||||
|
|
||||||
let message = response?.data?.message;
|
let message = response?.data?.message;
|
||||||
const linkPreview = response?.data?.linkPreview; // Extract linkPreview from n8n response
|
const rawLinkPreview = response?.data?.linkPreview;
|
||||||
|
|
||||||
|
// Validate linkPreview is boolean and default to true for backward compatibility
|
||||||
|
const linkPreview = typeof rawLinkPreview === 'boolean' ? rawLinkPreview : true;
|
||||||
|
|
||||||
this.logger.debug(`[EvolutionBot] Raw message from response: ${JSON.stringify(message)}`);
|
this.logger.debug(
|
||||||
this.logger.debug(`[EvolutionBot] LinkPreview setting from response: ${linkPreview}`);
|
`[EvolutionBot] Processing response - Message length: ${message?.length || 0}, LinkPreview: ${linkPreview}`,
|
||||||
|
);
|
||||||
|
|
||||||
if (message && typeof message === 'string' && message.startsWith("'") && message.endsWith("'")) {
|
if (message && typeof message === 'string' && message.startsWith("'") && message.endsWith("'")) {
|
||||||
const innerContent = message.slice(1, -1);
|
const innerContent = message.slice(1, -1);
|
||||||
if (!innerContent.includes("'")) {
|
if (!innerContent.includes("'")) {
|
||||||
message = innerContent;
|
message = innerContent;
|
||||||
this.logger.debug(`[EvolutionBot] Message cleaned (removed quotes): ${message}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
this.logger.debug(`[EvolutionBot] Sending message to WhatsApp: ${message}`);
|
// Send message directly with validated linkPreview option
|
||||||
this.logger.debug(`[EvolutionBot] Using linkPreview: ${linkPreview}`);
|
|
||||||
// Send message directly with linkPreview option
|
|
||||||
await instance.textMessage(
|
await instance.textMessage(
|
||||||
{
|
{
|
||||||
number: remoteJid.split('@')[0],
|
number: remoteJid.split('@')[0],
|
||||||
delay: settings?.delayMessage || 1000,
|
delay: settings?.delayMessage || 1000,
|
||||||
text: message,
|
text: message,
|
||||||
linkPreview: linkPreview, // Use linkPreview from n8n response
|
linkPreview: linkPreview, // Always boolean, defaults to true
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
this.logger.debug(`[EvolutionBot] Message sent successfully to WhatsApp`);
|
this.logger.debug(`[EvolutionBot] Message sent successfully with linkPreview: ${linkPreview}`);
|
||||||
} else {
|
} else {
|
||||||
this.logger.warn(`[EvolutionBot] No message content received from bot response`);
|
this.logger.warn(`[EvolutionBot] No message content received from bot response`);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user