| 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'; |
|
|
| 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) { |
| |
| 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 { |
| |
| 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); |
| } |
| } |
|
|
| |
| 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; |
|
|