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'); // Debug: Log all incoming updates to see if bot is receiving messages bot.use(async (ctx, next) => { console.log('Update received:', { updateType: ctx.updateType, chatType: ctx.chat?.type, chatId: ctx.chat?.id, fromId: ctx.from?.id, text: ctx.message?.text || ctx.update.message?.text || 'N/A' }); return next(); }); // 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) { // Log errors for debugging console.error('Error processing command:', error); console.error('Error details:', { message: error.message, code: error.code, response: error.response }); // Try to reply with error (but don't spam if it fails) try { await ctx.reply('An error occurred while processing the command.'); } catch (replyError) { // Silently continue if reply also fails } } }); // 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'));