/** * Telegram Bot for showing rules to new members in a Supergroup * The bot can be hosted on Cloudflare Workers * This bot automatically sends a welcome message with group rules when a new user joins the supergroup. * - The welcome message includes an "Accept" button. * - Only the mentioned user can click the button to accept the rules. * - When accepted, the bot deletes the welcome message to keep the chat clean. * - If another user clicks the button, they receive an alert message. * * The bot is deployed on Cloudflare Workers and listens for updates via Telegram Webhook. * More information about setting webhook here: https://core.telegram.org/bots/api#setwebhook * This is my first experience developing a Telegram bot. * Feel free to share your feedback with me! 😊 * Reza Zarchi https://t.me/rezaZarchi */ export default { async fetch(request) { const TOKEN = "YOUR_BOT_TOKEN"; // Replace with your bot token const API_URL = `https://api.telegram.org/bot${TOKEN}/`; const WELCOME_TEXT = "YOUR_RULES_OR_WELCOME_HERE"; const DEFAULT_USER_NAME = "Dear new member"; const updates = await request.json(); if (updates.message) { const chatId = updates.message.chat.id; // Detect new user joined if (updates.message.new_chat_members) { for (const user of updates.message.new_chat_members) { const userId = user.id; const userMention = user.username ? `@${user.username}` : user.first_name ? user.first_name : DEFAULT_USER_NAME; // Send welcome message with "Accept" button const response = await fetch(`${API_URL}sendMessage`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ chat_id: chatId, text: "Hey " + userMention + WELCOME_TEXT, reply_markup: { inline_keyboard: [[ { text: "✅ Accept", callback_data: `accept_rules_${userId}` } ]] } }) }); const responseBody = await response.json(); if (responseBody.ok) { const messageId = responseBody.result.message_id; return new Response(JSON.stringify({ chat_id: chatId, message_id: messageId })); } } } } // Handle "Accept" button click if (updates.callback_query) { const callbackQuery = updates.callback_query; const chatId = callbackQuery.message.chat.id; const messageId = callbackQuery.message.message_id; const userId = callbackQuery.from.id; const callbackData = callbackQuery.data; // Extract the user ID from callback data if (callbackData.startsWith("accept_rules_")) { const originalUserId = parseInt(callbackData.split("_")[2], 10); if (userId === originalUserId) { // Delete the welcome message if the correct user clicked await fetch(`${API_URL}deleteMessage`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ chat_id: chatId, message_id: messageId }) }); // Respond to the user await fetch(`${API_URL}answerCallbackQuery`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ callback_query_id: callbackQuery.id, show_alert: false, text: "✅ You have accepted the rules! Welcome to the club! 🎉" }) }); } else { // If another user clicks, show an alert message await fetch(`${API_URL}answerCallbackQuery`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ callback_query_id: callbackQuery.id, text: "⛔ You can not accept this message!", show_alert: true }) }); } } } return new Response("OK", { status: 200 }); } };