Comandi Script da BOT

This commit is contained in:
paoloar77
2022-03-12 21:38:19 +01:00
parent 5872a11a15
commit d91aeadb5c
3 changed files with 203 additions and 84 deletions

View File

@@ -392,6 +392,8 @@ async function resetProcessingJob() {
async function inizia() { async function inizia() {
try {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
await telegrambot.sendMsgTelegram(tools.FREEPLANET, await telegrambot.sendMsgTelegram(tools.FREEPLANET,
telegrambot.ADMIN_USER_SERVER, telegrambot.ADMIN_USER_SERVER,
@@ -404,11 +406,11 @@ async function inizia() {
} else { } else {
// await telegrambot.sendMsgTelegram(tools.FREEPLANET, telegrambot.ADMIN_USER_SERVER, `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}!`); await telegrambot.sendMsgTelegramToTheAdminAllSites(`Ciao Admin\n` + `🔅🔅🔅 Il Server col BOT di {appname} è appena ripartito!`, false);
await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET,
telegrambot.ADMIN_IDTELEGRAM_SERVER, }
`Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + } catch (e) {
`🔅 Il Server ${process.env.DATABASE} è appena ripartito!`);
} }
} }
@@ -530,6 +532,10 @@ async function faitest() {
// const $vers = tools.getVersionint('1.92.45'); // const $vers = tools.getVersionint('1.92.45');
if (true) {
// tools.execScript("ls -la");
}
if (false) { if (false) {
const prova = tools.getConfSiteOptionEnabledByIdApp('13', shared_consts.ConfSite.Notif_Reg_Push_Admin); const prova = tools.getConfSiteOptionEnabledByIdApp('13', shared_consts.ConfSite.Notif_Reg_Push_Admin);
console.log('prova', prova); console.log('prova', prova);

View File

@@ -268,6 +268,10 @@ const Menu = {
'Inv e NO 7 Req', 'Inv e NO 7 Req',
MSGSTAFF: emoji.get('incoming_envelope') + ' Invia a STAFF', MSGSTAFF: emoji.get('incoming_envelope') + ' Invia a STAFF',
MSGPAOLO: emoji.get('incoming_envelope') + ' Invia a PAOLO', MSGPAOLO: emoji.get('incoming_envelope') + ' Invia a PAOLO',
RESTART_SRV: emoji.get('incoming_envelope') + 'RestartServer',
REBOOT_SRV: emoji.get('incoming_envelope') + 'RebootServer',
EXECSH: emoji.get('incoming_envelope') + 'ExecSH',
LOG_SRV: '🖥Logserver.sh',
MSGATUTTI: emoji.get('incoming_envelope') + ' Invia a TUTTI', MSGATUTTI: emoji.get('incoming_envelope') + ' Invia a TUTTI',
it: { it: {
ACCEDI: emo.PERSON + ' Accedi', ACCEDI: emo.PERSON + ' Accedi',
@@ -404,9 +408,8 @@ const MenuYesNo = {
const MenuAdmin = { const MenuAdmin = {
it: { it: {
menu: [ menu: [[Menu.RESTART_SRV + ' 6711', Menu.LOG_SRV + ' 6711'],
[Menu.MSGSTAFF, Menu.MSGATUTTI, Menu.it.INDIETRO], [Menu.REBOOT_SRV+ ' 6711', Menu.it.INDIETRO]],
[Menu.MSGPAOLO]],
}, },
}; };
@@ -677,6 +680,10 @@ const MyTelegramBot = {
ISCRIZIONE_CONACREIS: 2, ISCRIZIONE_CONACREIS: 2,
}, },
getAppTelegram: function() {
return appTelegram;
},
local_sendMsgTelegramByIdTelegram: async function(idapp, idtelegram, text, local_sendMsgTelegramByIdTelegram: async function(idapp, idtelegram, text,
message_id, chat_id, ripr_menuPrec, message_id, chat_id, ripr_menuPrec,
MyForm = null) { MyForm = null) {
@@ -1015,6 +1022,13 @@ const MyTelegramBot = {
return {body: msg, title}; return {body: msg, title};
}, },
sendMsgTelegramToTheAdminAllSites: async function(text, senzaintestazione) {
for (const idapp of this.getAppTelegram()) {
text = text.replace('{appname}', tools.getNomeAppByIdApp(idapp));
await this.sendMsgTelegramToTheAdmin(idapp, text, senzaintestazione);
}
},
sendMsgTelegramToTheAdmin: async function(idapp, text, senzaintestazione) { sendMsgTelegramToTheAdmin: async function(idapp, text, senzaintestazione) {
const usersmanagers = await User.getusersManagers(idapp); const usersmanagers = await User.getusersManagers(idapp);
@@ -1709,6 +1723,7 @@ class Telegram {
} }
async isMenu(rec, msg, testo, notlast) { async isMenu(rec, msg, testo, notlast) {
try {
if (!msg) if (!msg)
return; return;
@@ -1755,6 +1770,36 @@ class Telegram {
await this.menumsgStaff(msg); await this.menumsgStaff(msg);
} else if (testo === Menu.MSGPAOLO) { } else if (testo === Menu.MSGPAOLO) {
await this.menumsgPaolo(msg); await this.menumsgPaolo(msg);
} else if (cmd1 === Menu.EXECSH) {
if (rec.user && cmd2) {
let isAdmin = rec.user ? rec.user.profile.manage_telegram : false;
if (isAdmin) {
const ris = await tools.execScript(this.idapp, msg, cmd2, 'Eseguo lo script');
}
}
} else if (cmd1 === Menu.LOG_SRV) {
if (rec.user) {
let isAdmin = rec.user ? rec.user.profile.manage_telegram : false;
if (isAdmin) {
await this.menuLogSrv(rec, msg, cmd2);
}
}
} else if (cmd1 === Menu.REBOOT_SRV) {
if (rec.user) {
let isAdmin = rec.user ? rec.user.profile.manage_telegram : false;
if (isAdmin) {
await this.menuRebootSrv(rec, msg, cmd2);
}
}
} else if (cmd1 === Menu.RESTART_SRV) {
if (rec.user) {
let isAdmin = rec.user ? rec.user.profile.manage_telegram : false;
if (isAdmin) {
await this.menuRestartSrv(rec, msg, cmd2);
}
}
// } else if (testo === Menu.MSG_NO_7_REQ) { // } else if (testo === Menu.MSG_NO_7_REQ) {
// await this.menumsg_No_7_Req(msg); // await this.menumsg_No_7_Req(msg);
// } else if (testo === Menu.MSG_NO_9_REQ) { // } else if (testo === Menu.MSG_NO_9_REQ) {
@@ -1780,6 +1825,9 @@ class Telegram {
} else { } else {
await this.isMenuNotVerified(rec, msg, notlast); await this.isMenuNotVerified(rec, msg, notlast);
} }
} catch (e) {
console.error('isMenu', e);
}
} }
getsymb(symb, valid, isnum, num, smileif) { getsymb(symb, valid, isnum, num, smileif) {
@@ -2167,6 +2215,42 @@ class Telegram {
await this.menumsgGenerico(msg, Destin.PAOLO); await this.menumsgGenerico(msg, Destin.PAOLO);
} }
nonAbilitato(msg) {
MyTelegramBot.local_sendMsgTelegramByIdTelegram(this.idapp, msg.chat.id, 'Utente Non abilitato o codice non valido ad eseguire questa funzione');
}
chisono(rec) {
if (rec && rec.user) {
return 'Eseguito da ' + rec.user.username + ': ';
}
}
async menuRestartSrv(rec, msg, cmd2) {
if (cmd2 === '6711') {
await MyTelegramBot.sendMsgTelegramToTheAdminAllSites(this.chisono(rec) + ' ha rilanciato il Server NODE.JS...');
const ris = await tools.execScript(this.idapp, msg, '~/batch/production_restart_server.sh', this.chisono(rec) + ' Restart il Server (Node.Js)');
} else {
this.nonAbilitato(msg)
}
}
async menuLogSrv(rec, msg, cmd2) {
if (cmd2 === '6711') {
const ris = await tools.execScript(this.idapp, msg, '~/batch/logserver.sh', this.chisono(rec) + ' Visualizzo il Log del Server...');
} else {
this.nonAbilitato(msg)
}
}
async menuRebootSrv(rec, msg, cmd2) {
if (cmd2 === '6711') {
await MyTelegramBot.sendMsgTelegramToTheAdminAllSites(this.chisono(rec) + ' ha effettuato il Reboot del Server! ...');
const ris = await tools.execScript(this.idapp, msg, '~/batch/esegui_reboot.sh', this.chisono(rec) + ' Eseguo il Reboot del Server !...');
} else {
this.nonAbilitato(msg)
}
}
async menumsg_No_7_Req(msg) { async menumsg_No_7_Req(msg) {
await this.menumsgGenerico(msg, Destin.NO_7_REQ); await this.menumsgGenerico(msg, Destin.NO_7_REQ);
} }
@@ -2986,8 +3070,8 @@ class Telegram {
// Check if you are Admin // Check if you are Admin
const user = await User.UserByIdTelegram(idapp, id); const user = await User.UserByIdTelegram(idapp, id);
let isAdmin = user ? user.profile.manage_telegram && user.username === // let isAdmin = user ? user.profile.manage_telegram && user.username === 'paoloar77' : false;
'paoloar77' : false; let isAdmin = user ? user.profile.manage_telegram: false;
const isManager = user ? user.profile.manage_telegram : false; const isManager = user ? user.profile.manage_telegram : false;
const isVerified = user ? user.profile.teleg_id > 0 && user.verified_by_aportador : false; const isVerified = user ? user.profile.teleg_id > 0 && user.verified_by_aportador : false;
@@ -3196,6 +3280,7 @@ class Telegram {
// text = text.replace(/<\/div>/g, ''); // text = text.replace(/<\/div>/g, '');
text = text.replace(/&nbsp;/g, ' '); text = text.replace(/&nbsp;/g, ' ');
if (!form) { if (!form) {
form = { form = {
'parse_mode': 'HTML', 'parse_mode': 'HTML',
@@ -3248,7 +3333,7 @@ class Telegram {
async sendMsg(id, text, menu, form, msg_id, chat_id, ripr_menuPrec) { async sendMsg(id, text, menu, form, msg_id, chat_id, ripr_menuPrec) {
if (!id) if (!id || !text)
return false; return false;
try { try {

View File

@@ -2711,6 +2711,7 @@ module.exports = {
convertSpecialTags(user, msg) { convertSpecialTags(user, msg) {
try { try {
if (!!user) { if (!!user) {
msg = msg.replace('{appname}', this.getNomeAppByIdApp(user.idapp));
msg = msg.replace('{username}', user.username); msg = msg.replace('{username}', user.username);
msg = msg.replace('{name}', user.name ? user.name : user.username); msg = msg.replace('{name}', user.name ? user.name : user.username);
msg = msg.replace('{surname}', user.surname ? user.surname : ''); msg = msg.replace('{surname}', user.surname ? user.surname : '');
@@ -2774,4 +2775,31 @@ module.exports = {
return strlinkreg; return strlinkreg;
}, },
execScript: function(idapp, msg, script, testo) {
const { exec } = require("child_process");
const telegrambot = require('../telegram/telegrambot');
const idchat = msg.chat.id;
console.log(testo + ' (' + script + ')');
telegrambot.local_sendMsgTelegramByIdTelegram(idapp, idchat, testo);
exec(script, (error, stdout, stderr) => {
if (error) {
telegrambot.local_sendMsgTelegramByIdTelegram(idapp, idchat, 'ERROR: ' + error.message);
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
telegrambot.local_sendMsgTelegramByIdTelegram(idapp, idchat, 'ERROR: ' + stderr);
console.log(`stderr: ${stderr}`);
return;
}
// console.log(` ... stdout: ${stdout}`);
telegrambot.local_sendMsgTelegramByIdTelegram(idapp, idchat, stdout);
});
}
}; };