diff --git a/README.md b/README.md index 0b0002c..47e3979 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ Chạy trên Cloudflare Worker, đơn giản hoạt động như một proxy cho - Hiệu suất cao với mạng lưới toàn cầu của Cloudflare - Trang tài liệu tích hợp sẵn - Hỗ trợ tất cả các phương thức HTTP (GET, POST, PUT, DELETE) +- Gửi tệp tin bằng `multipart/form-data` (ví dụ: `sendPhoto`, `sendDocument`) +- Xử lý biểu tượng cảm xúc và ký tự đặc biệt ổn định hơn ## Cài đặt diff --git a/telegram-bot-proxy.js b/telegram-bot-proxy.js index 409e3ef..ebb3bf0 100644 --- a/telegram-bot-proxy.js +++ b/telegram-bot-proxy.js @@ -108,25 +108,27 @@ async function handleRequest(request) { // Reconstruct the Telegram API URL const telegramUrl = `${TELEGRAM_API_BASE}${url.pathname}${url.search}`; - // Create headers for the new request - const headers = new Headers(request.headers); - - // Forward the request to Telegram API - let body = undefined; - if (request.method !== 'GET' && request.method !== 'HEAD') { - try { - body = await request.arrayBuffer(); - } catch (err) { - return new Response(`Failed to read request body: ${err.message}`, { status: 400 }); - } - } - - const proxyReq = new Request(telegramUrl, { - method: request.method, - headers: request.headers, - body, - redirect: 'follow', - }); + // Clone request headers so we can safely modify them + const headers = new Headers(request.headers); + + // Ensure JSON requests explicitly use UTF-8 to avoid issues with emoji or + // other special characters + const contentType = headers.get('Content-Type'); + if (contentType && contentType.startsWith('application/json') && !contentType.includes('charset')) { + headers.set('Content-Type', 'application/json; charset=UTF-8'); + } + + const init = { + method: request.method, + headers, + redirect: 'follow', + }; + if (request.method !== 'GET' && request.method !== 'HEAD') { + init.body = request.body; + } + + // Forward the request to Telegram API, streaming the body when present. + const proxyReq = new Request(telegramUrl, init); try { const tgRes = await fetch(proxyReq);