const { Telegraf, Markup } = require('telegraf'); require('dotenv').config(); const bot = new Telegraf(process.env.BOT_TOKEN); const APP_URL = process.env.APP_URL || 'https://alexainc-aurudu-krida.hf.space'; // Fallback for local bot.start(async (ctx) => { const payload = ctx.startPayload; const isGroup = ctx.chat.type === 'group' || ctx.chat.type === 'supergroup'; const botUser = ctx.botInfo.username; console.log(`[Bot Start] Chat: ${ctx.chat.id} (${ctx.chat.type}), Payload: ${payload}`); if (isGroup) { // Only pass Chat ID to keep payload under 64 characters const encoded = Buffer.from(ctx.chat.id.toString()).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); const privateLink = `https://t.me/${botUser}?start=${encoded}`; return ctx.replyWithMarkdown( `🏮 **Welcome to the ${ctx.chat.title} Village!** 🏮\n\nTo compete for your community and see your name on the board, please join the festival in the bot's private chat.`, Markup.inlineKeyboard([[Markup.button.url(`🏺 JOIN ${ctx.chat.title.toUpperCase()}`, privateLink)]]) ); } if (payload) { try { const decodedId = Buffer.from(payload.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString(); console.log("[Deep Link] Joining Village ID:", decodedId); let vName = 'Our Village'; try { // Fetch latest title from Telegram to handle emojis and long names const chat = await ctx.telegram.getChat(decodedId); vName = chat.title; } catch (e) { console.warn("Could not fetch chat title, bot might not be in group anymore."); } const finalUrl = `${APP_URL}?vId=${decodedId}&vName=${encodeURIComponent(vName)}&uName=${encodeURIComponent(ctx.from.first_name)}`; return ctx.replyWithMarkdown( `🏺 **Invitation to ${vName}**\n\nYou've been invited to join the festival as a hero for **${vName}**. Are you ready to battle?`, Markup.inlineKeyboard([[Markup.button.webApp(`🏮 JOIN VILLAGE ${vName.toUpperCase()}`, finalUrl)]]) ); } catch (e) { console.error('Payload error:', e); } } // Default Fallback return ctx.replyWithMarkdown( `🏮 **Welcome to the Aurudu Festival!** 🏮\n\nJoin a village group and click their **JOIN** button to compete for your community, or practice solo right now!`, Markup.inlineKeyboard([[Markup.button.webApp('🏮 ENTER BATTLE GROUND (SOLO)', APP_URL)]]) ); }); bot.help((ctx) => ctx.reply('Use /start to enter the Avurudu Village Festival.')); module.exports = bot;