-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (64 loc) · 2.78 KB
/
app.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
/* eslint-disable no-undef */
'use strict'
process.env["NTBA_FIX_350"] = 1; //https://github.com/yagop/node-telegram-bot-api/issues/482
process.env["NTBA_FIX_319"] = 1; //
import { HappyBot } from './Classes/bot.js'
import http from 'http';
import serveStatic from 'serve-static';
import finalhandler from 'finalhandler';
import * as schedule from 'node-schedule';
import { LogToPublic, ResetPublicLog_HTML, ShortError } from './utils.js';
import { AwSheetHandler } from './Classes/AwGSheetHandler.js';
import { AwBdDbModel } from './Classes/AwBdDbModel.js';
import { existsSync } from 'fs';
import exitHook from 'exit-hook';
const TG_Token = process.env.token;
const GoogleSheetID = process.env.document;
const TG_GroupId = "-1001224485894";//process.env.groupId
const GoogleServiceAcc = process.env.email;
const GoogleKey = process.env.key.replace(/\\n/g, "\n")
let publicPath = `./public_log/`;
let htmlindex = `./public_log/index.html`;
if (!existsSync(htmlindex)) { //create indext based on template.
await ResetPublicLog_HTML();
}
var serve = serveStatic(publicPath, { index: ['index.html', 'index.htm'] })
const server = http.createServer((rq, rs) => {
serve(rq, rs, finalhandler(rq, rs));
});
server.listen(process.env.PORT || 5000);
/**/
Main();
exitHook(() => { util.AddCounter(true) });
async function Main() {
try {
let counter = 0
let dbModel = new AwBdDbModel(GoogleSheetID, 0, 946533461);
let SheetHandler = new AwSheetHandler(GoogleServiceAcc, GoogleKey, dbModel);
let bot = new HappyBot(TG_Token, SheetHandler);
await bot.Init();
const rule = new schedule.RecurrenceRule();
rule.hour = new schedule.Range(0, 23, 2); //every x hour
rule.minute = 0 // needed for every
let ran = false;
/*let runner =*/ schedule.scheduleJob(rule, () => {
let date = new Date();
ran = false;
if (date.getHours() < 19 & date.getHours() > 1) { //in case server is utc, so i can send msg 5am in tehran
bot.SendHBD(TG_GroupId);
ran = true;
}
console.log(
`${++counter}. SendHBD Called: '${ran}' at (${date.toUTCString()}).\n` +
`\t Next run at: (${rule.nextInvocationDate().toUTCString()})`
);
LogToPublic(`${counter}. SendHBD Called: '${ran}' at (${date.toUTCString()}).\n` +
`\t Next run at: (${rule.nextInvocationDate().toUTCString()})`);
});
console.log(`0 - First Run at: (${rule.nextInvocationDate().toUTCString()})`);
LogToPublic(`0 - First Run at: (${rule.nextInvocationDate().toUTCString()})`);
} catch (err) {
console.log(`Main Entry Err: ${ShortError(err, 200)}`);
LogToPublic(`Main Entry Err: ${ShortError(err, 200)}`);
}
}