Personal Telegram developer console for GitHub

feat/studio-g0-foundation
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 203 lines 6.23 KB
Raw
1 import { NexaDevBot } from './bot.js';
2 import { CloudflareMenuManager } from './cloudflare-menu.js';
3 import { RuntimeDiagnostics } from './core/runtime-diagnostics.js';
4 import { platformHomeView, platformCapabilityView } from './platform-menu.js';
5 import { studioAppUrl } from './config.js';
6 import { interceptTelegramSecretMessage } from './core/telegram-secret-guard.js';
7
8 const STUDIO_URL_ERROR =
9 '⚠️ نشانی Nexa Studio پیکربندی نشده است. با مدیر ربات تماس بگیرید.';
10
11 function isOwnerPrivateChat(ownerId, from, chat) {
12 return (
13 Boolean(ownerId) &&
14 from?.id !== undefined &&
15 from?.id !== null &&
16 String(from.id) === String(ownerId) &&
17 chat?.type === 'private' &&
18 Number.isSafeInteger(chat.id) &&
19 chat.id > 0 &&
20 String(chat.id) === String(ownerId)
21 );
22 }
23
24 function studioEntryView(url) {
25 return url
26 ? {
27 text: 'برای کار با پروژه‌ها، Nexa Studio را با دکمه زیر باز کنید.\nتوکن و متن کد را در چت نفرستید.',
28 reply_markup: {
29 inline_keyboard: [
30 [{ text: 'باز کردن Nexa Studio', web_app: { url } }]
31 ]
32 }
33 }
34 : {
35 text: STUDIO_URL_ERROR,
36 reply_markup: { inline_keyboard: [] }
37 };
38 }
39 export class GuidedNexaDevBot extends NexaDevBot {
40 constructor(env = {}) {
41 super(env);
42 this.diagnostics = new RuntimeDiagnostics({
43 env,
44 github: this.github,
45 registry: this.registry,
46 replayStore: this.replayStore,
47 workspaceStore: this.workspaceStore,
48 approvals: this.approvals
49 });
50 this.menu = new CloudflareMenuManager(this.github, this.diagnostics);
51 }
52 async handleMessage(message) {
53 const intercepted = await interceptTelegramSecretMessage({
54 message,
55 telegram: this.telegram,
56 ownerId: this.env.OWNER_TELEGRAM_ID
57 });
58 if (intercepted) return intercepted;
59 const chatId = message?.chat?.id,
60 rawText = typeof message?.text === 'string' ? message.text : '',
61 text = rawText.trim();
62 const startCommand = /^\/start(?:@[A-Za-z0-9_]+)?(?:\s|$)/.test(text);
63 const menuCommand = /^\/menu(?:@[A-Za-z0-9_]+)?$/.test(text);
64 if (startCommand || menuCommand || text === '🏠 منوی اصلی') {
65 if (
66 !isOwnerPrivateChat(
67 this.env.OWNER_TELEGRAM_ID,
68 message?.from,
69 message?.chat
70 )
71 )
72 return {
73 ok: false,
74 studioEntry: true,
75 reason: 'owner_private_chat_required'
76 };
77 const url = studioAppUrl(this.env.STUDIO_URL);
78 if (!url) {
79 const res = await this.telegram.sendMessage(chatId, STUDIO_URL_ERROR, {
80 remove_keyboard: true
81 });
82 return { ok: false, studioEntry: true, invalidStudioUrl: true, res };
83 }
84 const welcomeRes = await this.telegram.sendMessage(
85 chatId,
86 'به Nexa Studio خوش آمدید.',
87 { remove_keyboard: true }
88 );
89 let menuButtonConfigured;
90 if (startCommand) {
91 menuButtonConfigured = false;
92 try {
93 await this.telegram.setChatMenuButton(chatId, {
94 type: 'web_app',
95 text: 'Nexa Studio',
96 web_app: { url }
97 });
98 menuButtonConfigured = true;
99 } catch {
100 // Telegram menu setup is optional; never expose provider errors.
101 }
102 }
103 const view = studioEntryView(url);
104 const res = await this.telegram.sendMessage(
105 chatId,
106 view.text,
107 view.reply_markup
108 );
109 return {
110 ok: true,
111 ...(text === '🏠 منوی اصلی'
112 ? { quickMenu: 'main' }
113 : { command: 'start' }),
114 studioEntry: true,
115 ...(startCommand ? { menuButtonConfigured } : {}),
116 welcomeRes,
117 res
118 };
119 }
120 if (!chatId) return super.handleMessage(message);
121 if (text === '📘 راهنما' || text === '/help') {
122 const view = this.menu.getGuideIndexView();
123 const res = await this.telegram.sendMessage(
124 chatId,
125 view.text,
126 view.reply_markup
127 );
128 return { ok: true, quickMenu: 'help', res };
129 }
130 if (text === '🧭 پلتفرم' || text === '/platform') {
131 const view = platformHomeView();
132 const res = await this.telegram.sendMessage(
133 chatId,
134 view.text,
135 view.reply_markup
136 );
137 return { ok: true, quickMenu: 'platform', res };
138 }
139 return super.handleMessage(message);
140 }
141 async handleCallbackQuery(query) {
142 const data = query?.data || '';
143 if (data === 'menu:main') {
144 const message = query?.message;
145 if (
146 !isOwnerPrivateChat(
147 this.env.OWNER_TELEGRAM_ID,
148 query?.from,
149 message?.chat
150 ) ||
151 !Number.isSafeInteger(message?.message_id)
152 )
153 return {
154 ok: false,
155 studioEntry: true,
156 reason: 'owner_private_chat_required'
157 };
158 let callbackAnswered = true;
159 try {
160 await this.telegram.answerCallbackQuery(query.id);
161 } catch {
162 callbackAnswered = false;
163 }
164 const url = studioAppUrl(this.env.STUDIO_URL);
165 const view = studioEntryView(url);
166 const res = await this.telegram.editMessageText(
167 message.chat.id,
168 message.message_id,
169 view.text,
170 view.reply_markup
171 );
172 return {
173 ok: Boolean(url),
174 studioEntry: true,
175 ...(!url ? { invalidStudioUrl: true } : {}),
176 callbackAnswered,
177 edited: true,
178 res
179 };
180 }
181 if (data === 'menu:platform' || data.startsWith('platform:')) {
182 const view =
183 data === 'menu:platform'
184 ? platformHomeView()
185 : platformCapabilityView(data.slice(9));
186 if (!view) return super.handleCallbackQuery(query);
187 let callbackAnswered = true;
188 try {
189 await this.telegram.answerCallbackQuery(query.id);
190 } catch {
191 callbackAnswered = false;
192 }
193 const res = await this.telegram.editMessageText(
194 query?.message?.chat?.id,
195 query?.message?.message_id,
196 view.text,
197 view.reply_markup
198 );
199 return { ok: true, platform: true, callbackAnswered, edited: true, res };
200 }
201 return super.handleCallbackQuery(query);
202 }
203 }