-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifyutils.js
201 lines (172 loc) · 5.13 KB
/
notifyutils.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
const { Client } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");
const fs = require("fs");
const redis = require("redis");
const redisClient = redis.createClient();
const SESSION_FILE_PATH = "./session.json";
const QR_FILE_PATH = "./qr.txt";
let client;
let sessionData;
if (fs.existsSync(SESSION_FILE_PATH)) {
sessionData = require(SESSION_FILE_PATH);
client = new Client({
session: sessionData,
});
} else {
client = new Client();
}
client.on("qr", (qr) => {
console.log("QR RECEIVED", qr);
if (fs.existsSync(QR_FILE_PATH)) {
fs.unlinkSync(QR_FILE_PATH);
}
fs.writeFile(QR_FILE_PATH, qr, function (err) {
if (err) {
console.error(err);
}
});
qrcode.generate(qr, { small: true });
});
client.on("message", (message) => {
console.log(message.body, message.from);
//message.reply('Ok')
});
client.on("ready", () => {
console.log("Client is ready!");
});
client.on("authenticated", (session) => {
console.log("Auth saving");
sessionData = session;
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
if (err) {
console.error(err);
}
});
});
client.on("auth_failure", () => {
console.log("Auth failure occured");
});
client.on("disconnected", (reason) => {
console.log("Disconnected", reason);
});
client.initialize();
function getQR() {
if (fs.existsSync(QR_FILE_PATH)) {
return {
status: "success",
data: fs.readFileSync(QR_FILE_PATH).toString(),
};
} else {
return { status: "error", message: "qr not generated/found" };
}
}
function killSessions() {
client.destroy();
if (fs.existsSync(SESSION_FILE_PATH)) {
fs.unlinkSync(SESSION_FILE_PATH);
}
if (fs.existsSync(SESSION_FILE_PATH)) {
return {
status: "error",
message: "Unable to kill session try a hard cleanup / restart",
};
} else {
return { status: "success", message: "Session killed succefuly" };
}
}
function resumeSession() {
if (fs.existsSync(SESSION_FILE_PATH)) {
client.initialize();
client.on("ready", () => {
console.log("Client is ready again!");
return { status: "success", message: "client resumed" };
});
} else {
return {
status: "error",
message: "Unknown error occured while resuming client",
};
}
}
function sendMessage(phoneNumber, center, sessions, userId) {
redisClient.get(`${userId}-${center.center_id}`, (err, data) => {
if (data != null) {
console.log(data, "data from redis");
const currentTime = new Date();
const milliseconds = Math.abs(currentTime - new Date(data));
const hours = milliseconds / 36e5;
if (hours < 6) {
console.log(
"Skipped sending message to ",
phoneNumber,
" due to recently sent "
);
} else {
console.log("Sending after 6 hours to", phoneNumber);
redisClient.set(
`${userId}-${center.center_id}`,
new Date(),
(err, data) => {
sendRealMessage(phoneNumber, center, sessions);
}
);
}
} else {
console.log("Sending about a new center to ", phoneNumber);
redisClient.set(
`${userId}-${center.center_id}`,
new Date(),
(err, data) => {
sendRealMessage(phoneNumber, center, sessions);
}
);
}
});
}
function sendRealMessage(phoneNumber, center, sessions) {
client.sendMessage(
`91${phoneNumber}@c.us`,
`Hi! We are gald to inform you the open slots for covid-19 vaccine in the region you were looking for
Name: ${center.name}
Address: ${center.address}
Pincode: ${center.pincode}
Free / Paid : ${center.fee_type}
Here are the list of sessions :
${sessions.map(
(session) => `
Date:${session.date},
Available Capacity : ${session.available_capacity},
Minimum Age Limit : ${session.min_age_limit},
Vacciine Name: ${session.vaccine},
`
)}
Quickly book your slots at https://selfregistration.cowin.gov.in
Stay Safe! Share the link among ur well wishers.
To Unsubscribe, Reply UNSUBSCRIBE! Feel free to send feedback via insta: sahil_on_wheels
Note:- Less than 10 slots probably means it may be booked until you pass the OTP stage.
We are trying to reduce repeated message. Please bear with us.
`
);
console.log("sentwhatsappmessage: to ", phoneNumber, " on ", new Date());
}
function sendWelcomeMessage(phoneNumber) {
console.log("sent welcome message to ", phoneNumber);
client.sendMessage(
`91${phoneNumber}@c.us`,
`Hi There,
Thank you for registering instant vaccination slot alerts.
We are improving gradually.
Stay Safe and Relaxed, we will notify you if new slots are available.
If you find this helpful Please share among u well wishers.
To Unsubscribe, Reply UNSUBSCRIBE! Feel free to send feedback via insta: sahil_on_wheels
Note:- Less than 10 slots probably means it may be booked until you pass the OTP stage.
We are trying to reduce repeated message. Please bear with us.
`
);
}
module.exports = {
sendMessage,
resumeSession,
killSessions,
sendWelcomeMessage,
};