-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (82 loc) · 2.32 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
const axios = require("axios");
const calcTime = (offset) => {
var d = new Date();
var utc = d.getTime() + d.getTimezoneOffset() * 60000;
var nd = new Date(utc + 3600000 * offset);
const date = nd.getDate() > 9 ? nd.getDate() : `0${nd.getDate()}`;
const month =
nd.getMonth() + 1 > 9 ? nd.getMonth() + 1 : `0${nd.getMonth() + 1}`;
return `${date}-${month}-2021`;
};
const handler = async (event) => {
const _date = calcTime("+5.5");
const CHAT_ID = "";
const BOT_API_KEY = "";
const _res = await new Promise(async (resolve, reject) => {
axios
.get(
`https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=389&date=${_date}`
)
.then(async ({ data }) => {
const centers = data.centers;
let out = centers.reduce((acc, curr) => {
const sessions = curr.sessions.map((_el) => ({
name: curr.name,
address: curr.address,
..._el,
}));
acc = acc.concat(sessions);
return acc;
}, []);
dates = Array.from(new Set(out.map((el) => el.date)));
console.log(dates);
out = out
.filter((el) => el.min_age_limit === 18)
.filter((el) => el.available_capacity_dose1 > 0)
.filter((el) => el.vaccine === "COVAXIN");
if (new Date().getMinutes() % 10 === 0) {
await new Promise((res, rej) => {
// Interval Pings
axios
.get(
`https://api.telegram.org/bot${BOT_API_KEY}/sendMessage?chat_id=${CHAT_ID}&text=Interval Ping`
)
.then(({ data }) => {
console.log(data);
res(data);
})
.catch((err) => {
rej(err);
});
});
}
if (out.length > 0) {
const _data = await new Promise((res, rej) => {
// Positive Responses
axios
.get(
`https://api.telegram.org/bot${BOT_API_KEY}/sendMessage?chat_id=${CHAT_ID}&text=${out.length} slot(s) available in Nasik for dose 1 of COVAXIN. Checked on ${_date}. Showing for ${dates}`
)
.then(({ data }) => {
console.log(data);
res(data);
})
.catch((err) => {
rej(err);
});
});
resolve(_data);
} else {
resolve(null);
}
})
.catch((err) => reject(err));
});
const response = {
statusCode: 200,
body: JSON.stringify(_res, null, 2),
};
return response;
};
exports.handler = handler;
// handler();