diff --git a/src/server/server.js b/src/server/server.js index 8072b11..f52ea90 100755 --- a/src/server/server.js +++ b/src/server/server.js @@ -392,23 +392,25 @@ async function resetProcessingJob() { async function inizia() { - if (process.env.NODE_ENV === 'development') { - await telegrambot.sendMsgTelegram(tools.FREEPLANET, - telegrambot.ADMIN_USER_SERVER, - `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}!`); + try { - await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET, - telegrambot.ADMIN_IDTELEGRAM_SERVER, - `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + - `๐Ÿ”… Il Server ${process.env.DATABASE} รจ appena ripartito!`); + if (process.env.NODE_ENV === 'development') { + await telegrambot.sendMsgTelegram(tools.FREEPLANET, + telegrambot.ADMIN_USER_SERVER, + `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}!`); - } else { + await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET, + telegrambot.ADMIN_IDTELEGRAM_SERVER, + `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + + `๐Ÿ”… Il Server ${process.env.DATABASE} รจ appena ripartito!`); + + } else { + + await telegrambot.sendMsgTelegramToTheAdminAllSites(`Ciao Admin\n` + `๐Ÿ”…๐Ÿ”…๐Ÿ”… Il Server col BOT di {appname} รจ appena ripartito!`, false); + + } + } catch (e) { - // await telegrambot.sendMsgTelegram(tools.FREEPLANET, telegrambot.ADMIN_USER_SERVER, `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}!`); - await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET, - telegrambot.ADMIN_IDTELEGRAM_SERVER, - `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + - `๐Ÿ”… Il Server ${process.env.DATABASE} รจ appena ripartito!`); } } @@ -530,6 +532,10 @@ async function faitest() { // const $vers = tools.getVersionint('1.92.45'); + if (true) { + // tools.execScript("ls -la"); + } + if (false) { const prova = tools.getConfSiteOptionEnabledByIdApp('13', shared_consts.ConfSite.Notif_Reg_Push_Admin); console.log('prova', prova); diff --git a/src/server/telegram/telegrambot.js b/src/server/telegram/telegrambot.js index a4cb8c5..91e1868 100755 --- a/src/server/telegram/telegrambot.js +++ b/src/server/telegram/telegrambot.js @@ -268,6 +268,10 @@ const Menu = { 'Inv e NO 7 Req', MSGSTAFF: emoji.get('incoming_envelope') + ' Invia a STAFF', 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', it: { ACCEDI: emo.PERSON + ' Accedi', @@ -404,9 +408,8 @@ const MenuYesNo = { const MenuAdmin = { it: { - menu: [ - [Menu.MSGSTAFF, Menu.MSGATUTTI, Menu.it.INDIETRO], - [Menu.MSGPAOLO]], + menu: [[Menu.RESTART_SRV + ' 6711', Menu.LOG_SRV + ' 6711'], + [Menu.REBOOT_SRV+ ' 6711', Menu.it.INDIETRO]], }, }; @@ -677,6 +680,10 @@ const MyTelegramBot = { ISCRIZIONE_CONACREIS: 2, }, + getAppTelegram: function() { + return appTelegram; + }, + local_sendMsgTelegramByIdTelegram: async function(idapp, idtelegram, text, message_id, chat_id, ripr_menuPrec, MyForm = null) { @@ -1015,6 +1022,13 @@ const MyTelegramBot = { 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) { const usersmanagers = await User.getusersManagers(idapp); @@ -1709,76 +1723,110 @@ class Telegram { } async isMenu(rec, msg, testo, notlast) { - if (!msg) - return; + try { + if (!msg) + return; - if (testo === undefined) - return; + if (testo === undefined) + return; - const arrtext = testo.split(' '); - let cmd2 = ''; - let cmd1 = arrtext[0]; - if (arrtext.length > 1) - cmd2 = arrtext[1]; + const arrtext = testo.split(' '); + let cmd2 = ''; + let cmd1 = arrtext[0]; + if (arrtext.length > 1) + cmd2 = arrtext[1]; - let oldusername = rec.msgall_username_specifico; + let oldusername = rec.msgall_username_specifico; - if ((this.isSelMenu(msg, testo, 'ESCI_DA_CHAT')) || - (this.isSelMenu(msg, testo, 'INDIETRO'))) { - rec.msgall_username_specifico = ''; - } else { - if (rec.msgall_username_specifico !== '') { - await this.SendMsgToUser(msg, rec, rec.msgall_username_specifico, - testo); - return true; + if ((this.isSelMenu(msg, testo, 'ESCI_DA_CHAT')) || + (this.isSelMenu(msg, testo, 'INDIETRO'))) { + rec.msgall_username_specifico = ''; + } else { + if (rec.msgall_username_specifico !== '') { + await this.SendMsgToUser(msg, rec, rec.msgall_username_specifico, + testo); + return true; + } } - } - if (this.isSelMenu(msg, testo, 'LAVAGNA') || - MsgBot.LAVAGNA.find((rec) => testo.indexOf(rec) > -1)) { - await this.menuLavagna(msg); - } else if (this.isSelMenu(msg, testo, 'ACCEDI')) { - await this.menuAccedi(msg); - } else if (this.isSelMenu(msg, testo, 'LINK_CONDIVIDERE')) { - await this.menuLinkCondividere(msg); - } else if (testo === Menu.EXIT_TELEGRAM) { - await this.menuExitToTelegram(msg); - } else if (testo === Menu.ADMIN) { - await this.menuAdmin(msg); - } else if (testo === Menu.AIUTO) { - await this.menuAiuto(msg); - } else if (testo === Menu.LANG) { - await this.menuLang(msg); - } else if (testo === Menu.MSGATUTTI) { - await this.menumsgAll(msg); - } else if (testo === Menu.MSGSTAFF) { - await this.menumsgStaff(msg); - } else if (testo === Menu.MSGPAOLO) { - await this.menumsgPaolo(msg); - // } else if (testo === Menu.MSG_NO_7_REQ) { - // await this.menumsg_No_7_Req(msg); - // } else if (testo === Menu.MSG_NO_9_REQ) { - // await this.menumsg_No_9_Req(msg); - // } else if (testo === Menu.NESSUN_IMBARCO_7REQ) { - // await this.menumsgGenerico(msg, Destin.NESSUN_IMBARCO_7REQ); - // } else if (cmd1 === Menu.MSG_TO_NAVE) { - // await this.menumsg_to_Nave(msg, cmd2); - // } else if (testo === Menu.MSG_SI_INVITATI_NO_7REQ_INVITATI) { - // await this.menumsg_Si_Invitati_No_7Req(msg); - } else if (cmd1.toLowerCase() === Menu.MSG_TO_USER) { - await this.menumsg_A_Utente(msg); - } else if (testo === Menu.SETPICPROFILE) { - await this.setPhotoProfile(rec.user, msg.from.id); - } else if (testo === Menu.RESETPWD) { - await this.setResetPwd(rec, msg); - } else if (this.isSelMenu(msg, testo, 'INDIETRO') || - (testo === Menu.it.INDIETRO)) { - await this.msgScegliMenu(msg); - } else if (this.isSelMenu(msg, testo, 'ESCI_DA_CHAT')) { - await this.sendMsg(msg.chat.id, 'Uscito dalla Chat con ' + oldusername); - rec.msgall_username_specifico = ''; - } else { - await this.isMenuNotVerified(rec, msg, notlast); + if (this.isSelMenu(msg, testo, 'LAVAGNA') || + MsgBot.LAVAGNA.find((rec) => testo.indexOf(rec) > -1)) { + await this.menuLavagna(msg); + } else if (this.isSelMenu(msg, testo, 'ACCEDI')) { + await this.menuAccedi(msg); + } else if (this.isSelMenu(msg, testo, 'LINK_CONDIVIDERE')) { + await this.menuLinkCondividere(msg); + } else if (testo === Menu.EXIT_TELEGRAM) { + await this.menuExitToTelegram(msg); + } else if (testo === Menu.ADMIN) { + await this.menuAdmin(msg); + } else if (testo === Menu.AIUTO) { + await this.menuAiuto(msg); + } else if (testo === Menu.LANG) { + await this.menuLang(msg); + } else if (testo === Menu.MSGATUTTI) { + await this.menumsgAll(msg); + } else if (testo === Menu.MSGSTAFF) { + await this.menumsgStaff(msg); + } else if (testo === Menu.MSGPAOLO) { + 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) { + // await this.menumsg_No_7_Req(msg); + // } else if (testo === Menu.MSG_NO_9_REQ) { + // await this.menumsg_No_9_Req(msg); + // } else if (testo === Menu.NESSUN_IMBARCO_7REQ) { + // await this.menumsgGenerico(msg, Destin.NESSUN_IMBARCO_7REQ); + // } else if (cmd1 === Menu.MSG_TO_NAVE) { + // await this.menumsg_to_Nave(msg, cmd2); + // } else if (testo === Menu.MSG_SI_INVITATI_NO_7REQ_INVITATI) { + // await this.menumsg_Si_Invitati_No_7Req(msg); + } else if (cmd1.toLowerCase() === Menu.MSG_TO_USER) { + await this.menumsg_A_Utente(msg); + } else if (testo === Menu.SETPICPROFILE) { + await this.setPhotoProfile(rec.user, msg.from.id); + } else if (testo === Menu.RESETPWD) { + await this.setResetPwd(rec, msg); + } else if (this.isSelMenu(msg, testo, 'INDIETRO') || + (testo === Menu.it.INDIETRO)) { + await this.msgScegliMenu(msg); + } else if (this.isSelMenu(msg, testo, 'ESCI_DA_CHAT')) { + await this.sendMsg(msg.chat.id, 'Uscito dalla Chat con ' + oldusername); + rec.msgall_username_specifico = ''; + } else { + await this.isMenuNotVerified(rec, msg, notlast); + } + } catch (e) { + console.error('isMenu', e); } } @@ -2167,6 +2215,42 @@ class Telegram { 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) { await this.menumsgGenerico(msg, Destin.NO_7_REQ); } @@ -2986,8 +3070,8 @@ class Telegram { // Check if you are Admin const user = await User.UserByIdTelegram(idapp, id); - let isAdmin = user ? user.profile.manage_telegram && user.username === - 'paoloar77' : false; + // let isAdmin = user ? user.profile.manage_telegram && user.username === 'paoloar77' : false; + let isAdmin = 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; @@ -3196,6 +3280,7 @@ class Telegram { // text = text.replace(/<\/div>/g, ''); text = text.replace(/ /g, ' '); + if (!form) { form = { 'parse_mode': 'HTML', @@ -3248,7 +3333,7 @@ class Telegram { async sendMsg(id, text, menu, form, msg_id, chat_id, ripr_menuPrec) { - if (!id) + if (!id || !text) return false; try { diff --git a/src/server/tools/general.js b/src/server/tools/general.js index f1327d8..22cd7de 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -2711,6 +2711,7 @@ module.exports = { convertSpecialTags(user, msg) { try { if (!!user) { + msg = msg.replace('{appname}', this.getNomeAppByIdApp(user.idapp)); msg = msg.replace('{username}', user.username); msg = msg.replace('{name}', user.name ? user.name : user.username); msg = msg.replace('{surname}', user.surname ? user.surname : ''); @@ -2774,4 +2775,31 @@ module.exports = { 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); + }); + } + };