-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (154 loc) · 5.47 KB
/
index.js
File metadata and controls
188 lines (154 loc) · 5.47 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const express = require("express");
const app = express();
const path = require("path");
const fs = require("fs");
const { Client, MessageMedia, LegacySessionAuth, LocalAuth } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
// Create the "downloaded" folder if it doesn't exist
const downloadedFolder = path.join(__dirname, "downloaded");
if (!fs.existsSync(downloadedFolder)) {
fs.mkdirSync(downloadedFolder);
console.log(`Created ${downloadedFolder} directory \n`);
}
// WhatsApp Bot Setup
const bot = new Client();
// const SESSION_FILE_PATH = './session.json';
// // Load the session data if it has been previously saved
// let sessionData;
// if(fs.existsSync(SESSION_FILE_PATH)) {
// sessionData = require(SESSION_FILE_PATH);
// }
// // Use the saved values
// const bot = new Client({
// session: sessionData,
// authOptions: {
// clientOptions: {
// sessionId: 'session',
// authTimeout: 60000,
// },
// auth: LocalAuth,
// },
// });
// // Save session values to the file upon successful auth
// bot.on('authenticated', (session) => {
// sessionData = session;
// fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) => {
// if (err) {
// console.error(err);
// }
// });
// });
bot.on("qr", (qr) => {
qrcode.generate(qr, { small: true }); // Display QR code in the console
});
bot.on("ready", () => {
console.log("WhatsApp Bot is ready! \n");
});
// bot.on("message_create", async (message) => {
// if (message.hasMedia) {
// const media = await message.downloadMedia();
// // Save the media file to the "downloaded" folder
// const filePath = path.join(downloadedFolder, `${Date.now()}.${media.mimetype.split("/")[1]}`);
// fs.writeFileSync(filePath, media.data);
// console.log(`Media saved to ${filePath}`);
// }
// });
const recreateFolder = () => {
fs.rmdirSync(downloadedFolder, { recursive: true });
fs.mkdirSync(downloadedFolder);
console.log("Deleted and recreated the 'downloaded' folder \n" );
};
// Schedule folder recreation every 10 minutes (600000 milliseconds)
setInterval(recreateFolder, 3600000);
app.use("/downloaded", express.static(downloadedFolder));
bot.on("message_create", async (message) => {
if (message.hasMedia) {
const media = await message.downloadMedia();
if (media && media.mimetype && media.mimetype.startsWith("image/")) {
const contact = await message.getContact();
const name = contact.pushname;
const contactn = contact.number;
// Convert the media data to a Buffer
const buff = Buffer.from(media.data, 'base64');
// Save the Buffer to a file with .jpg extension
const filePath = path.join(downloadedFolder, `${contactn}_${name}_${Date.now()}.jpg`);
fs.writeFileSync(filePath, buff);
console.log(`Media saved to ${filePath} [${new Date().toLocaleTimeString()}] \n`);
} else {
return console.log(media.mimetype);
}
}
});
app.get("/gallery", (req, res) => {
// Read all files in the "downloaded" folder
fs.readdir(downloadedFolder, (err, files) => {
if (err) {
console.error(err);
res.status(500).send("An error occurred. \n");
} else {
// Filter JPEG files
const jpegFiles = files.filter((file) => path.extname(file) === ".jpg");
// Render the "gallery" EJS template and pass the JPEG file names as data
res.render("gallery", { files: jpegFiles });
}
});
});
const { performance } = require('perf_hooks');
function getCircleColor(ping) {
if (ping <= 50) {
return 'green';
} else if (ping <= 100) {
return 'yellow';
} else {
return 'red';
}
}
function measurePing() {
const startTime = performance.now();
// Perform an operation that takes some time
const endTime = performance.now();
const ping = endTime - startTime;
return ping;
}
app.get('/ping', (req, res) => {
res.json({ timestamp: Date.now() });
});
app.get('/pings', (req, res) => {
const ping = measurePing();
console.log(ping)
// Send the ping duration to the EJS template
res.render('status', { ping: measurePing, getCircleColor: getCircleColor });
});
app.get('/remove-image/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(downloadedFolder, filename);
// Check if the file exists
if (fs.existsSync(filePath)) {
// Remove the file
fs.unlinkSync(filePath);
console.log(`File ${filename} has been removed. \n`);
res.redirect('/gallery'); // Redirect to the gallery page
} else {
console.log(`File ${filename} does not exist. \n`);
res.redirect('/gallery'); // Redirect to the gallery page
}
});
// Express route to serve the downloaded media file
// app.get("/media/:fileName", (req, res) => {
// const fileName = req.params.fileName;
// const filePath = path.join(downloadedFolder, fileName);
// if (fs.existsSync(filePath)) {
// res.sendFile(filePath);
// } else {
// res.status(404).send("File not found");
// }
// });
// Start the WhatsApp bot
bot.initialize();
app.listen(7000, () => {
console.log("Server listening on port 3000");
});