error code: 1101 // ============================================ // Cloudflare Worker - ساده برای تست // ============================================ const BOT_TOKEN = '8614787482:AAF1n2FSdMTZEGcrjQKG3jTZ1tyaMGMgYyQ'; const API_URL = 'https://api-proxy.vbnsan.workers.dev/api'; const SECRET_TOKEN = 'mySecretToken12345'; function createResponse(data, status = 200) { return new Response(JSON.stringify(data), { status: status, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, }); } async function handleRequest(request) { const url = new URL(request.url); const path = url.pathname; // ===== تست ===== if (path === '/' || path === '/test') { return createResponse({ status: 'ok', message: 'Bot is running', version: '1.0.0' }); } // ===== Webhook ===== if (path === '/webhook') { try { const content = await request.text(); if (!content) { return createResponse({ ok: true }); } const update = JSON.parse(content); if (!update.message) { return createResponse({ ok: true }); } const message = update.message; const chat_id = message.chat.id; const text = message.text ? message.text.trim() : ''; const contact = message.contact; // پردازش شماره تماس if (contact && contact.phone_number) { let mobile = contact.phone_number.replace('+98', '').replace(/[^0-9]/g, ''); if (!mobile.startsWith('09')) { mobile = '0' + mobile; } // ارسال به API const postData = new URLSearchParams({ token: SECRET_TOKEN, action: 'find_user', mobile: mobile }); const response = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: postData.toString(), }); const result = await response.json(); if (result && result.success) { const user = result.user; const user_name = user.fullname || user.name; // ذخیره chat_id const saveData = new URLSearchParams({ token: SECRET_TOKEN, action: 'save_chat_id', user_id: user.user_id, chat_id: chat_id }); await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: saveData.toString(), }); const msg = `✅ ${user_name} عزیز، شماره شما ثبت شد.\nاسناد مالی شما از این پس ارسال خواهد شد.`; await sendMessage(chat_id, msg); } else { const msg = `❌ شماره ${mobile} در سیستم ثبت نشده است.`; await sendMessage(chat_id, msg); } return createResponse({ ok: true }); } // ===== /start ===== if (text === '/start') { const msg = "👋 سلام! به ربات مالی خوش آمدید.\n\nبرای ثبت نام، روی دکمه زیر کلیک کنید:"; const keyboard = { keyboard: [ [{ text: "📱 اشتراک‌گذاری شماره تلفن", request_contact: true }] ], resize_keyboard: true, one_time_keyboard: true }; await sendMessageWithKeyboard(chat_id, msg, keyboard); return createResponse({ ok: true }); } return createResponse({ ok: true }); } catch (error) { console.error('Error:', error.message); return createResponse({ ok: false, error: error.message }, 500); } } return createResponse({ error: 'Not Found' }, 404); } // ============================================ // تابع ارسال پیام // ============================================ async function sendMessage(chat_id, text) { const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`; const data = { chat_id: chat_id, text: text, parse_mode: 'HTML' }; return fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }); } // ============================================ // تابع ارسال پیام با کیبورد // ============================================ async function sendMessageWithKeyboard(chat_id, text, keyboard) { const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`; const data = { chat_id: chat_id, text: text, parse_mode: 'HTML', reply_markup: keyboard }; return fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }); } // ============================================ // رویداد Fetch // ============================================ addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); });