50 lines
1.6 KiB
JavaScript

import { getLocation } from '../utils/geolocation.js';
import { addToNotionDatabase, getNotionDatabaseWithCache } from '../utils/notion.js';
const TURNSTILE_SECRET_KEY = process.env.TURNSTILE_AUTH;
const TURNSTILE_ENABLED = true;
export async function handleContactRequest(request) {
const { email, token } = await request.json();
if (!email || !token) {
return new Response('Email and token are required', { status: 400 });
}
// Extract the IP address from the request headers
const ip = request.headers.get('CF-Connecting-IP');
// Get location info
const location = await getLocation(ip);
const locationString = location.city + ' - ' + location.country;
if (TURNSTILE_ENABLED) {
// Verify the Turnstile token
const verificationResponse = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
secret: TURNSTILE_SECRET_KEY,
response: token,
}),
});
const verificationData = await verificationResponse.json();
if (!verificationData.success) {
return new Response('Turnstile verification failed', { status: 400 });
}
}
// Add the email and location to Notion
try {
await addToNotionDatabase({ Name: email.split('@')[0], Email: email, Location: locationString }, '1abdd26d60b68076a886fb0525ff0a4f');
return new Response(JSON.stringify({ success: true, message: 'Email processed and added to Notion' }), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response('Error processing email', { status: 500 });
}
}