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

Commit

Permalink
Update richieste server
Browse files Browse the repository at this point in the history
  • Loading branch information
aletomm committed Mar 28, 2020
1 parent f9eb036 commit 703b838
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,44 @@ exports.botServer = http.createServer((req, res) => {
console.log(JSON.parse(jsonRes));
const response = JSON.parse(jsonRes);
const chatId = response.chat_id;
const authCode = response.auth_code;
axios
.post(
`https://api.telegram.org/bot${tokenBot}/sendMessage?chat_id=${chatId}&text=Ecco il tuo codice di autenticazione: ${authCode}`
)
.then(() => {
console.log("Messaggio inviato con successo");
})
.catch((err) => {
console.log(
"Errore " + err.response.status + " nell'invio del messaggio"
);
});
if (response.req_type == "authentication") {
const authCode = response.auth_code;
axios
.post(
`https://api.telegram.org/bot${tokenBot}/sendMessage?chat_id=${chatId}&text=Ecco il tuo codice di autenticazione: ${authCode}`
)
.then(() => {
console.log("Messaggio inviato con successo");
})
.catch((err) => {
console.log(
"Errore " + err.response.status + " nell'invio del messaggio"
);
});
} else if (response.req_type == "alert") {
const deviceId = response.device_id;
const sensorId = response.sensor_id;
const sensorValue = response.sensor_value;
const threshold = response.threshold;
const valueType = response.value_type;

const message1 = `⚠️ Il sensore ${sensorId} del dispositivo ${deviceId} ha registrato un valore di `;
const message2 = `${sensorValue} ${valueType} superando la soglia di ${threshold}`;
axios
.post(
`https://api.telegram.org/bot${tokenBot}/sendMessage?chat_id=${chatId}&text=` +
message1 +
message2
)
.then(() => {
console.log("Messaggio inviato con successo");
})
.catch((err) => {
console.log(
"Errore " + err.response.status + " nell'invio del messaggio"
);
});
}
});
req.on("end", () => {
console.log(JSON.parse(jsonRes));
Expand Down

0 comments on commit 703b838

Please sign in to comment.