-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (83 loc) · 3.14 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
require('dotenv').config();
const { Telegraf } = require('telegraf');
const axios = require("axios");
const schedule = require('node-schedule');
const express = require("express");
const expressApp = express();
const API_TOKEN = process.env.API_TOKEN;
const chat_id = process.env.CHAT_ID;
const PORT = process.env.PORT;
const bot = new Telegraf(API_TOKEN);
const district_id = process.env.DISTRICT_ID //Mumbai
const newSlots = require('./util/slots');
const {getOldSlots, updateNewSlots} = require('./util/dbmodules');
let telegram_url = `https://api.telegram.org/bot${API_TOKEN}/sendMessage`
// Function: Update data every 10 second
const job = schedule.scheduleJob('*/10 * * * * *', function(){
(async () => {
const oldData = await getOldSlots(chat_id);
const newData = await newSlots(chat_id, district_id);
comparer = (oldArr) => {
return (newArr) => {
return oldArr.filter((old) => {
return old.name == newArr.name && old.session_id == newArr.session_id
}).length == 0;
}
}
updateNewSlots(chat_id, newData);
let latestSlots = newData.filter(comparer(oldData));
latestSlots.forEach((item) => {
if(latestSlots.length > 0){
if(item.available > 10){
let message = `
<b>✅ New Slot Available</b>
<b>Vaccine:</b> <b> ${item.vaccine} </b>
<b>Date: </b> ${item.date}
<b>Pin Code:</b> ${item.pincode}
${item.district_name}
<b>Age Group:</b> <b> ${item.age}+ </b>
<b>Center Name: </b> ${item.name}
<b>Address: </b> ${item.address}
<b>Available Capacity Dose 1: ${item.dose1} </b>
<b>Available Capacity Dose 2: ${item.dose2} </b>
Fee:<b> ${item.fee} </b>
`
axios.post(telegram_url,{
chat_id: chat_id,
text: message,
parse_mode: 'HTML',
reply_markup: {
"resize_keyboard": true,
"inline_keyboard": [
[
{
"text": "Book on Cowin",
"url": "https://selfregistration.cowin.gov.in/"
}
]
]
}
}).then(res => {
output = res.data.result.text;
}).catch(err => {
console.log(err);
})
}
}
})
var d = new Date();
var currentDateTime = d.toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }, { hour12: true});
console.log(`New Slots Found: ${latestSlots.length} --- On: ${currentDateTime}`);
return latestSlots;
})();
});
// bot.launch()
expressApp.get('/', (req, res) => {
res.send('Your bot is running')
});
expressApp.listen(PORT, () => {
console.log(`Server is running on PORT: ${PORT}`);
});
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))