-
Notifications
You must be signed in to change notification settings - Fork 111
/
index.js
80 lines (69 loc) · 2.35 KB
/
index.js
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
const {
makeWASocket,
useMultiFileAuthState,
DisconnectReason,
fetchLatestBaileysVersion,
Browsers
} = require('@whiskeysockets/baileys');
const fs = require('fs');
const { File } = require('megajs');
const config = process.env; // Access environment variables in Render
const P = require('pino');
const express = require('express');
const app = express();
const port = process.env.PORT || 8000;
const SESSION_ID = config.SESSION_ID;
// Ensure the auth_info_baileys folder exists
const sessionPath = __dirname + '/auth_info_baileys';
if (!fs.existsSync(sessionPath)) {
fs.mkdirSync(sessionPath, { recursive: true });
}
//=================== SESSION-AUTH with MEGA ============================
const credsPath = `${sessionPath}/creds.json`;
if (!fs.existsSync(credsPath)) {
if (!SESSION_ID) return console.log('Please set the SESSION_ID environment variable in Render.');
const file = File.fromURL(`https://mega.nz/file/${SESSION_ID}`);
file.download((err, data) => {
if (err) throw err;
fs.writeFileSync(credsPath, data, () => {
console.log("*Session downloaded from MEGA [🌟]*");
});
});
}
//==============================================
async function connectToWA() {
console.log("Connecting...");
const { state, saveCreds } = await useMultiFileAuthState(sessionPath);
const { version } = await fetchLatestBaileysVersion();
const conn = makeWASocket({
logger: P({ level: 'silent' }),
printQRInTerminal: false,
browser: Browsers.macOS("Firefox"),
syncFullHistory: true,
auth: state,
version
});
conn.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update;
if (connection === 'close') {
if (lastDisconnect.error.output.statusCode !== DisconnectReason.loggedOut) {
connectToWA();
}
} else if (connection === 'open') {
console.log('*Connected*');
// Additional setup if needed
}
});
conn.ev.on('creds.update', saveCreds);
}
// Express server setup for health check or other routes
app.get("/", (req, res) => {
res.send("Bot is running");
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
// Start WhatsApp connection
setTimeout(() => {
connectToWA();
}, 4000);