support_bot.js aktualisiert

This commit is contained in:
M_Viper 2024-01-17 15:20:39 +01:00
parent 38c244a1fa
commit 611e101483
1 changed files with 97 additions and 77 deletions

View File

@ -13,11 +13,9 @@ const emailConfig = {
};
const botToken = process.env.BOT_TOKEN;
const ADMIN_ID = process.env.ADMIN_ID;
const adminChatId = process.env.ADMIN_CHAT_ID;
// Lade die erlaubten Admin-IDs aus der .env-Datei
const allowedAdminIds = ADMIN_ID.split(',');
const allowedAdminIds = adminChatId.split(',');
const botInfo = {
name: 'Telegram Support Bot',
@ -32,7 +30,6 @@ const ticketCreationStatus = new Map();
const logoPath = path.join(__dirname, 'logo.png');
function sendErrorNotification(error) {
try {
const adminChatId = 5507179337;
@ -46,20 +43,36 @@ function sendErrorNotification(error) {
}
}
async function checkBotStatus(chatId) {
async function checkBotStatus(msg) {
try {
const isBotOnline = true;
console.log('Received message:', msg);
// Überprüfe, ob der Befehl /status ausgeführt wurde
const isStatusCommand = msg && msg.text && msg.text.toLowerCase() === '/status';
console.log('Is /status command?', isStatusCommand);
const isBotOnline = true; // Hier sollte deine Logik stehen, um den Bot-Status zu überprüfen.
const statusMessage = isBotOnline ? 'Der Bot ist online.' : 'Der Bot ist offline.';
bot.sendMessage(chatId, statusMessage);
if (isStatusCommand) {
bot.sendMessage(msg.chat.id, statusMessage);
}
} catch (error) {
console.error('Fehler beim Überprüfen des Bot-Status:', error);
sendErrorNotification(error);
bot.sendMessage(chatId, 'Fehler beim Überprüfen des Bot-Status.');
if (msg && msg.text && msg.text.toLowerCase() === '/status') {
bot.sendMessage(msg.chat.id, 'Fehler beim Überprüfen des Bot-Status.');
}
}
}
async function checkWebsiteStatus(chatId) {
/* bot.onText(/\/status/, checkBotStatus);
async function checkWebsiteStatus() {
const adminChatId = process.env.ADMIN_CHAT_ID;
const websites = [
{
name: process.env.WEBSITE_1_NAME,
@ -75,18 +88,23 @@ async function checkWebsiteStatus(chatId) {
try {
const response = await axios.get(website.url);
const statusMessage = `✅ <b>${website.name}</b> ist <i>online</i>.`;
bot.sendMessage(chatId, statusMessage, { parse_mode: 'HTML' });
if (!website.online) {
website.online = true;
bot.sendMessage(adminChatId, statusMessage, { parse_mode: 'HTML' });
}
} catch (error) {
console.error(`Fehler beim Zugriff auf ${website.name}:`, error);
const errorMessage = `❌ <b>${website.name}</b> ist <i>offline</i>. Fehler: ${error.message}`;
if (website.online !== false) {
website.online = false;
sendErrorNotification(errorMessage);
bot.sendMessage(chatId, errorMessage, { parse_mode: 'HTML' });
bot.sendMessage(adminChatId, errorMessage, { parse_mode: 'HTML' });
}
}
}
}
setInterval(checkWebsiteStatus, 30000);
setInterval(checkWebsiteStatus, 3600000); */
async function loadFromYaml(filePath) {
try {
@ -132,9 +150,12 @@ async function loadFragen() {
}
}
function sendWelcomeMessage(chatId) {
function sendWelcomeMessage(chatId, isStatusCommand = false) {
const excludedCommands = ['/status', '/info', '/help', '/menu'];
if (!isStatusCommand && !excludedCommands.includes('/' + chatId)) {
const welcomeMessage = `
**Herzlich willkommen beim Support!**
Herzlich willkommen beim Support!
Wir freuen uns, dass Sie sich für unseren Support entschieden haben. Unser vorrangiges Ziel ist es, Ihnen bestmöglich zu helfen und Ihre Anliegen so schnell wie möglich zu klären. Falls Sie Unterstützung benötigen, bieten wir Ihnen verschiedene Optionen:
@ -154,6 +175,24 @@ function sendWelcomeMessage(chatId) {
bot.sendMessage(chatId, welcomeMessage, { parse_mode: 'Markdown' });
}
}
function handleStatusCommand(msg) {
const chatId = msg.chat.id;
const userId = msg.from.id;
if (allowedAdminIds.includes(userId.toString())) {
bot.sendMessage(chatId, 'Aktueller Bot- und Webseitenstatus wird abgerufen...');
checkBotStatus(chatId);
checkWebsiteStatus(chatId);
} else {
bot.sendMessage(chatId, 'Du hast keine Berechtigung, diesen Befehl zu verwenden.');
}
}
bot.onText(/\/status/, handleStatusCommand);
async function initializeBot() {
try {
@ -170,7 +209,6 @@ async function initializeBot() {
bot.on('message', async (msg) => {
const chatId = msg.chat.id;
// Überprüfe, ob die Nachricht "/help" ist, und sende die Hilfe-Nachricht
if (msg.text && msg.text.toLowerCase() === '/help') {
const helpMessage = `
🤖 ${botInfo.name} - Hilfe 🤖
@ -179,8 +217,8 @@ Verfügbare Befehle:
1. /menu - Zeigt das Hauptmenü an.
2. /help - Zeigt diese Hilfe an.
2. /info - Zeigt Informationen zum Bot an.
3. /status - Überprüft den aktuellen Bot- und Webseitenstatus. "Nur für Administrator"
3. /info - Zeigt Informationen zum Bot an.
🎫 Ticket erstellen: 🎫
@ -206,44 +244,27 @@ Verfügbare Befehle:
return;
}
// Überprüfe, ob es sich um den Befehl /menu handelt
if (msg.text === '/menu') {
handleMainMenu(msg, chatId, linksData, fragenData);
return;
}
// Überprüfe, ob es sich um den Befehl /info handelt
if (msg.text && msg.text.toLowerCase() === '/info') {
// Code für den Befehl /info
return;
}
// Überprüfe, ob sich der Benutzer im Ticketerstellungsstatus befindet
if (msg.text && msg.text.toLowerCase() === '/status') {
// Code für den Befehl /status
return;
}
if (ticketCreationStatus.has(chatId)) {
handleTicketCreation(msg, chatId, linksData);
return;
}
// Wenn keiner der speziellen Befehle erkannt wurde, sende die Willkommensnachricht
sendWelcomeMessage(chatId);
bot.onText(/\/status/, (msg) => {
const chatId = msg.chat.id;
const userId = msg.from.id;
// Prüfe, ob der Benutzer die Berechtigung hat, den /status-Befehl zu verwenden
if (allowedAdminIds.includes(userId.toString())) {
// Führe die Logik für den /status-Befehl aus
bot.sendMessage(chatId, 'Aktueller Bot- und Webseitenstatus wird abgerufen...');
// Hier kannst du die Logik für den Statusbefehl implementieren
// Z.B., rufe Funktionen auf, um den aktuellen Bot- und Webseitenstatus zu überprüfen
checkBotStatus(chatId);
checkWebsiteStatus(chatId);
} else {
bot.sendMessage(chatId, 'Du hast keine Berechtigung, diesen Befehl zu verwenden.');
}
});
});
bot.on('callback_query', (callbackQuery) => {
@ -288,7 +309,6 @@ Verfügbare Befehle:
`);
} catch (error) {
console.error('Fehler beim Initialisieren des Bots:', error);
sendErrorNotification(error);
}
}
@ -297,13 +317,13 @@ function showBotInfo(chatId) {
bot.sendPhoto(chatId, logoPath, { caption: '*Der Bot wurde am 08.01.2024 erstellt*' })
.then(() => {
const botInfoMessage = `
🤖Bot Information:🤖
🤖Bot Information:
- *Name:* ${escapeMarkdown(botInfo.name)}
- *Author:* ${escapeMarkdown(botInfo.author)}
- *License:* ${escapeMarkdown(botInfo.license)}
`;
return bot.sendMessage(chatId, botInfoMessage, { parse_mode: 'Markdown' });
bot.sendMessage(chatId, botInfoMessage, { parse_mode: 'Markdown' });
})
.then(() => {
console.log('Bot-Informationen erfolgreich gesendet.');