Enhance spam detection logic in contact form API
- Improved spam detection by integrating Gemini AI and heuristic checks, allowing for more accurate identification of spam messages. - Updated logging to specify the source of spam detection (Gemini or heuristic) for better debugging and monitoring. - Maintained existing functionality while enhancing the overall user experience in the contact form submission process.
This commit is contained in:
@@ -211,16 +211,20 @@ export const POST: APIRoute = async ({ request, clientAddress }) => {
|
|||||||
errors.disclaimer = 'Please check the required consent box before submitting';
|
errors.disclaimer = 'Please check the required consent box before submitting';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for spam
|
// Gemini AI spam detection FIRST
|
||||||
if (isSpam(message, name, email)) {
|
let spamDetected = false;
|
||||||
errors.spam = 'Your message was flagged as potential spam. Please revise your message and try again.';
|
let spamReason = '';
|
||||||
}
|
|
||||||
|
|
||||||
// Gemini AI spam detection
|
|
||||||
if (await isSpamWithGemini(message)) {
|
if (await isSpamWithGemini(message)) {
|
||||||
|
spamDetected = true;
|
||||||
|
spamReason = 'Gemini';
|
||||||
|
} else if (isSpam(message, name, email)) {
|
||||||
|
spamDetected = true;
|
||||||
|
spamReason = 'heuristic';
|
||||||
|
}
|
||||||
|
if (spamDetected) {
|
||||||
const token = jwt.sign({ email, message }, MANUAL_REVIEW_SECRET, { expiresIn: '1h' });
|
const token = jwt.sign({ email, message }, MANUAL_REVIEW_SECRET, { expiresIn: '1h' });
|
||||||
console.warn(
|
console.warn(
|
||||||
`[SPAM DETECTED by Gemini]`,
|
`[SPAM DETECTED by ${spamReason === 'Gemini' ? 'Gemini' : 'heuristic'}]`,
|
||||||
{ name, email, message, ip: request.headers.get('x-forwarded-for') }
|
{ name, email, message, ip: request.headers.get('x-forwarded-for') }
|
||||||
);
|
);
|
||||||
return new Response(
|
return new Response(
|
||||||
|
Reference in New Issue
Block a user