Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
Fix alerts encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxelweb committed May 14, 2020
1 parent bfadf3a commit a1dfe47
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions utils/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ const axios = require("axios");

const sendMessage = (message, chatId) => {
axios
.post(
`https://api.telegram.org/bot${process.env.BOT_TOKEN}/sendMessage?chat_id=${chatId}&text=${message}`
)
.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");
console.log(res.status);
console.log(res.data);
res.json("OK");
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");
console.log("Errore (" + err.response + ") nell'invio del messaggio");
});
};

Expand All @@ -39,29 +41,33 @@ const botServer = http.createServer((req, res) => {
if (!checkChatId(chatId)) {
console.log("Invalid chat id");
} else {
const authMessage = `Ecco il tuo codice di autenticazione: ${authCode}`;
const authMessage = `\u{1F4F2} Ecco il tuo codice di autenticazione: ${authCode}`;
sendMessage(authMessage, chatId);
}
} else if (response.reqType === "alert") {
const chatIds = response.telegramChatIds;
const deviceId = response.realDeviceId;
const sensorId = response.realSensorId;
const sensorValue = response.currentValue;
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";
valueType = "superiore (>)";
break;
case 1:
valueType = "inferiore";
valueType = "inferiore (<)";
break;
case 2:
valueType = "uguale";
valueType = "uguale (=)";
break;
}
const messagePart1 = `Alert: il sensore S#${sensorId} del dispositivo D#${deviceId} ha registrato un valore di `;
const messagePart2 = `${sensorValue} ${valueType} alla soglia (${threshold})`;
const alertMessage = messagePart1 + messagePart2;
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];
Expand Down

0 comments on commit a1dfe47

Please sign in to comment.