File size: 2,463 Bytes
33564cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
const express = require('express');
const cors = require('cors');
const { db } = require('./src/firebase');
const axios = require('axios'); // Import axios
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 7860;
app.use(cors());
app.use(express.json());
// --- HELPER: Telegram Notification ---
const sendTelegramAlert = async (data) => {
const token = process.env.TELEGRAM_BOT_TOKEN;
const chatId = process.env.TELEGRAM_CHAT_ID;
if (!token || !chatId) {
console.warn("β οΈ Telegram credentials missing in .env");
return;
}
// Format the message nicely
const message = `
π *New Lead: House of Ruqa*
π€ *Name:* ${data.name}
π§ *Email:* ${data.email}
π *Phone:* ${data.phone}
π¦ *Package:* ${data.packageType}
π *Message:*
${data.message}
`;
try {
const url = `https://api.telegram.org/bot${token}/sendMessage`;
await axios.post(url, {
chat_id: chatId,
text: message,
parse_mode: 'Markdown' // Allows bolding
});
console.log("βοΈ Telegram Notification Sent");
} catch (error) {
console.error("β Telegram Error:", error.response ? error.response.data : error.message);
}
};
// --- ROUTES ---
app.get('/health', (req, res) => {
res.status(200).json({ status: 'Online', system: 'House of Ruqa Backend' });
});
app.post('/api/lead', async (req, res) => {
try {
const { name, email, phone, message, packageType } = req.body;
if (!name || !email) {
return res.status(400).json({ error: 'Name and Email are required.' });
}
const leadData = {
name,
email,
phone: phone || 'N/A',
message: message || '',
packageType: packageType || 'General Inquiry',
timestamp: new Date().toISOString(),
status: 'new',
source: 'website_form'
};
// 1. Save to Firebase
const docRef = await db.collection('leads').add(leadData);
console.log(`β
New Lead Saved: ${docRef.id}`);
// 2. Send Telegram Alert (Async - don't wait for it to respond to user)
sendTelegramAlert(leadData);
res.status(201).json({
success: true,
message: 'Request received.',
id: docRef.id
});
} catch (error) {
console.error("β Error processing lead:", error);
res.status(500).json({ success: false, error: 'Internal Server Error' });
}
});
app.listen(PORT, () => {
console.log(`π House of Ruqa Server running on port ${PORT}`);
}); |