Created
April 19, 2026 07:28
-
-
Save erbanku/5f48d7bed0868c9a73850de42c9eba35 to your computer and use it in GitHub Desktop.
Cloudflare Worker Telegram Bot, Used to Get UserIDs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Cloudflare Worker Telegram Bot | |
| * Logic: Extracts metadata, handles /setup for menu, and provides individually | |
| * copiable monospace fields with a full JSON dump. | |
| */ | |
| export default { | |
| async fetch(request, env) { | |
| const url = new URL(request.url); | |
| // Manual trigger to set the bot menu: https://your-worker.dev/setup | |
| if (url.pathname === "/setup") { | |
| return this.setupCommands(env); | |
| } | |
| if (request.method === "POST") { | |
| try { | |
| const update = await request.json(); | |
| if (update.message) { | |
| await this.handleUpdate(update, env); | |
| } | |
| } catch (err) { | |
| // Silent catch for malformed updates | |
| } | |
| } | |
| return new Response("OK", { status: 200 }); | |
| }, | |
| async setupCommands(env) { | |
| const endpoint = `https://api.telegram.org/bot${env.TG_TOKEN}/setMyCommands`; | |
| const commands = { | |
| commands: [ | |
| { command: "start", description: "Get your account info" }, | |
| { command: "info", description: "Get detailed metadata" } | |
| ] | |
| }; | |
| const response = await fetch(endpoint, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify(commands), | |
| }); | |
| const result = await response.json(); | |
| return new Response(JSON.stringify(result), { | |
| headers: { "Content-Type": "application/json" } | |
| }); | |
| }, | |
| async handleUpdate(update, env) { | |
| const message = update.message; | |
| const user = message.from; | |
| const chatId = message.chat.id; | |
| // Fixed ts(2769) error by using 'as const' | |
| const dateOptions = { | |
| weekday: 'long', | |
| year: 'numeric', | |
| month: 'long', | |
| day: 'numeric' | |
| } as const; | |
| const dateString = new Intl.DateTimeFormat('en-US', dateOptions).format(new Date()); | |
| // Account Details Section | |
| let accountDetails = [ | |
| "<b>Account Details</b>", | |
| `User ID: <code>${user.id}</code>`, | |
| `First Name: <code>${user.first_name}</code>` | |
| ]; | |
| // Hide Last Name if N/A | |
| if (user.last_name) { | |
| accountDetails.push(`Last Name: <code>${user.last_name}</code>`); | |
| } | |
| accountDetails.push(`Username: <code>${user.username ? "@" + user.username : "N/A"}</code>`); | |
| accountDetails.push(`Language: <code>${user.language_code || "N/A"}</code>`); | |
| accountDetails.push(`Is Premium: <code>${user.is_premium || "false"}</code>`); | |
| // Chat Context Section | |
| const chatContext = [ | |
| "<b>Chat Context</b>", | |
| `Chat ID: <code>${chatId}</code>`, | |
| `Chat Type: <code>${message.chat.type}</code>`, | |
| `Message ID: <code>${message.message_id}</code>` | |
| ]; | |
| // Final Assembly with HTML Escaping for JSON | |
| const fullJSON = JSON.stringify(update, null, 2) | |
| .replace(/&/g, "&") | |
| .replace(/</g, "<") | |
| .replace(/>/g, ">"); | |
| const finalMessage = [ | |
| "<b>ID & METADATA REPORT</b>", | |
| "Click any value below to copy:", | |
| "", | |
| ...accountDetails, | |
| "", | |
| ...chatContext, | |
| "", | |
| dateString, | |
| "", | |
| "<b>All Details</b>", | |
| `<pre><code class="language-json">${fullJSON}</code></pre>` | |
| ].join("\n"); | |
| return this.sendToTelegram(chatId, finalMessage, env); | |
| }, | |
| async sendToTelegram(chat_id, text, env) { | |
| const endpoint = `https://api.telegram.org/bot${env.TG_TOKEN}/sendMessage`; | |
| return fetch(endpoint, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| chat_id: chat_id, | |
| text: text, | |
| parse_mode: "HTML", | |
| disable_web_page_preview: true | |
| }), | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment