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

Commit

Permalink
Bug fixed, attiva/disattiva sensore
Browse files Browse the repository at this point in the history
  • Loading branch information
giovd8 committed Apr 27, 2020
1 parent a8f4ea6 commit 965fd34
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions commands/devices.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Markup = require("telegraf/markup");
const Extra = require("telegraf/extra");
// const Extra = require("telegraf/extra");
let admin = false;

const botDevices = (bot, axios, auth) => {
const deviceList = [];
let deviceList = [];
bot.command("devices", (message) => {
const username = message.from.username;
const getType = async () => {
const getTypeUser = async () => {
await auth.jwtAuth(axios, message);
return await axios
.get(`${process.env.URL_API}/users?telegramName=${username}`)
Expand All @@ -27,7 +27,7 @@ const botDevices = (bot, axios, auth) => {
return message.reply(`Esegui di nuovo il comando /login`);
});
};
getType().then(() => {
getTypeUser().then(() => {
if (admin) {
const getButtons = async () => {
return await axios
Expand All @@ -43,26 +43,59 @@ const botDevices = (bot, axios, auth) => {
)
);
});
deviceList.push(
Markup.callbackButton("bottone senza risposta", "ciaone")
);
// deviceList.push(
// Markup.callbackButton("bottone senza risposta", "ciaone")
// );
})
.catch(() => {
admin = false;
});
};
getButtons(message).then(() => {
console.log("Lista dispositivi caricata correttamente");
message.reply("", Markup.removeKeyboard(true));
Markup.removeKeyboard();
message.reply(
"Ecco la lista dei dispositivi a quali puoi inviare un comando:",
"Seleziona il dispositivo a cui inviare un comando:",
Markup.keyboard(deviceList).oneTime().resize().extra()
);
deviceList = [];
});
}
});
});
bot.hears(/^\d(_)(.*)$/gi, (message) => {
// user has selected to switch on one sensor
bot.hears(/^\d(_)+\d(_)(Attiva)/g, (message) => {
const devSensID = message.match[0].match(/\d/gi);
Markup.removeKeyboard();
message.reply(
`Il sensore ${devSensID[1]} del dispositivo ${devSensID[0]} è stato attivato con successo!`
);
});
// user has selected to switch off one sensor
bot.hears(/^\d(_)+\d(_)(Disattiva)/g, (message) => {
const devSensID = message.match[0].match(/\d/gi);
Markup.removeKeyboard();
message.reply(
`Il sensore ${devSensID[1]} del dispositivo ${devSensID[0]} è stato disattivato con successo!`
);
});
// user has selected one sensor
bot.hears(/^\d(_)+\d(_)(.*)/g, (message) => {
const sensorID = message.match[0].match(/\d/gi);
Markup.removeKeyboard();
message.reply(
"Seleziona l'opzione:",
Markup.keyboard([
`${sensorID[0]}_${sensorID[1]}_Attiva`,
`${sensorID[0]}_${sensorID[1]}_Disattiva`,
])
.oneTime()
.resize()
.extra()
);
});
// user has selected one device
bot.hears(/^\d(_)(.*)/g, (message) => {
const deviceID = message.match[0].match(/^\d/gi);
let sensorsList = [];
const getButtons = async () => {
Expand All @@ -73,7 +106,7 @@ const botDevices = (bot, axios, auth) => {
sensors.forEach((sensor) => {
sensorsList.push(
Markup.callbackButton(
`${sensor.sensorId}_${sensor.type}`,
`${deviceID}_${sensor.sensorId}_${sensor.type}`,
sensor.type
)
);
Expand All @@ -82,7 +115,7 @@ const botDevices = (bot, axios, auth) => {
};
getButtons(message).then(() => {
console.log("Lista sensori caricata correttamente");
message.reply("", Markup.removeKeyboard(true));
Markup.removeKeyboard();
message.reply(
"Seleziona il sensore:",
Markup.keyboard(sensorsList).oneTime().resize().extra()
Expand Down

0 comments on commit 965fd34

Please sign in to comment.