This repository was archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
86 lines (83 loc) · 2.79 KB
/
Copy pathserver.js
File metadata and controls
86 lines (83 loc) · 2.79 KB
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
require("dotenv").config();
const http = require("http");
const axios = require("axios");
const sendMessage = (message, chatId) => {
axios
.get(`https://api.telegram.org/bot${process.env.BOT_TOKEN}/sendMessage`, {
data: {
chat_id: chatId,
parse_mode: "markdown",
text: message,
},
})
.then((res) => {
console.log("Messaggio inviato con successo: " + res.status);
// console.log(res.data);
})
.catch((err) => {
console.log(err);
console.log("Errore (" + err.response + ") nell'invio del messaggio");
});
};
const checkChatId = (chatId) => {
const pattern = "^[0-9]{6,}$";
const regex = new RegExp(pattern);
return regex.test(chatId);
};
const botServer = http.createServer((req, res) => {
// Request and Response object
if (req.method === "POST") {
let jsonRes = "";
req.on("data", (data) => {
jsonRes += data.toString();
console.log(JSON.parse(jsonRes));
const response = JSON.parse(jsonRes);
if (response.reqType === "authentication") {
const authCode = response.authCode;
const chatId = response.chatId;
if (!checkChatId(chatId)) {
console.log("Invalid chat id");
} else {
const authMessage = `\u{1F4F2} Ecco il tuo codice di autenticazione: ${authCode}`;
sendMessage(authMessage, chatId);
}
} else if (response.reqType === "alert") {
const chatIds = response.telegramChatIds;
const threshold = response.currentThreshold;
const sensorType = response.sensorType.replace(/_/g, "\\_");
const deviceName = response.deviceName.replace(/_/g, "\\_");
const gatewayName = response.realGatewayName; // .replace(/_/g, "\\_"); // Solo se non si usa `
let valueType;
switch (response.currentThresholdType) {
case 0:
valueType = "superiore (>)";
break;
case 1:
valueType = "inferiore (<)";
break;
case 2:
valueType = "uguale (=)";
break;
}
const alertMessage = `\u{26A0}\u{FE0F} Alert #${response.alertId} \u{26A0}\u{FE0F}
- *Sensore:* ${sensorType} (S@${response.realSensorId})
- *Dispositivo:* ${deviceName} (D#${response.deviceId})
- *Gateway:* \`${gatewayName}\`
- *Valore:* ${response.currentValue} *${valueType}* alla soglia ${threshold}`;
// eslint-disable-next-line guard-for-in
for (const index in chatIds) {
const chatId = chatIds[index];
if (!checkChatId(chatId)) {
console.log("Invalid chat id");
} else {
sendMessage(alertMessage, chatId);
}
}
}
});
req.on("end", () => {
res.end("ok");
});
}
});
module.exports = { botServer, checkChatId, sendMessage };