-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
212 lines (176 loc) · 6.94 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
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
202
203
204
205
206
207
208
209
210
211
212
const TelegramBot = require('node-telegram-bot-api');
const { format } = require('path');
const { start } = require('repl');
const fs = require('fs');
const { includes } = require('lodash');
const express = require('express');
const app = express();
const port = 3000;
app.get('/', function(request, response){ response.send(`Монитор активен. Локальный адрес: http://localhost:${port}`); });
app.listen(port, () => console.log());
const token = '5468851871:AAHTmWbryFXtMogAIqecx2znizfRBNQOztM'
const bot = new TelegramBot(token, { polling: true })
console.log('READY')
// let holidays = ['2022-07-09'];
function holidaysService()
{
let holidaysCount = 0
let holidaysNotFormatted = JSON.parse(fs.readFileSync('holidays.json'))
console.log(holidaysNotFormatted)
let holidays = []
let reasons = []
let counter = 0
while (counter < holidaysNotFormatted.days.length)
{
console.log('123')
reasons[counter] = holidaysNotFormatted.days[counter][1]
holidays[counter] = holidaysNotFormatted.days[counter][0]
counter = counter + 1
}
// console.log(holidays)
// console.log(reasons)
return [holidays, reasons, holidaysCount]
}
function getBusinessDatesCount(startDate, endDate) {
let holidays = holidaysService()[0]
let holidaysCount = holidaysService()[2]
let count = 0;
const curDate = new Date(startDate.getTime());
while (curDate <= endDate) {
const dayOfWeek = curDate.getDay();
let formattedDate = curDate.toISOString().slice(0,10)
// console.log(formattedDate)
// console.log()
if (holidays.includes(formattedDate))
{
console.log('t')
holidaysCount = holidaysCount + 1
}
if(dayOfWeek !== 0 && dayOfWeek !== 6) count++;
curDate.setDate(curDate.getDate() + 1);
}
console.log('OTDIX' + holidaysCount)
console.log(count - holidaysCount);
return count - holidaysCount;
}
function getDatesCount(startDate, endDate)
{
let difference = endDate.getTime() - startDate.getTime();
let days = Math.ceil(difference / (1000 * 3600 * 24));
console.log(days + ' обычных дней');
return days;
}
function getDatesBeforeHolidays(startDate)
{
let holidays = holidaysService()[0]
let counter2 = 0
let looper = true
while (looper == true)
{
looper = new Date(holidays[counter2]) < startDate
counter2 = counter2 + 1
console.log('LOOPER' + looper)
}
console.log('before - ' + counter2 - 1)
let BHDays = getDatesCount(startDate, new Date(holidays[counter2-1]))
let BHBuisnessDays = getBusinessDatesCount(startDate, new Date(holidays[counter2-1]))
return [BHDays, BHBuisnessDays]
}
function getAfterworks()
{
let counter3 = 0
let awNotFormated = JSON.parse(fs.readFileSync('afterworks.json'))
let aw = awNotFormated.days
let amountOfAW = 0
while (counter3 < aw.length)
{
if (new Date(aw[counter3]) > new Date())
{
amountOfAW = amountOfAW + 1
}
counter3 = counter3 + 1
}
return amountOfAW;
}
bot.on('message', function (msg) {
var chatId = msg.chat.id; // Берем ID чата (не отправителя)
let startDate = new Date();
console.log(startDate)
let endDate = new Date('05/24/2023');
bot.sendMessage('925304597', getBusinessDatesCount(startDate, endDate) + ' = ' + getDatesCount(startDate, endDate) + '-=' + getDatesBeforeHolidays(startDate));
});
setInterval(function(){
let startDate = new Date();
console.log(startDate)
let endDate = new Date('05/24/2023');
let formattedDate2 = startDate.toISOString().slice(0,10)
////////////////
if ((startDate.getHours() == 17) && (startDate.getDay() !== 6) && (startDate.getDay() !== 0) && (holidaysService()[0].includes(formattedDate2) == false)) // && (startDate.getMinutes() == 0)
{
console.log('E')
let month = startDate.getMonth()
let date = startDate.getDate()
let weekday = startDate.getDay()
let weekFormat = 'пн'
switch(weekday)
{
case 0: weekFormat = 'воскресенье'
break;
case 1: weekFormat = 'понедельник'
break;
case 2: weekFormat = 'вторник'
break;
case 3: weekFormat = 'среда'
break;
case 4: weekFormat = 'четверг'
break;
case 5: weekFormat = 'пятница'
break;
case 6: weekFormat = 'суббота'
break;
}
let monthFormat = 'июля'
switch(month) {
case 0: monthFormat = " января"
break
case 1: monthFormat = " февраля"
break;
case 2: monthFormat = " марта"
break;
case 3: monthFormat = " апреля"
break;
case 4: monthFormat = " мая"
break;
case 5: monthFormat = " июня"
break;
case 6: monthFormat = " июля"
break;
case 7: monthFormat = " августа"
break;
case 8: monthFormat = " сентября"
break;
case 9: monthFormat = " октября"
break;
case 10: monthFormat = " ноября"
break;
case 11: monthFormat = " декабря"
break;
}
let normalDays = getDatesCount(startDate, endDate)
let buisnessdays = getBusinessDatesCount(startDate, endDate)
let daysToHolidays = getDatesBeforeHolidays(startDate)
let passedDays = getBusinessDatesCount(new Date('2022-09-01'), startDate)
let percentage = (passedDays/166*100).toFixed(2)
let service = holidaysService()
console.log('SERVICEEEE\n' + service)
i = 0
while (startDate > new Date(service[0][i]))
{
i++
}
console.log('I AMOUNT: ' + i)
bot.sendMessage('925304597', '<u><b>' + date + monthFormat + ' - ' + weekFormat + '. День ' + passedDays + '</b></u> (<i>' + percentage + '%)</i>\n\n<b>≫Основное</b>\nОсталось дней: <code>' + normalDays + '</code>\nИз них рабочих: <code>' + buisnessdays + '</code>\n\n\n<b>≫Праздники и каникулы</b>\nСледующее событие: <code>' + service[1][i] + ' (' + service[0][i] + ')</code>\nОсталось дней: <code>' + daysToHolidays[0] + '</code>\nИз них рабочих: <code>' + daysToHolidays[1] + '</code>', { parse_mode: 'HTML' });
// [holidays, reasons, holidaysCount]
// bot.sendMessage('678007842', date + monthFormat + ' - ' + weekFormat + '. День ' + passedDays + ' ( ' + percentage + '% )\n\n≫Основное\nОсталось дней: ' + normalDays + '\nИз них рабочих: ' + buisnessdays + '\nС учётом доп. работы:' + (getAfterworks()+buisnessdays) + '\n\n≫Праздники и отпуск\nСледующее событие: ' + service[1][service[2]] + ' (' + service[0][service[2]] + ')\nОсталось дней:' + daysToHolidays[0] + '\nИз них рабочих: ' + daysToHolidays[1]);
}
}, 3600000);