NimroozSoftware/free_temp_gmail_botPublic · Bot Template

ProfileTelegram

AIThis Persian-language Telegram bot offers temporary email addresses and extracts OTP/verification codes from incoming messages. It uses Libs.mcl to enforce mandatory channel subscriptions before showing the main menu, and provides inline keyboard actions for creating a temp email, viewing stats, and getting help. The callback handler processes provider selection, join verification, and menu navigation via update.callback_query and Api.sendMessage.

Utilitytemp emailgmailotpverification codeinline keyboardcallback query
2 commands1 envUpdated 28d agoCreated Jul 25, 2026
Back to folder

commands/_handle_callback_query.js

javascript · 487 lines

Raw
1/**#command2name: /handle_callback_query3answer: 4keyboard: 5parse_mode: markdown6aliases: 7allow_only_group: false8need_reply: false9is_web: 010#command**/11 12/*Command: /handle_callback_query13need_reply: false14*/15 16const REQUIRED_CHANNELS = ["@Nimrooz_Soft", "@Nimrooznow"];17 18const data     = update.callback_query.data;19const callbackId = update.callback_query.id;20const chatId   = update.callback_query.message.chat.id;21const messageId  = update.callback_query.message.message_id;22const userId   = update.callback_query.from.id;23 24// ==================== توابع کمکی ====================25 26function autoLayout(buttons, itemsPerRow = 2) {27    const rows = [];28    for (let i = 0; i < buttons.length; i += itemsPerRow) {29        rows.push(buttons.slice(i, i + itemsPerRow));30    }31    return rows;32}33 34function escapeMd(text) {35    return String(text ?? "").replace(/([_*`\[\]])/g, "\\$1");36}37 38function stripHtml(html) {39    return String(html ?? "")40        .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "")41        .replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "")42        .replace(/<br\s*\/?>/gi, "\n")43        .replace(/<\/p>/gi, "\n")44        .replace(/<[^>]+>/g, "")45        .replace(/&nbsp;/g, " ")46        .replace(/&amp;/g, "&")47        .replace(/&lt;/g, "<")48        .replace(/&gt;/g, ">")49        .replace(/\n{3,}/g, "\n\n")50        .trim();51}52 53function extractCode(text) {54    const patterns = [55        /(?:code|OTP|verification code|passcode|رمز|کد)[^\d]{0,20}(\d{4,8})/i,56        /\b(\d{6})\b/,57        /\b(\d{4,8})\b/58    ];59    for (const p of patterns) {60        const m = text.match(p);61        if (m) return m[1];62    }63    return null;64}65 66async function safeEdit(params) {67    try {68        await Api.editMessageText(params);69    } catch (e) {}70}71 72function getMainMenuKeyboard() {73    return {74        inline_keyboard: [75            [{ text: "📧 ساخت ایمیل موقت", callback_data: "choose_provider" }],76            [77                { text: "📊 آمار", callback_data: "stats" },78                { text: "❓ راهنما", callback_data: "help" }79            ]80        ]81    };82}83 84// ==================== چک عضویت ====================85 86async function checkMembership() {87    try {88        return await Libs.mcl.check(userId, REQUIRED_CHANNELS);89    } catch (e) {90        return { all_joined: false, left: [], error: true };91    }92}93 94async function promptJoin(membership, text) {95    let btn;96    if (membership.error || !membership.left || membership.left.length === 0) {97        btn = REQUIRED_CHANNELS.map(ch => ([{ text: ch, url: `https://t.me/${ch.replace("@", "")}` }]));98    } else {99        btn = Libs.mcl.getBtn(membership.left);100    }101    btn.push([{ text: "✅ عضو شدم، بررسی کن", callback_data: "check_join" }]);102    await safeEdit({103        chat_id: chatId,104        message_id: messageId,105        text: text,106        reply_markup: { inline_keyboard: btn }107    });108}109 110// ==================== پاسخ سریع به Callback ====================111 112try {113    await Api.answerCallbackQuery({ callback_query_id: callbackId });114} catch (e) {}115 116// ==================== گیت عضویت ====================117 118if (data === "check_join") {119    const membership = await checkMembership();120    if (!membership.all_joined) {121        await promptJoin(membership, "❌ هنوز عضو همه‌ی کانال‌ها نشدی:");122        return;123    }124    await safeEdit({125        chat_id: chatId,126        message_id: messageId,127        text: "👋 *خوش اومدی!*\n\n📮 با این ربات میتونی ایمیل موقت بسازی\n" +128              "و کدهای تایید رو دریافت کنی.\n\n👇 *از دکمه‌های زیر استفاده کن:*",129        parse_mode: "Markdown",130        reply_markup: getMainMenuKeyboard()131    });132    return;133}134 135const membership = await checkMembership();136if (!membership.all_joined) {137    await promptJoin(membership, "🔒 برای ادامه، باید عضو کانال‌(های) زیر باشی:");138    return;139}140 141// ==================== انتخاب Provider ====================142 143if (data === "choose_provider") {144    const providerButtons = [145        { text: "📩 Gmail",   callback_data: "make_gmail"   },146        { text: "📩 Outlook", callback_data: "make_outlook" },147        { text: "📩 Hotmail", callback_data: "make_hotmail" },148        { text: "🎲 تصادفی", callback_data: "make_any"     },149        { text: "🏠 منوی اصلی", callback_data: "main_menu" }150    ];151    await safeEdit({152        chat_id: chatId,153        message_id: messageId,154        text: "📮 *نوع ایمیل مورد نظر را انتخاب کن:*\n\n" +155              "🔹 هر کدام را که انتخاب کنی ساخته میشه\n" +156              "🔹 بهترین گزینه *تصادفی* است\n\n" +157              "👇 *یکی رو انتخاب کن:*",158        parse_mode: "Markdown",159        reply_markup: { inline_keyboard: autoLayout(providerButtons, 2) }160    });161    return;162}163 164// ==================== ساخت ایمیل ====================165 166if (["make_gmail","make_outlook","make_hotmail","make_any"].includes(data)) {167    await safeEdit({ chat_id: chatId, message_id: messageId, text: "⏳ در حال ساخت ایمیل..." });168 169    let apiUrl;170    switch (data) {171        case "make_gmail":172            apiUrl = "https://temp.tf/api/account?dot=1&plus=1&providers=gmail";173            break;174        case "make_outlook":175            apiUrl = "https://temp.tf/api/account?plus=1&providers=outlook";176            break;177        case "make_hotmail":178            apiUrl = "https://temp.tf/api/account?plus=1&providers=high.edu.pl";179            break;180        default:181            apiUrl = "https://temp.tf/api/account?dot=1&plus=1&providers=gmail,outlook,hotmail";182    }183 184    let res;185    try {186        res = await HTTP.get({ url: apiUrl, timeout: 20000 });187    } catch (e) {188        res = { ok: false };189    }190 191    if (!res.ok || !res.data || !res.data.email) {192        await safeEdit({193            chat_id: chatId,194            message_id: messageId,195            text: "❌ خطا در ساخت ایمیل موقت. لطفاً دوباره تلاش کن.",196            reply_markup: {197                inline_keyboard: [[198                    { text: "🔄 تلاش مجدد", callback_data: "choose_provider" },199                    { text: "🏠 منوی اصلی", callback_data: "main_menu" }200                ]]201            }202        });203        return;204    }205 206    const email = res.data.email;207    await db.user.set("temp_email", email);208    await db.user.set("last_count", 0);209 210    const emailButtons = [211        { text: "📋 کپی ایمیل", copy_text: { text: email } },212        { text: "📬 مشاهده پیام‌ها", callback_data: "check_inbox" },213        { text: "📧 ایمیل جدید", callback_data: "choose_provider" },214        { text: "🏠 منوی اصلی", callback_data: "main_menu" }215    ];216 217    await safeEdit({218        chat_id: chatId,219        message_id: messageId,220        text: `✅ *ایمیل موقت ساخته شد!* 🎉\n\n` +221              `📋 *چطور استفاده کنم؟*\n` +222              `🔹 دکمه *کپی* رو بزن 📋\n` +223              `🔹 ایمیل رو جایی استفاده کن ✨\n` +224              `🔹 *مشاهده پیام‌ها* رو بزن تا کدها رو ببینی 📬\n\n` +225              `📧 \`${escapeMd(email)}\`\n\n` +226              `👇 *یکی رو انتخاب کن:*`,227        parse_mode: "Markdown",228        reply_markup: { inline_keyboard: autoLayout(emailButtons, 2) }229    });230    return;231}232 233// ==================== مشاهده صندوق پیام ====================234// ==================== مشاهده صندوق پیام ====================235 236if (data === "check_inbox") {237    const email = await db.user.get("temp_email");238 239    if (!email) {240        await safeEdit({241            chat_id: chatId,242            message_id: messageId,243            text: "❌ اول باید یه ایمیل بسازی.",244            reply_markup: {245                inline_keyboard: [[246                    { text: "📧 ساخت ایمیل", callback_data: "choose_provider" }247                ]]248            }249        });250        return;251    }252 253    await safeEdit({254        chat_id: chatId,255        message_id: messageId,256        text: "📬 در حال بررسی صندوق پیام‌ها..."257    });258 259    let res;260    try {261        res = await HTTP.post({262            url: "https://temp.tf/api/check",263            headers: { "Content-Type": "application/json" },264            body: { email },265            timeout: 8000  // ✅ از ۳۰۰۰۰ به ۸۰۰۰ کاهش داده شد266        });267    } catch (e) {268        // ==================== مدیریت Timeout ====================269        const isTimeout = String(e).toLowerCase().includes("timeout") ||270                          String(e).toLowerCase().includes("timed out");271 272        await safeEdit({273            chat_id: chatId,274            message_id: messageId,275            text: isTimeout276                ? "⏳ *سرور کمی کنده!*\n\nدوباره تلاش کن 👇"277                : "❌ *خطای شبکه!*\n\nدوباره تلاش کن 👇",278            parse_mode: "Markdown",279            reply_markup: {280                inline_keyboard: [281                    [{ text: "🔄 تلاش مجدد", callback_data: "check_inbox" }],282                    [{ text: "🏠 منوی اصلی", callback_data: "main_menu"   }]283                ]284            }285        });286        return;287    }288 289    const backButtons = [290        { text: "🔄 بررسی مجدد",  callback_data: "check_inbox"     },291        { text: "📧 ایمیل جدید",  callback_data: "choose_provider" },292        { text: "🏠 منوی اصلی",   callback_data: "main_menu"       }293    ];294    const backKeyboard = { inline_keyboard: autoLayout(backButtons, 2) };295 296    if (!res.ok) {297        const errText = res.status === 429298            ? "⏳ درخواست زیاده، کمی صبر کن."299            : `❌ خطای سرور (کد ${res.status || "نامشخص"}).`;300        await safeEdit({301            chat_id: chatId,302            message_id: messageId,303            text: `❌ *خطا در بررسی صندوق*\n\n${errText}`,304            parse_mode: "Markdown",305            reply_markup: backKeyboard306        });307        return;308    }309 310    const messages = (res.data && res.data.data) || [];311 312    if (messages.length === 0) {313        await db.user.set("last_count", 0);314        await safeEdit({315            chat_id: chatId,316            message_id: messageId,317            text: `📭 *صندوق خالیه!*\n\n` +318                  `📧 \`${escapeMd(email)}\`\n\n` +319                  `⏰ هنوز پیامی نرسیده.\n` +320                  `🔄 چند دقیقه دیگه دوباره چک کن.`,321            parse_mode: "Markdown",322            reply_markup: backKeyboard323        });324        return;325    }326 327    const latest   = messages[0];328    const bodyText = latest.bodyContentType === "html"329        ? stripHtml(latest.body || "")330        : String(latest.body || "");331    const code = extractCode(bodyText);332 333    let dateStr = String(latest.date || "");334    try { dateStr = new Date(latest.date).toLocaleString("fa-IR"); } catch (e) {}335 336    let text =337        `📬 *${messages.length} پیام* برای \`${escapeMd(email)}\`\n\n` +338        `━━━━━━━━━━━━━━━━━━━━\n` +339        `📩 *${escapeMd(latest.subject || "(بدون موضوع)")}*\n` +340        `👤 *از:* ${escapeMd(latest.from || "نامشخص")}\n` +341        `🕒 *زمان:* ${escapeMd(dateStr)}\n` +342        `━━━━━━━━━━━━━━━━━━━━\n\n` +343        `${escapeMd(bodyText.slice(0, 500))}`;344 345    if (bodyText.length > 500) text += "\n\n... *(متن کامل نمایش داده نشد)*";346    if (code) text += `\n\n🔐 *کد تایید:* \`${code}\``;347 348    const actionButtons = [349        ...(code ? [{ text: `📋 کپی کد: ${code}`, copy_text: { text: code } }] : []),350        { text: "🔄 بروزرسانی",  callback_data: "check_inbox"     },351        { text: "📧 ایمیل جدید", callback_data: "choose_provider" },352        { text: "🗑 حذف ایمیل",  callback_data: "delete_email"    },353        { text: "🏠 منوی اصلی",  callback_data: "main_menu"       }354    ];355 356    await db.user.set("last_count", messages.length);357 358    await safeEdit({359        chat_id: chatId,360        message_id: messageId,361        text,362        parse_mode: "Markdown",363        reply_markup: { inline_keyboard: autoLayout(actionButtons, 2) }364    });365    return;366}367// ==================== آمار ====================368 369if (data === "stats") {370    await safeEdit({ chat_id: chatId, message_id: messageId, text: "📊 در حال دریافت آمار..." });371 372    let res;373    try {374        res = await HTTP.get({ url: "https://temp.tf/api/stats?dot=1&plus=1", timeout: 20000 });375    } catch (e) {376        res = { ok: false };377    }378 379    const backKeyboard = {380        inline_keyboard: [[381            { text: "🔄 بروزرسانی", callback_data: "stats"     },382            { text: "🏠 منوی اصلی", callback_data: "main_menu" }383        ]]384    };385 386    if (!res.ok || !res.data) {387        await safeEdit({388            chat_id: chatId,389            message_id: messageId,390            text: "❌ خطا در دریافت آمار سرویس.",391            reply_markup: backKeyboard392        });393        return;394    }395 396    const s = res.data;397    const userEmail  = await db.user.get("temp_email");398    const lastCount  = (await db.user.get("last_count")) || 0;399 400    let userStats = "";401    if (userEmail) {402        userStats = `\n\n👤 *آمار شما*\n` +403                    `📧 \`${escapeMd(userEmail)}\`\n` +404                    `📬 ${lastCount} پیام دریافتی`;405    }406 407    let dateStr = "";408    try { dateStr = new Date().toLocaleString("fa-IR"); } catch(e) { dateStr = new Date().toString(); }409 410    await safeEdit({411        chat_id: chatId,412        message_id: messageId,413        text: `📊 *آمار سرویس*\n\n` +414              `📮 آدرس‌های ممکن: ${escapeMd(String(s.totalFormatted || "نامشخص"))}\n` +415              `📩 کل پیام‌های دریافتی: ${s.totalReceived ?? "نامشخص"}` +416              `${userStats}\n\n` +417              `🕒 ${dateStr}`,418        parse_mode: "Markdown",419        reply_markup: backKeyboard420    });421    return;422}423 424// ==================== منوی اصلی ====================425 426if (data === "main_menu") {427    await safeEdit({428        chat_id: chatId,429        message_id: messageId,430        text: "👋 *به منوی اصلی خوش اومدی!*\n\n" +431              "📮 با این ربات میتونی ایمیل موقت بسازی\n" +432              "و کدهای تایید رو دریافت کنی.\n\n" +433              "👇 *از دکمه‌های زیر استفاده کن:*",434        parse_mode: "Markdown",435        reply_markup: getMainMenuKeyboard()436    });437    return;438}439 440// ==================== راهنما ====================441 442if (data === "help") {443    const helpButtons = [444        { text: "📧 ساخت ایمیل",    callback_data: "choose_provider" },445        { text: "📬 مشاهده پیام‌ها", callback_data: "check_inbox"     },446        { text: "🏠 منوی اصلی",      callback_data: "main_menu"       }447    ];448    await safeEdit({449        chat_id: chatId,450        message_id: messageId,451        text: "📖 *راهنمای ربات ایمیل موقت*\n\n" +452              "1️⃣ *ساخت ایمیل:*\n" +453              "   یک ایمیل موقت جدید بساز\n\n" +454              "2️⃣ *مشاهده پیام‌ها:*\n" +455              "   پیام‌های دریافتی رو ببین\n\n" +456              "3️⃣ *آمار:*\n" +457              "   وضعیت و آمار سرویس رو ببین\n\n" +458              "🔐 *امنیت:*\n" +459              "   ایمیل‌ها موقتی هستن و بعد از مدتی حذف میشن\n\n" +460              "📌 *نکته:*\n" +461              "   برای دریافت کدهای تایید سایت‌ها از این ربات استفاده کن",462        parse_mode: "Markdown",463        reply_markup: { inline_keyboard: autoLayout(helpButtons, 2) }464    });465    return;466}467 468// ==================== حذف ایمیل ====================469 470if (data === "delete_email") {471    await db.user.delete("temp_email");472    await db.user.delete("last_count");473 474    const deleteButtons = [475        { text: "📧 ساخت ایمیل", callback_data: "choose_provider" },476        { text: "🏠 منوی اصلی",  callback_data: "main_menu"       }477    ];478    await safeEdit({479        chat_id: chatId,480        message_id: messageId,481        text: "🗑 *ایمیل شما با موفقیت حذف شد!*\n\n" +482              "برای ساخت ایمیل جدید روی دکمه زیر کلیک کن 👇",483        parse_mode: "Markdown",484        reply_markup: { inline_keyboard: autoLayout(deleteButtons, 2) }485    });486    return;487}