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

Commit

Permalink
Merge branch 'develop' of https://github.com/RedRoundRobin/swe-telegram
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
giovd8 committed Apr 23, 2020
2 parents 94b41e0 + f8018fc commit 7e8eb96
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 64 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@
- entrare nella cartella con il terminale
- $ npm install
- $ node bot


#### Comandi per code style JS

- Check:
`npm run prettier-eslint-check`
`npm run prettier-eslint-test-check`
- Autofix:
`npm run prettier-eslint`
`npm run prettier-eslint-test`


#### Comandi per i test JS

`npm test`
72 changes: 36 additions & 36 deletions commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@
const axios = require("axios");

const botLogin = (bot) => {
bot.command("login", (message) => {
const username = message.from.username;
const chatId = message.from.id;
axios
.post(`http://core.host.redroundrobin.site:9999/auth/telegram`, {
telegramName: username,
telegramChat: chatId,
})
.then((res) => {
const code = res.data.code;
const token = res.data.token;
if (code === 1) {
axios.defaults.headers.common["Authorization"] = "Bearer " + token;
return message.reply("Username trovato, registrazione riuscita");
} else if (code === 2) {
axios.defaults.headers.common["Authorization"] = "Bearer " + token;
return message.reply(
"Account già registrato, nessuna modifica apportata"
);
} else if (code === 0) {
return message.reply(
"Username non trovato, registra il tuo Username dalla web-app"
);
}
})
.catch((err) => {
console.log(err);
// console.log(err.status);
if (err.response.status === 403) {
return message.reply(
"Rieffettua l'autenticazione usando il comando /login"
);
} else {
return message.reply("Errore nel controllo dei dati");
}
});
bot.command("login", (message) => {
const username = message.from.username;
const chatId = message.from.id;
axios
.post(`http://thirema-api:9999/auth/telegram`, {
telegramName: username,
telegramChat: chatId,
})
.then((res) => {
const code = res.data.code;
const token = res.data.token;
if (code === 1) {
axios.defaults.headers.common["Authorization"] = "Bearer " + token;
return message.reply("Username trovato, registrazione riuscita");
} else if (code === 2) {
axios.defaults.headers.common["Authorization"] = "Bearer " + token;
return message.reply(
"Account già registrato, nessuna modifica apportata"
);
} else if (code === 0) {
return message.reply(
"Username non trovato, registra il tuo Username dalla web-app"
);
}
})
.catch((err) => {
console.log(err);
// console.log(err.status);
if (err.response != null && err.response.status === 403) {
return message.reply(
"Rieffettua l'autenticazione usando il comando /login"
);
} else {
return message.reply("Errore nel controllo dei dati");
}
});
});
};
module.exports.botLogin = botLogin;
40 changes: 22 additions & 18 deletions commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ const linkAPI = process.env.LINK_API;
const axios = require("axios");

const botStatus = (bot) => {
bot.command("status", (message) => {
axios
.get(`${linkAPI}/status`)
.then((res) => {
const data = res.data;
const name = data.name;
const surname = data.surname;
const email = data.email;
const typeNumber = data.type;
let type = "Utente";
if (typeNumber === 1) {
type = "Moderatore";
} else if (typeNumber === 2) {
type = "Amministratore";
}
return message.replyWithMarkdown(
`

bot.command("status", (message) => {
const username = message.from.username;
console.log(username);
axios
.get(`http://thirema-api:9999/users?telegramName=` + username)
.then((res) => {
console.log(res.data);
const data = res.data[0];
const name = data.name;
const surname = data.surname;
const email = data.email;
const typeNumber = data.type;
let type = "Utente";
if (typeNumber === 1) {
type = "Moderatore";
} else if (typeNumber === 2) {
type = "Amministratore";
}
return message.replyWithMarkdown(
`
Ecco i tuoi dati *${message.from.username}*
- *Nome:* ${name}
- *Cognome:* ${surname}
Expand All @@ -28,7 +32,7 @@ const botStatus = (bot) => {
);
})
.catch((err) => {
if (err.response.status === 403) {
if (err.response != null && err.response.status === 403) {
message.reply("Rieffettua l'autenticazione usando il comando /login");
} else {
message.reply("Errore nel controllo dei dati");
Expand Down
30 changes: 20 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const sendMessage = (message, chatId) => {
)
.then((res) => {
console.log("Messaggio inviato con successo");
console.log(res.response.status);
console.log(res.status);
console.log(res.data);
res.json("OK");
})
Expand Down Expand Up @@ -54,18 +54,28 @@ const botServer = http.createServer((req, res) => {
sendMessage(authMessage, chatId);
}
} else if (response.reqType == "alert") {
const chatsId = response.chatId;
const deviceId = response.deviceId;
const sensorId = response.sensorId;
const sensorValue = response.sensorValue;
const threshold = response.threshold;
const valueType = response.valueType;
const chatIds = response.telegramChatIds;
const deviceId = response.realDeviceId;
const sensorId = response.realSensorId;
const sensorValue = response.currentValue;
const threshold = response.currentThreshold;
let valueType;
switch (response.currentThresholdType) {
case 0:
valueType = "superiore";
break;
case 1:
valueType = "inferiore";
break;
case 2:
valueType = "uguale";
}
const messagePart1 = `Attenzione: il sensore ${sensorId} del dispositivo ${deviceId} ha registrato un valore di `;
const messagePart2 = `${sensorValue} ${valueType} superando la soglia (${threshold})`;
const messagePart2 = `${sensorValue} ${valueType} alla soglia (${threshold})`;
const alertMessage = messagePart1 + messagePart2;
// eslint-disable-next-line guard-for-in
for (const index in chatsId) {
const chatId = chatsId[index];
for (const index in chatIds) {
const chatId = chatIds[index];
if (!checkChatId(chatId)) {
console.log("Invalid chat id");
} else {
Expand Down

0 comments on commit 7e8eb96

Please sign in to comment.