import { Telegraf, Markup } from 'telegraf'; import dotenv from 'dotenv'; dotenv.config(); const BOT_TOKEN = process.env.BOT_TOKEN; if (!BOT_TOKEN) { console.error('Error: BOT_TOKEN environment variable is not set'); process.exit(1); } const bot = new Telegraf(BOT_TOKEN); // Log startup success console.log('Keyboard Reset Bot started successfully'); // Handle /resetkeyboard command bot.command('resetkeyboard', async(ctx) => { const chatId = ctx.chat.id; const userId = ctx.from.id; const chatType = ctx.chat.type; // Log command usage console.log(`Command received - Chat ID: ${chatId}, User ID: ${userId}, Chat Type: ${chatType}`); // Check if chat is a group or supergroup if (chatType !== 'group' && chatType !== 'supergroup') { await ctx.reply('This bot only works in groups.'); return; } try { // Check if user is an administrator const member = await ctx.getChatMember(userId); const isAdmin = member.status === 'administrator' || member.status === 'creator'; if (!isAdmin) { // Log authorization failure at debug level console.log(`Authorization failed - Chat ID: ${chatId}, User ID: ${userId}`); await ctx.reply('Only group admins can use this command.'); return; } // Send keyboard reset message const sentMessage = await ctx.reply('Keyboard reset.', { reply_markup: Markup.removeKeyboard() }); // Log successful reset console.log(`Keyboard reset successful - Chat ID: ${chatId}, User ID: ${userId}`); // Delete the confirmation message after 7 seconds setTimeout(async() => { try { await ctx.deleteMessage(sentMessage.message_id); } catch (error) { // Silently continue if deletion fails (permissions, race condition, etc.) // This is expected behavior - do not log or spam } }, 7000); } catch (error) { // Fail silently on Telegram API errors (do not spam the group) // Only log to console for debugging console.error('Error processing command:', error.message); } }); // Handle errors bot.catch((err, ctx) => { console.error('Bot error:', err); }); // Start polling bot.launch().then(() => { console.log('Bot is polling for updates...'); }).catch((error) => { console.error('Failed to start bot:', error); process.exit(1); }); // Enable graceful stop process.once('SIGINT', () => bot.stop('SIGINT')); process.once('SIGTERM', () => bot.stop('SIGTERM'));