-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (95 loc) · 2.8 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
/*
* Author : Rahul <[email protected]>
* Vaccine Notification For Everyone
*/
// axios call the api
require("dotenv").config();
const axios = require("axios");
const _ = require("lodash");
const Twitter = require("twitter");
const moment = require("moment");
const cron = require("node-cron");
const accountSid = process.env.TWILIO_MESSAGE_ACCOUNT_SID;
const authToken = process.env.TWILIO_MESSAGE_ACCOUNT_AUTH_TOKEN;
const twilioClient = require("twilio")(accountSid, authToken);
const getCenters = async (districtId) => {
let config = {
headers: {
accept: "application/json",
"Accept-Language": "hi_IN",
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
},
};
let today = moment();
let dateString = today.format("DD-MM-YYYY");
const center = await axios.get(
`https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=${districtId}&date=${dateString}`,
config
);
return center.data.centers;
};
const notifyUser = async (payload) => {
// Send SMS Alert
if (process.env.TWILIO_ENABLED) {
await twilioClient.messages.create({
body: "Vaccine Available",
messagingServiceSid: process.env.TWILIO_MESSAGE_SERVICE_SID,
to: process.env.TWILIO_MESSAGE_TO,
});
}
// Send Email
// Send Whatsapp
// Send Twitter Update
if (process.env.TWITTER_ENABLED) {
let Tclient = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
Tclient.post(
"statuses/update",
{ status: "test status" },
function (error, tweet, response) {
if (error) throw error;
}
);
}
};
const main = async () => {
try {
cron.schedule("*/50 * * * *", async () => {
await checkAvailability();
});
} catch (e) {
console.log("an error occured: " + JSON.stringify(e, null, 2));
throw e;
}
};
// Main Execution
const checkAvailability = async () => {
const centers = await getCenters(process.env.DISTRICT_ID);
let availableLocation = [];
centers.map((c) => {
// Find From Session
c.sessions.map((session) => {
if (session.available_capacity > 0) {
availableLocation.push({
name: c.name,
date: session.date,
capacity: session.available_capacity,
});
}
});
});
if (availableLocation.length > 0) {
console.log("Vaccine Available");
await notifyUser({ count: availableLocation.length });
} else {
console.log("No Vaccine Available");
}
};
main().then(() => {
console.log("Application Started!");
});