-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
135 lines (116 loc) · 3.87 KB
/
index.ts
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
import "https://deno.land/[email protected]/dotenv/load.ts";
import {
TelegramBot,
UpdateType,
} from "https://deno.land/x/[email protected]/mod.ts";
import { get_db } from "./src/db.ts";
import { get_env } from "./src/env.ts";
import { ResponseLab } from "./src/interface.ts";
const env = get_env();
const db = get_db();
function generataRisposta(id: number, nome?: string): string {
const date = new Date().toLocaleString("it", { timeZone: "Europe/Rome" });
const formattedDate = date;
if (id == 1 && nome != undefined)
return (
"Il laboratorio è stato aperto " + formattedDate + " da " + nome
);
else if (id === 1)
return (
"Il laboratorio è stato aperto " + formattedDate + " con la chiave"
);
else return "Il laboratorio è stato chiuso " + formattedDate;
}
const getCurrentLabState = async () => {
try {
const raw = await fetch(env.GET_LAB_STATE_ENDPOINT);
const json: ResponseLab = await raw.json();
const rispostaValoreAPI = json.id;
console.log(json);
const lastState = await db.query(
"SELECT status FROM logs ORDER BY id DESC LIMIT 1"
);
if (lastState.length === 0 || lastState[0][0] !== rispostaValoreAPI) {
await db.query("INSERT INTO logs (status) VALUES (?)", [
rispostaValoreAPI,
]);
const users = await db.query("SELECT telegram_id FROM users");
const rawFetchApetureOnline = await fetch(env.GET_LAB_HISTORY_ENDPOINT);
const jsonFetchApetureOnline = await rawFetchApetureOnline.json();
let nome = undefined;
const dataAttuale = new Date() as any;
const dataApetura = new Date(jsonFetchApetureOnline[0].time+env.TIMEZONE_OFFSET) as any;
if ((dataAttuale - dataApetura) < env.HISTORY_INTERVAL) {
nome = jsonFetchApetureOnline[0].user;
}
//console.log(nome);
for (const user of users) {
bot.sendMessage({
chat_id: user[0],
text: generataRisposta(rispostaValoreAPI, nome),
});
}
}
} catch (_) {}
};
const bot = new TelegramBot(env.TOKEN_TELEGRAM);
bot.run({
polling: true,
});
bot.on(UpdateType.Error, ({ error }) =>
console.error("Glitch in the Matrix", error.stack)
);
bot.on(UpdateType.Message, async ({ message }) => {
console.log(message);
if (message?.text === "/start") {
let lastState;
try {
if(env.DEBUG){console.log("Inizio recupero logs nel db");}
lastState = await db.query(
"SELECT status FROM logs ORDER BY id DESC LIMIT 1"
)[0][0];
if(env.DEBUG){console.log("Fine recupero logs nel db");}
} catch (_) {}
const stringaStato =
lastState === 1
? "Il laboratorio è aperto attualmente"
: "Il laboratorio è chiuso attualmente";
console.log("start");
console.log(message!.from!.id);
try {
if(env.DEBUG){console.log("Inizio insert utente nel db");}
db.query("INSERT INTO users (telegram_id) VALUES (?)", [
message!.from!.id,
]);
if(env.DEBUG){console.log("Fine insert utente nel db");}
if(env.DEBUG){console.log("Inizio invio messaggio");}
bot.sendMessage({
chat_id: message.chat.id,
text:
"Ciao, sono il bot HLCS. Ti avviserò quando il laboratorio sarà aperto o chiuso. \n" +
stringaStato,
});
if(env.DEBUG){
console.log("Fine invio messaggio");
}
} catch (_) {
if(env.DEBUG){
console.log("Inizio invio messaggio");
}
bot.sendMessage({
chat_id: message.chat.id,
text:
"Ciao, sono il bot HLCS. Ti avviserò quando il laboratorio sarà aperto o chiuso, ti notifico che eri già iscritto \n" +
stringaStato,
});
if(env.DEBUG){
console.log("Fine invio messaggio");
}
}
}
});
setTimeout(() => {
console.log("Bot Avviato");
setInterval(getCurrentLabState, env.POLLING_INTERVAL);
//getCurrentLabState();
}, 7000);