Files
mijn-docker-stacks/uptime-bot/bot.js
T

158 lines
4.3 KiB
JavaScript

const { driver } = require('@rocket.chat/sdk');
const axios = require('axios');
// Configuratie - Pas deze aan naar jouw situatie
const config = {
host: process.env.ROCKETCHAT_URL || 'localhost:3000',
useSsl: process.env.ROCKETCHAT_USE_SSL === 'true' || false,
username: process.env.BOT_USERNAME || 'uptime.bot',
password: process.env.BOT_PASSWORD || 'changeme',
uptimeKumaUrl: process.env.UPTIME_KUMA_URL || 'http://uptime-kuma:3001'
};
const BOT_NAME = 'uptime.bot';
// Hulpfunctie voor bot responses
async function sendMessage(roomId, message) {
await driver.sendToRoomId(message, roomId);
}
// Functie om Uptime Kuma status op te halen
async function getUptimeStatus() {
try {
// Note: Uptime Kuma heeft een API maar vereist authenticatie
// Dit is een basis voorbeeld - je moet mogelijk een API key toevoegen
const response = await axios.get(`${config.uptimeKumaUrl}/api/status-page/heartbeat`);
return response.data;
} catch (error) {
console.log('Kon geen verbinding maken met Uptime Kuma API');
return null;
}
}
// Commando handler
async function handleCommand(message, roomId) {
const text = message.msg.toLowerCase().trim();
// Help commando
if (text === '!help' || text.includes('help')) {
await sendMessage(roomId,
`🤖 **Uptime Bot Commands**\n` +
`\`!help\` - Toon deze help\n` +
`\`!status\` - Toon monitoring status\n` +
`\`!ping\` - Check of bot online is\n` +
`\`!uptime\` - Toon uptime informatie`
);
return;
}
// Ping commando
if (text === '!ping') {
await sendMessage(roomId, '🏓 Pong! Bot is online en actief!');
return;
}
// Status commando
if (text === '!status') {
await sendMessage(roomId,
`✅ **Monitoring Status**\n` +
`Bot is actief en monitoring services.\n` +
`Uptime Kuma URL: ${config.uptimeKumaUrl}\n` +
`Gebruik \`!uptime\` voor gedetailleerde informatie.`
);
return;
}
// Uptime commando
if (text === '!uptime') {
const uptime = process.uptime();
const days = Math.floor(uptime / 86400);
const hours = Math.floor((uptime % 86400) / 3600);
const minutes = Math.floor((uptime % 3600) / 60);
await sendMessage(roomId,
`📊 **Uptime Bot Statistics**\n` +
`Bot uptime: ${days}d ${hours}h ${minutes}m\n` +
`Status: 🟢 Online\n` +
`Monitoring: Actief`
);
return;
}
// Als iemand de bot noemt
if (text.includes('@uptime.bot') || text.includes('uptime.bot')) {
await sendMessage(roomId,
`👋 Hoi! Ik ben de Uptime Bot.\n` +
`Type \`!help\` voor een lijst met commando's.`
);
return;
}
}
// Main bot functie
async function main() {
try {
console.log('🚀 Starting Uptime Bot...');
console.log(`Connecting to Rocket.Chat at ${config.host}...`);
// Verbind met Rocket.Chat
await driver.connect({
host: config.host,
useSsl: config.useSsl
});
console.log('✅ Connected to Rocket.Chat');
// Login
await driver.login({
username: config.username,
password: config.password
});
console.log(`✅ Logged in as ${config.username}`);
// Subscribe naar alle rooms waar de bot in zit
await driver.subscribeToMessages();
console.log('✅ Subscribed to messages');
console.log('🤖 Bot is nu online en luistert naar berichten!');
// Luister naar berichten
await driver.reactToMessages(async (err, message, messageOptions) => {
if (err) {
console.error('Error:', err);
return;
}
// Negeer berichten van de bot zelf
if (!message || message.u.username === config.username) {
return;
}
// Log inkomende berichten
console.log(`📨 Bericht ontvangen van ${message.u.username}: ${message.msg}`);
// Verwerk het commando
await handleCommand(message, message.rid);
});
} catch (error) {
console.error('❌ Error starting bot:', error);
setTimeout(main, 5000); // Probeer opnieuw na 5 seconden
}
}
// Error handling
process.on('unhandledRejection', (error) => {
console.error('Unhandled rejection:', error);
});
process.on('SIGINT', async () => {
console.log('\n👋 Shutting down bot...');
await driver.disconnect();
process.exit(0);
});
// Start de bot
main().catch(console.error);