File size: 2,770 Bytes
459c2fd
 
 
 
 
 
c41d06a
ef02db0
459c2fd
c41d06a
da9604c
ef02db0
 
f7d9c2f
 
6efe55a
 
ef02db0
2bb34d7
 
ef02db0
 
 
 
bf210eb
f7d9c2f
 
 
 
 
 
 
 
 
 
da9604c
f7d9c2f
 
 
 
 
 
 
bf210eb
c41d06a
bf210eb
ef02db0
 
da9604c
ef02db0
da9604c
 
ef02db0
459c2fd
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;