-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
254 lines (235 loc) · 7.96 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// * To downlod the youtube video from the URL
const { MongoClient, ServerApiVersion } = require("mongodb");
const dotenv = require("dotenv");
dotenv.config();
const hashmap = {};
// *!whatsbot
const makeWASocket = require("@whiskeysockets/baileys").default;
const {
DisconnectReason,
useMultiFileAuthState,
MessageType,
downloadMediaMessage,
} = require("@whiskeysockets/baileys");
const store = {};
const getMessage = (key) => {
const { id } = key;
if (store[id]) return store[id].message;
};
async function WABot() {
const { state, saveCreds } = await useMultiFileAuthState("auth");
const sock = makeWASocket({
printQRInTerminal: true,
auth: state,
getMessage,
});
const getText = (message) => {
try {
return (
message.conversation ||
message.extendedTextMessage.text ||
message.imageMessage.caption
);
} catch {
return "";
}
};
const sendMessage = async (jid, content) => {
try {
const sent = await sock.sendMessage(jid, content);
store[sent.key.id] = sent;
} catch (err) {
console.error("Error sending message: ", err);
}
};
// Replace the placeholder with your Atlas connection string
const uri = process.env.MONGODB_URI;
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
const handleJobOrDashboard = async (remoteJid, reply) => {
await sendMessage(remoteJid, { text: reply });
};
async function run() {
try {
await client.connect();
let temppreviousJob = "",
temppreviousDb = "",
previousJob = "",
previousDb = "";
const db = client.db("learning_mongodb");
const collection = db.collection("jobs");
const secondLastDocument = await collection
.find({})
.sort({ _id: -1 })
.skip(1)
.limit(1)
.toArray();
const pld = secondLastDocument[0];
// console.log(pld);
temppreviousJob = pld.previousjob;
temppreviousDb = pld.previousdata;
previousJob = temppreviousJob
.replace(/<b>/g, "*")
.replace(/<\/b>/g, "*")
.replace(/<i>/g, "_")
.replace(/<\/i>/g, "_")
.replace(/\n\n\n/g, "")
.replace(/\n\n/g, "\n");
previousDb = temppreviousDb
.replace(/<b>/g, "*")
.replace(/<\/b>/g, "*")
.replace(/<i>/g, "_")
.replace(/<\/i>/g, "_")
.replace(/\n\n\n/g, "")
.replace(/\n\n/g, "\n");
// console.log(previousJob);
// console.log(previousDb);
const msg = process.env.GROUPID;
while (true) {
let tempcurrentJob = "",
tempcurrentDb = "",
currentJob = "",
currentDb = "";
const lastDocument = await collection
.find({})
.sort({ _id: -1 })
.limit(1)
.toArray();
// console.log("Last Document:", lastDocument[0]);
const ld = lastDocument[0];
// console.log(ld);
tempcurrentJob = ld.previousjob;
tempcurrentDb = ld.previousdata;
currentJob = tempcurrentJob
.replace(/<b>/g, "*")
.replace(/<\/b>/g, "*")
.replace(/<i>/g, "_")
.replace(/<\/i>/g, "_")
.replace(/\n\n\n/g, "")
.replace(/\n\n/g, "\n");
currentDb = tempcurrentDb
.replace(/<b>/g, "*")
.replace(/<\/b>/g, "*")
.replace(/<i>/g, "_")
.replace(/<\/i>/g, "_")
.replace(/\n\n\n/g, "")
.replace(/\n\n/g, "\n");
// console.log("Current Job:", currentJob);
// console.log("Current Data:", currentDb);
if (hashmap[currentJob] !== 1 && currentJob !== previousJob) {
await handleJobOrDashboard(
msg,
"💡 *New Job Posted* 💡\n---------------------------------------\n" +
currentJob
);
previousJob = currentJob;
hashmap[currentJob] = 1;
const searchSentence = "Deadline to Apply :";
const index = currentJob.indexOf(searchSentence);
// console.log(index);
const newStr = currentJob.substring(index);
// console.log(newStr);
const indexn = newStr.indexOf("\n");
// console.log(indexn);
const DATE = newStr.substring(0, indexn);
// console.log(DATE);
const idx = DATE.indexOf("_");
const lidx = DATE.lastIndexOf("_");
const dt = DATE.substring(idx + 1, lidx);
// console.log(dt);
let [day, month, year, hour, minute] = dt.split(/[-: ]/);
// console.log(day);
const deadline = new Date(
`${year}-${month}-${day} ${hour}:${minute}`
);
const notificationTime = new Date(deadline.getTime() - 540 * 60000); // 60 minutes before deadline
const currentTime = new Date();
const delay = notificationTime.getTime() - currentTime.getTime();
const options = { timeZone: "Asia/Kolkata" };
const currentTimeIST = currentTime.toLocaleString("en-US", options);
const notificationTimeIST = notificationTime.toLocaleString(
"en-US",
options
);
console.log("Notification Time " + notificationTime);
console.log("Current Time " + currentTime);
console.log("Notification Time(IST): " + notificationTimeIST);
console.log("Current Time (IST): " + currentTimeIST);
console.log("Delay In Seconds " + delay);
// Testing
// const notificationTime = new Date(deadline.getTime() - 60 * 60000); // 60 minutes before deadline
// const currentTime = new Date(notificationTime.getTime() - 2 * 60000); // 1 minute before notificationTime
// let delay = notificationTime.getTime() - currentTime.getTime();
// delay /= 10;
// console.log(delay);
const checkNotificationTime = () => {
const now = new Date();
// console.log(now);
// When you are testing go for <=
if (now >= notificationTime) {
const attentionReply =
"🔴 *Attention Deadline Is Approaching*\n------------------------------------------------------------\n" +
currentJob;
// console.log(attentionReply);
sendMessage(msg, { text: attentionReply });
} else {
setTimeout(checkNotificationTime, 1000);
const ch = "Delay left " + delay + "\n";
console.log(ch);
}
};
setTimeout(checkNotificationTime, delay);
}
if (currentDb !== previousDb) {
await handleJobOrDashboard(
msg,
"🚀 *RM Dashboard* 🔮\n------------------------------------\n" +
currentDb
);
previousDb = currentDb;
}
// Delay between iterations (e.g., 5 seconds)
await new Promise((resolve) => setTimeout(resolve, 5000));
}
} catch (err) {
console.error("Error:", err);
} finally {
await client.close();
}
}
run().catch(console.dir);
sock.ev.process(async (events) => {
if (events["connection.update"]) {
const { connection, lastDisconnect } = events["connection.update"];
if (connection === "close") {
if (
lastDisconnect?.error?.output?.statusCode !==
DisconnectReason.loggedOut
) {
WABot();
} else {
console.log("Disconnected because you logged out");
}
}
}
if (events["creds.update"]) {
await saveCreds();
}
if (events["messages.upsert"]) {
const { messages } = events["messages.upsert"];
messages.forEach((msg) => {
// processing
if (getText(msg.message).startsWith("@hi")) {
console.log(msg.key.remoteJid);
}
});
}
});
}
WABot();