289 lines
9.7 KiB
JavaScript
289 lines
9.7 KiB
JavaScript
|
const TelegramBot = require('node-telegram-bot-api');
|
||
|
const ping = require('ping');
|
||
|
const os = require('os');
|
||
|
const fs = require('fs');
|
||
|
const yaml = require('js-yaml');
|
||
|
|
||
|
const token = '6409908881:AAGoE35Ohy1TIKljSb7Y0gaDWhdR23Z_Pl4';
|
||
|
const bot = new TelegramBot(token, { polling: true });
|
||
|
|
||
|
let userServerConfigurations = loadConfigurations();
|
||
|
const chatIds = new Set(); // Set für die Chat-IDs
|
||
|
|
||
|
function loadConfigurations() {
|
||
|
try {
|
||
|
if (fs.existsSync('configurations.yml')) {
|
||
|
const data = fs.readFileSync('configurations.yml', 'utf8');
|
||
|
return yaml.load(data) || {};
|
||
|
} else {
|
||
|
saveConfigurations({});
|
||
|
return {};
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Fehler beim Laden der Konfigurationen:', error.message);
|
||
|
return {};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function saveConfigurations(configurations) {
|
||
|
try {
|
||
|
const yamlString = yaml.dump(configurations);
|
||
|
fs.writeFileSync('configurations.yml', yamlString, 'utf8');
|
||
|
} catch (error) {
|
||
|
console.error('Fehler beim Speichern der Konfigurationen:', error.message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function getCpuUsage() {
|
||
|
return Math.floor(Math.random() * 101);
|
||
|
}
|
||
|
|
||
|
function getRamUsage() {
|
||
|
return Math.floor(Math.random() * 101);
|
||
|
}
|
||
|
|
||
|
function getNetworkTraffic() {
|
||
|
return Math.floor(Math.random() * 101);
|
||
|
}
|
||
|
|
||
|
async function getUptime() {
|
||
|
const uptimeInSeconds = os.uptime();
|
||
|
const days = Math.floor(uptimeInSeconds / (24 * 3600));
|
||
|
const hours = Math.floor((uptimeInSeconds % (24 * 3600)) / 3600);
|
||
|
const minutes = Math.floor((uptimeInSeconds % 3600) / 60);
|
||
|
return `${days} Tage, ${hours} Stunden, ${minutes} Minuten`;
|
||
|
}
|
||
|
|
||
|
async function checkServerStatus(serverAddress) {
|
||
|
try {
|
||
|
const result = await ping.promise.probe(serverAddress);
|
||
|
return result.alive ? 'Online' : 'Offline';
|
||
|
} catch (error) {
|
||
|
console.error(`Fehler beim Überprüfen des Servers ${serverAddress}: ${error.message}`);
|
||
|
return 'Fehler';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const usersAddingServer = {};
|
||
|
|
||
|
const onlineIcon = '✅';
|
||
|
const offlineIcon = '❌';
|
||
|
const errorIcon = '⚠️';
|
||
|
const bannerEmoji = '🚀';
|
||
|
|
||
|
bot.onText(/\/start/, (msg) => {
|
||
|
const chatId = msg.chat.id;
|
||
|
chatIds.add(chatId); // Füge die Chat-ID zum Set hinzu
|
||
|
bot.sendMessage(chatId, 'Bot gestartet. Benutze /add_server, /delete_server, /status und /help für Serververwaltung.');
|
||
|
});
|
||
|
|
||
|
bot.onText(/\/add_server/, (msg) => {
|
||
|
const chatId = msg.chat.id;
|
||
|
|
||
|
bot.sendMessage(chatId, 'Füge einen neuen Server hinzu. Sende mir zuerst den Namen des Servers:');
|
||
|
|
||
|
usersAddingServer[chatId] = { name: null, address: null };
|
||
|
|
||
|
bot.once('text', (serverNameMsg) => {
|
||
|
usersAddingServer[chatId].name = serverNameMsg.text;
|
||
|
|
||
|
bot.sendMessage(chatId, `Servername ${bannerEmoji}${serverNameMsg.text}${bannerEmoji} wurde hinzugefügt. Jetzt sende mir die IP-Adresse des Servers:`);
|
||
|
|
||
|
bot.once('text', (serverAddressMsg) => {
|
||
|
usersAddingServer[chatId].address = serverAddressMsg.text;
|
||
|
|
||
|
userServerConfigurations[chatId] = userServerConfigurations[chatId] || [];
|
||
|
userServerConfigurations[chatId].push(usersAddingServer[chatId]);
|
||
|
|
||
|
saveConfigurations(userServerConfigurations);
|
||
|
|
||
|
bot.sendMessage(chatId, `Server ${bannerEmoji}${usersAddingServer[chatId].name}${bannerEmoji} mit Adresse "${usersAddingServer[chatId].address}" wurde hinzugefügt.`);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
bot.onText(/\/delete_server/, (msg) => {
|
||
|
const chatId = msg.chat.id;
|
||
|
const userServers = userServerConfigurations[chatId] || [];
|
||
|
|
||
|
if (userServers.length === 0) {
|
||
|
bot.sendMessage(chatId, 'Du hast noch keine Server hinzugefügt. Benutze /add_server, um einen Server hinzuzufügen.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const keyboard = {
|
||
|
reply_markup: {
|
||
|
inline_keyboard: userServers.map(server => [{
|
||
|
text: `${server.name} - ${server.address}`,
|
||
|
callback_data: `delete:${server.name}:${server.address}`
|
||
|
}])
|
||
|
}
|
||
|
};
|
||
|
|
||
|
bot.sendMessage(chatId, 'Wähle einen Server zum Löschen aus:', keyboard);
|
||
|
});
|
||
|
|
||
|
bot.on('callback_query', (query) => {
|
||
|
const chatId = query.message.chat.id;
|
||
|
const [action, serverName, serverAddress] = query.data.split(':');
|
||
|
|
||
|
if (action === 'delete') {
|
||
|
userServerConfigurations[chatId] = (userServerConfigurations[chatId] || []).filter(server => !(server.name === serverName && server.address === serverAddress));
|
||
|
|
||
|
saveConfigurations(userServerConfigurations);
|
||
|
|
||
|
bot.answerCallbackQuery(query.id, `Server ${bannerEmoji}${serverName}${bannerEmoji} mit Adresse "${serverAddress}" wurde gelöscht.`);
|
||
|
} else if (action === 'details') {
|
||
|
showServerDetails(chatId, serverName, serverAddress);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
bot.onText(/\/status/, async (msg) => {
|
||
|
const chatId = msg.chat.id;
|
||
|
const userServers = userServerConfigurations[chatId] || [];
|
||
|
|
||
|
if (userServers.length === 0) {
|
||
|
bot.sendMessage(chatId, 'Du hast noch keine Server hinzugefügt. Benutze /add_server, um einen Server hinzuzufügen.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
let statusMessage = 'Server Status:\n';
|
||
|
|
||
|
for (const serverConfig of userServers) {
|
||
|
const serverName = serverConfig.name;
|
||
|
const serverAddress = serverConfig.address;
|
||
|
|
||
|
try {
|
||
|
const serverStatus = await checkServerStatus(serverAddress);
|
||
|
|
||
|
const statusIcon = serverStatus === 'Online' ? '✅' : serverStatus === 'Offline' ? '❌' : '⚠️';
|
||
|
|
||
|
statusMessage += `
|
||
|
*Server:* ${bannerEmoji}${serverName}${bannerEmoji} ${statusIcon} ${serverStatus}
|
||
|
`;
|
||
|
} catch (error) {
|
||
|
console.error(`Fehler beim Überprüfen des Servers ${serverName}: ${error.message}`);
|
||
|
statusMessage += `Fehler beim Überprüfen des Servers ${bannerEmoji}${serverName}${bannerEmoji}. ${errorIcon}\n`;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const options = {
|
||
|
parse_mode: 'Markdown',
|
||
|
reply_markup: {
|
||
|
inline_keyboard: [
|
||
|
[{
|
||
|
text: 'Details',
|
||
|
callback_data: 'details'
|
||
|
}]
|
||
|
]
|
||
|
}
|
||
|
};
|
||
|
|
||
|
bot.sendMessage(chatId, statusMessage, options);
|
||
|
});
|
||
|
|
||
|
async function showServerDetails(chatId, serverName, serverAddress) {
|
||
|
const detailsMessage = `
|
||
|
*Details für Server ${bannerEmoji}${serverName}${bannerEmoji}*
|
||
|
*Uptime:* ${await getUptime()}
|
||
|
*CPU-Auslastung:* ${getCpuUsage()}%
|
||
|
*RAM-Auslastung:* ${getRamUsage()}%
|
||
|
*Netzwerk-Traffic:* ${getNetworkTraffic()}%
|
||
|
*IP/Adresse:* ${serverAddress}
|
||
|
`;
|
||
|
|
||
|
const options = {
|
||
|
parse_mode: 'Markdown',
|
||
|
};
|
||
|
|
||
|
bot.sendMessage(chatId, detailsMessage, options);
|
||
|
}
|
||
|
|
||
|
bot.on('callback_query', async (query) => {
|
||
|
const chatId = query.message.chat.id;
|
||
|
const [action, serverName, serverAddress] = query.data.split(':');
|
||
|
|
||
|
if (action === 'details') {
|
||
|
const userServers = userServerConfigurations[chatId] || [];
|
||
|
|
||
|
if (userServers.length === 0) {
|
||
|
bot.sendMessage(chatId, 'Du hast noch keine Server hinzugefügt. Benutze /add_server, um einen Server hinzuzufügen.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const keyboard = {
|
||
|
reply_markup: {
|
||
|
inline_keyboard: userServers.map(server => [{
|
||
|
text: `${server.name}} - ${server.address}`,
|
||
|
callback_data: `details:${server.name}:${server.address}`
|
||
|
}])
|
||
|
}
|
||
|
};
|
||
|
|
||
|
bot.sendMessage(chatId, 'Wähle einen Server für Details aus:', keyboard);
|
||
|
} else if (action === 'details') {
|
||
|
showServerDetails(chatId, serverName);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
bot.onText(/\/help/, (msg) => {
|
||
|
const chatId = msg.chat.id;
|
||
|
const helpMessage = `
|
||
|
Verfügbare Befehle:
|
||
|
- /add_server: Fügt einen neuen Server hinzu.
|
||
|
- /delete_server: Löscht einen vorhandenen Server.
|
||
|
- /status: Zeigt den Status der konfigurierten Server an.
|
||
|
- /help: Zeigt diese Hilfe an.
|
||
|
`;
|
||
|
bot.sendMessage(chatId, helpMessage);
|
||
|
});
|
||
|
|
||
|
// Intervall für die Serverüberprüfung
|
||
|
const checkIntervalMinutes = 2;
|
||
|
const checkIntervalMilliseconds = checkIntervalMinutes * 60 * 1000;
|
||
|
|
||
|
setInterval(async () => {
|
||
|
await checkAndSendServerStatus();
|
||
|
}, checkIntervalMilliseconds);
|
||
|
|
||
|
async function checkAndSendServerStatus() {
|
||
|
// Iteriere über alle gespeicherten Chat-IDs
|
||
|
for (const chatId of chatIds) {
|
||
|
const userServers = userServerConfigurations[chatId] || [];
|
||
|
|
||
|
if (userServers.length === 0) {
|
||
|
bot.sendMessage(chatId, 'Du hast noch keine Server hinzugefügt. Benutze /add_server, um einen Server hinzuzufügen.');
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
let statusMessage = 'Automatischer Statusbericht:\n';
|
||
|
|
||
|
for (const serverConfig of userServers) {
|
||
|
const serverName = serverConfig.name;
|
||
|
const serverAddress = serverConfig.address;
|
||
|
|
||
|
try {
|
||
|
const serverStatus = await checkServerStatus(serverAddress);
|
||
|
|
||
|
if (serverStatus === 'Offline') {
|
||
|
const statusIcon = '❌';
|
||
|
statusMessage += `
|
||
|
*Server:* ${bannerEmoji}${serverName}${bannerEmoji} ${statusIcon} ${serverStatus}
|
||
|
`;
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error(`Fehler beim Überprüfen des Servers ${serverName}: ${error.message}`);
|
||
|
statusMessage += `Fehler beim Überprüfen des Servers ${bannerEmoji}${serverName}${bannerEmoji}. ⚠️\n`;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (statusMessage.includes('❌')) {
|
||
|
const options = {
|
||
|
parse_mode: 'Markdown',
|
||
|
};
|
||
|
|
||
|
bot.sendMessage(chatId, statusMessage, options);
|
||
|
}
|
||
|
}
|
||
|
}
|