Personal Telegram developer console for GitHub

feat: add secure Telegram webhook and GitHub repo listing

davoodmehraban89 committed Oct 2, 2026 at 16:24 UTC 4283ee9c8fa6977d03d4f398dc005c08471d7397
1 file changed +158
src/index.js new
+158
@@ -0,0 +1,158 @@
1 +const json = (data, status = 200) =>
2 + new Response(JSON.stringify(data), {
3 + status,
4 + headers: { "content-type": "application/json; charset=utf-8" },
5 + });
6 +
7 +const text = (body, status = 200) =>
8 + new Response(body, {
9 + status,
10 + headers: { "content-type": "text/plain; charset=utf-8" },
11 + });
12 +
13 +async function telegramApi(env, method, payload) {
14 + const response = await fetch(`https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/${method}`, {
15 + method: "POST",
16 + headers: { "content-type": "application/json" },
17 + body: JSON.stringify(payload),
18 + });
19 +
20 + if (!response.ok) {
21 + const body = await response.text();
22 + throw new Error(`Telegram API ${method} failed: ${response.status} ${body}`);
23 + }
24 +
25 + return response.json();
26 +}
27 +
28 +async function sendMessage(env, chatId, message, extra = {}) {
29 + return telegramApi(env, "sendMessage", {
30 + chat_id: chatId,
31 + text: message,
32 + parse_mode: "HTML",
33 + disable_web_page_preview: true,
34 + ...extra,
35 + });
36 +}
37 +
38 +function isOwner(env, from) {
39 + if (!from || !env.OWNER_TELEGRAM_ID) return false;
40 + return String(from.id) === String(env.OWNER_TELEGRAM_ID);
41 +}
42 +
43 +function githubHeaders(env) {
44 + return {
45 + Accept: "application/vnd.github+json",
46 + Authorization: `Bearer ${env.GITHUB_TOKEN}`,
47 + "X-GitHub-Api-Version": "2022-11-28",
48 + "User-Agent": "nexa-dev-bot",
49 + };
50 +}
51 +
52 +async function fetchRepos(env) {
53 + const response = await fetch(
54 + "https://api.github.com/user/repos?sort=updated&per_page=20&affiliation=owner,collaborator,organization_member",
55 + { headers: githubHeaders(env) }
56 + );
57 +
58 + if (!response.ok) {
59 + const body = await response.text();
60 + throw new Error(`GitHub repos failed: ${response.status} ${body}`);
61 + }
62 +
63 + return response.json();
64 +}
65 +
66 +function startMessage() {
67 + return [
68 + "<b>Nexa Dev</b>",
69 + "دستیار شخصی توسعه و مدیریت GitHub از داخل تلگرام.",
70 + "",
71 + "فرمان‌های نسخه اول:",
72 + "/repos — نمایش مخزن‌های قابل دسترس",
73 + "/status — وضعیت سرویس",
74 + ].join("\n");
75 +}
76 +
77 +async function handleCommand(env, message) {
78 + const chatId = message.chat?.id;
79 + const command = (message.text || "").trim().split(/\s+/)[0].split("@")[0].toLowerCase();
80 +
81 + if (!isOwner(env, message.from)) {
82 + if (chatId) await sendMessage(env, chatId, "⛔️ دسترسی مجاز نیست.");
83 + return;
84 + }
85 +
86 + if (command === "/start") {
87 + await sendMessage(env, chatId, startMessage());
88 + return;
89 + }
90 +
91 + if (command === "/status") {
92 + await sendMessage(env, chatId, "✅ Nexa Dev آنلاین است.\nGitHub integration: آماده بررسی");
93 + return;
94 + }
95 +
96 + if (command === "/repos") {
97 + if (!env.GITHUB_TOKEN) {
98 + await sendMessage(env, chatId, "⚠️ اتصال GitHub هنوز با Secret نهایی نشده است.");
99 + return;
100 + }
101 +
102 + const repos = await fetchRepos(env);
103 + if (!repos.length) {
104 + await sendMessage(env, chatId, "هیچ مخزنی پیدا نشد.");
105 + return;
106 + }
107 +
108 + const lines = repos.slice(0, 20).map((repo, i) => {
109 + const visibility = repo.private ? "🔒" : "🌐";
110 + return `${i + 1}. ${visibility} <b>${repo.full_name}</b>\n branch: <code>${repo.default_branch}</code>`;
111 + });
112 +
113 + await sendMessage(env, chatId, `<b>Repositories</b>\n\n${lines.join("\n\n")}`);
114 + return;
115 + }
116 +
117 + await sendMessage(env, chatId, "فرمان شناخته نشد. /start را بزن.");
118 +}
119 +
120 +async function handleTelegramWebhook(request, env) {
121 + const expected = env.TELEGRAM_WEBHOOK_SECRET;
122 + if (expected) {
123 + const actual = request.headers.get("X-Telegram-Bot-Api-Secret-Token");
124 + if (actual !== expected) return text("Forbidden", 403);
125 + }
126 +
127 + const update = await request.json();
128 + if (update.message?.text) {
129 + await handleCommand(env, update.message);
130 + }
131 +
132 + return text("OK");
133 +}
134 +
135 +export default {
136 + async fetch(request, env) {
137 + const url = new URL(request.url);
138 +
139 + if (request.method === "GET" && url.pathname === "/") {
140 + return json({
141 + name: "Nexa Dev",
142 + status: "ok",
143 + version: "0.1.0",
144 + });
145 + }
146 +
147 + if (request.method === "POST" && url.pathname === "/telegram/webhook") {
148 + try {
149 + return await handleTelegramWebhook(request, env);
150 + } catch (error) {
151 + console.error(error);
152 + return text("Internal Server Error", 500);
153 + }
154 + }
155 +
156 + return text("Not Found", 404);
157 + },
158 +};