-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (138 loc) · 4.57 KB
/
index.js
File metadata and controls
162 lines (138 loc) · 4.57 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const path = require("path");
const express = require("express");
const hbs = require("hbs");
const app = express();
const port = process.env.PORT || 3000;
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { Telegraf } = require('telegraf');
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const compression = require('compression');
const morgan = require('morgan');
const validator = require('validator');
// Initialize Firebase
const serviceAccount = require("./software.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://software-hiring-bot-default-rtdb.firebaseio.com/",
});
// Initialize Telegram bot
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
// Setup paths
const staticPath = path.join(__dirname, "./public");
const templatePath = path.join(__dirname, "./templates/views");
const partialsPath = path.join(__dirname, "./templates/partials");
// Middleware
app.use(express.static(staticPath));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(helmet());
app.use(compression());
app.use(morgan('combined'));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
app.use(limiter);
// View engine setup
app.set('view engine', 'hbs');
app.set("views", templatePath);
hbs.registerPartials(partialsPath);
// CORS setup
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Routes
app.get('/', (req, res) => {
res.send("Welcome to our application!");
});
app.get('/form', (req, res) => {
res.render("form");
});
app.post("/form", async (request, response) => {
try {
// Input validation
if (!validator.isEmail(request.body.email)) {
throw new Error('Invalid email');
}
if (!validator.isMobilePhone(request.body.mobile)) {
throw new Error('Invalid mobile number');
}
await insertUserData(request.body);
// Send notification to Telegram
await bot.telegram.sendMessage(process.env.TELEGRAM_CHAT_ID,
`New user registered:\nName: ${request.body.name}\nEmail: ${request.body.email}`
);
response.status(200).send("User data successfully saved");
} catch (error) {
console.error("Error processing form:", error);
response.status(400).send("Error processing form: " + error.message);
}
});
// New route for fetching user data
app.get("/user/:id", async (req, res) => {
try {
const userId = req.params.id;
const userDoc = await admin.firestore().collection("userdata").doc(userId).get();
if (!userDoc.exists) {
res.status(404).send("User not found");
} else {
res.status(200).json(userDoc.data());
}
} catch (error) {
console.error("Error fetching user data:", error);
res.status(500).send("Error fetching user data");
}
});
// New route for updating user data
app.put("/user/:id", async (req, res) => {
try {
const userId = req.params.id;
const updatedData = req.body;
await admin.firestore().collection("userdata").doc(userId).update(updatedData);
res.status(200).send("User data updated successfully");
} catch (error) {
console.error("Error updating user data:", error);
res.status(500).send("Error updating user data");
}
});
// Firebase functions
async function insertUserData(userData) {
try {
const writeResult = await admin
.firestore()
.collection("userdata")
.doc(userData.id)
.set({
id: userData.id,
name: userData.name,
email: userData.email,
mobile: userData.mobile,
checkbox1: userData.checkbox1,
createdAt: admin.firestore.FieldValue.serverTimestamp(),
});
console.log("Document successfully written!");
return writeResult;
} catch (error) {
console.error("Error writing document: ", error);
throw error;
}
}
// Telegram bot commands
bot.command('start', (ctx) => ctx.reply('Welcome to our bot!'));
bot.command('help', (ctx) => ctx.reply('This bot notifies about new user registrations.'));
// Start server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
// Start Telegram bot
bot.launch();
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
// Export the Express API as a Firebase Cloud Function
exports.app = functions.https.onRequest(app);