-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (122 loc) · 6.26 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
// for web scrapping
const rp = require('request-promise');
const cheerio = require('cheerio');
require('dotenv').config();
// for telegram bot
const Telegraf = require('telegraf');
// const { Composer } = require('micro-bot')
const bot = new Telegraf(process.env.BOT_TOKEN);
// const bot = new Composer()
// ******************************************************************************************
const landlineSent=[];
const site="http://intranetd.mtnldelhi.in:7778/j2ee/hrd/pend_list.jsp?category=WEST&subcategory=RG";
async function startApp(){
try{
letFRSData=[];
const response= await rp({
url: site,
json: true
});
let $=cheerio.load(response);
let firstRow=2;
let lastRow=$("tr").length-7;
for (let i = firstRow; i <= lastRow; i++) {
// Extract Individual Data
let EXCH= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(2)").text().trim();
let JESEC= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(3)").text().trim();
let TELNO= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(4)").text().trim();
let COMD= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(5)").text().trim();
let MDFNO= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(7)").text().trim();
let NAME= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(9)").text().trim();
let ADD= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(10)").text().trim();
let CONTACT= $("body > center > table > tbody > tr:nth-child("+i+") > td:nth-child(11)").text().trim();
// FOR RG 121
if (EXCH == 'RG' && JESEC == '121') {
// console.log(EXCH + JESEC);
const isSent= landlineSent.find(element => element == TELNO);
// if this landline fault is not sent
if (isSent===undefined) {
// design a message and send it now
var opts = {
"parse_mode": 'HTML'
};
let message=`TELNO:<b>${TELNO}</b>\nMDFNO:<b>${MDFNO}</b>\nADD:<b>${ADD}</b>\n\nJESEC:<i>${JESEC}</i>\nNAME:<i>${NAME}</i>\nCONTACT:<i>${CONTACT}</i>\nCOMD:<i>${COMD}</i>\n**********`;
bot.telegram.sendMessage(process.env.GROUP_ID_121,message,opts);
landlineSent.push(TELNO);
}
}
// FOR RG 141
if (EXCH == 'RG' && JESEC == '141') {
// console.log(EXCH + JESEC);
const isSent= landlineSent.find(element => element == TELNO);
// if this landline fault is not sent
if (isSent===undefined) {
// design a message and send it now
var opts = {
"parse_mode": 'HTML'
};
let message=`TELNO:<b>${TELNO}</b>\nMDFNO:<b>${MDFNO}</b>\nADD:<b>${ADD}</b>\n\nJESEC:<i>${JESEC}</i>\nNAME:<i>${NAME}</i>\nCONTACT:<i>${CONTACT}</i>\nCOMD:<i>${COMD}</i>\n***********`;
bot.telegram.sendMessage(process.env.GROUP_ID_141,message,opts);
landlineSent.push(TELNO);
}
}
// FOR RG 151
if (EXCH == 'RG' && JESEC == '151') {
// console.log(EXCH + JESEC);
const isSent= landlineSent.find(element => element == TELNO);
// if this landline fault is not sent
if (isSent===undefined) {
// design a message and send it now
var opts = {
"parse_mode": 'HTML'
};
let message=`TELNO:<b>${TELNO}</b>\nMDFNO:<b>${MDFNO}</b>\nADD:<b>${ADD}</b>\n\nJESEC:<i>${JESEC}</i>\nNAME:<i>${NAME}</i>\nCONTACT:<i>${CONTACT}</i>\nCOMD:<i>${COMD}</i>\n***********`;
bot.telegram.sendMessage(process.env.GROUP_ID_151,message,opts);
console.log("done");
landlineSent.push(TELNO);
}
}
// FOR RG 161
if (EXCH == 'RG' && JESEC == '161') {
// console.log(EXCH + JESEC);
const isSent= landlineSent.find(element => element == TELNO);
// if this landline fault is not sent
if (isSent===undefined) {
// design a message and send it now
var opts = {
"parse_mode": 'HTML'
};
let message=`TELNO:<b>${TELNO}</b>\nMDFNO:<b>${MDFNO}</b>\nADD:<b>${ADD}</b>\n\nJESEC:<i>${JESEC}</i>\nNAME:<i>${NAME}</i>\nCONTACT:<i>${CONTACT}</i>\nCOMD:<i>${COMD}</i>\n***********`;
bot.telegram.sendMessage(process.env.GROUP_ID_161,message,opts);
landlineSent.push(TELNO);
}
}
}
// console.log(date_ob.getHours());
}catch(e) {
console.log('Error happened while connecting to the DB: ', e.message)
}
}
console.log("Working Hours: 9:30 to 15:00 IST -> 4:00 - 9:30 UTC");
startApp();
// run this function every 10 minutes = 10*60*1000 milliseconds
var interval=setInterval(()=>{
const date_ob = new Date();
const currUTCHour=date_ob.getUTCHours();
const currUTCMin=date_ob.getUTCMinutes();
// if time is between 1000Hours to 1500Hours then run the command at regular interval of 10 minutes
// 10am to 3pm IST is 4:30am to 9:30pm UTC
if((currUTCHour>=4 && currUTCHour<9) || (currUTCHour==9 && currUTCMin<=30)){
console.log("["+currUTCHour+":"+currUTCMin+"] "+"Starting the application at: ",currUTCHour);
startApp();
}
else if(landlineSent.size>0){
console.log("["+currUTCHour+":"+currUTCMin+"] "+"Working Hours are over! Cleaning the memory");
landlineSent=[];
}
else console.log("["+currUTCHour+":"+currUTCMin+"] "+"Memory Clean complete. Working Hours are over!");
},10*60*1000);
// }
bot.start((ctx) => ctx.reply('Bot has started!'));
bot.launch();
// module.exports = bot