Personal Telegram developer console for GitHub
fix/ci-actions-pagination
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
| 1 | import { TelegramClient } from './telegram.js'; |
| 2 | import { GitHubService } from './github.js'; |
| 3 | import { MenuManager } from './menu.js'; |
| 4 | import { PluginRegistry } from './core/plugin-registry.js'; |
| 5 | import { ActionPolicy } from './core/action-policy.js'; |
| 6 | import { ApprovalService } from './core/approval.js'; |
| 7 | import { DurableApprovalReplayStore } from './core/durable-replay-store.js'; |
| 8 | import { AuditLogger } from './core/audit.js'; |
| 9 | import { PluginRouter } from './core/router.js'; |
| 10 | import { createGitHubPlugin } from './plugins/github/plugin.js'; |
| 11 | |
| 12 | function approvalSourceCallback(payload = {}) { |
| 13 | const repoId = payload.input?.repoId; |
| 14 | const runId = payload.input?.runId; |
| 15 | if (!Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(runId))) { |
| 16 | throw new Error('Approval input is invalid'); |
| 17 | } |
| 18 | |
| 19 | if (payload.actionId === 'github.workflow.run.rerun_failed') { |
| 20 | return `rerun:${repoId}:${runId}`; |
| 21 | } |
| 22 | if (payload.actionId === 'github.workflow.run.cancel') { |
| 23 | return `cancelrun:${repoId}:${runId}`; |
| 24 | } |
| 25 | throw new Error('Approval action is not supported'); |
| 26 | } |
| 27 | |
| 28 | function approvalView({ label, token, repoId, runId }) { |
| 29 | return { |
| 30 | text: |
| 31 | `⚠️ <b>تأیید عملیات لازم است</b> |
| 32 | ➖➖➖➖➖➖➖➖➖➖ |
| 33 | عملیات: <b>${label}</b> |
| 34 | این عملیات روی GitHub تغییر ایجاد میکند و فقط پس از تأیید شما اجرا میشود. |
| 35 | |
| 36 | اگر مطمئن هستید، «تأیید و اجرا» را بزنید.`, |
| 37 | reply_markup: { |
| 38 | inline_keyboard: [ |
| 39 | [{ text: '✅ تأیید و اجرا', callback_data: `approve:${token}` }], |
| 40 | [{ text: '↩️ انصراف', callback_data: `run:${repoId}:${runId}` }] |
| 41 | ] |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | export class NexaDevBot { |
| 47 | constructor(env = {}) { |
| 48 | this.env = env; |
| 49 | this.telegram = new TelegramClient(env.TELEGRAM_BOT_TOKEN || ''); |
| 50 | this.github = new GitHubService(env.GITHUB_TOKEN || '', env.GITHUB_OWNER || 'davoodmehraban89'); |
| 51 | this.menu = new MenuManager(this.github); |
| 52 | this.replayStore = new DurableApprovalReplayStore({ |
| 53 | namespace: env.APPROVAL_REPLAY_GUARD |
| 54 | }); |
| 55 | this.approvals = new ApprovalService({ |
| 56 | secret: env.APPROVAL_SIGNING_SECRET || env.TELEGRAM_WEBHOOK_SECRET || '', |
| 57 | replayStore: this.replayStore |
| 58 | }); |
| 59 | |
| 60 | this.githubPlugin = createGitHubPlugin({ |
| 61 | github: this.github, |
| 62 | menu: this.menu |
| 63 | }); |
| 64 | this.registry = new PluginRegistry([this.githubPlugin]); |
| 65 | this.policy = new ActionPolicy(); |
| 66 | this.audit = new AuditLogger({ |
| 67 | secrets: [ |
| 68 | env.TELEGRAM_BOT_TOKEN, |
| 69 | env.TELEGRAM_WEBHOOK_SECRET, |
| 70 | env.GITHUB_TOKEN, |
| 71 | env.APPROVAL_SIGNING_SECRET |
| 72 | ] |
| 73 | }); |
| 74 | this.router = new PluginRouter({ |
| 75 | registry: this.registry, |
| 76 | policy: this.policy, |
| 77 | audit: this.audit |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | async handleUpdate(update) { |
| 82 | if (!update) return { ok: false, error: 'Empty update' }; |
| 83 | if (update.callback_query) return this.handleCallbackQuery(update.callback_query); |
| 84 | if (update.message) return this.handleMessage(update.message); |
| 85 | return { ok: true, ignored: true }; |
| 86 | } |
| 87 | |
| 88 | async handleCallbackQuery(query) { |
| 89 | const callbackId = query.id; |
| 90 | const data = query.data || 'menu:main'; |
| 91 | const message = query.message; |
| 92 | const chatId = message?.chat?.id; |
| 93 | const messageId = message?.message_id; |
| 94 | const ownerId = query.from?.id; |
| 95 | |
| 96 | let callbackAnswered = true; |
| 97 | try { |
| 98 | await this.telegram.answerCallbackQuery(callbackId); |
| 99 | } catch { |
| 100 | callbackAnswered = false; |
| 101 | } |
| 102 | |
| 103 | if (!chatId || !messageId) { |
| 104 | return { ok: false, error: 'Missing message context for callback', callbackAnswered }; |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | if (data.startsWith('approve:')) { |
| 109 | if (ownerId === undefined || ownerId === null) throw new Error('Approval owner is required'); |
| 110 | const token = data.slice('approve:'.length); |
| 111 | let payload; |
| 112 | try { |
| 113 | payload = await this.approvals.consume(token, { |
| 114 | ownerId: String(ownerId), |
| 115 | pluginId: 'github', |
| 116 | risk: 'WRITE' |
| 117 | }); |
| 118 | } catch (error) { |
| 119 | this.audit.log({ |
| 120 | event: 'approval_rejected', |
| 121 | pluginId: 'github', |
| 122 | risk: 'WRITE', |
| 123 | outcome: 'rejected', |
| 124 | error: error?.message || 'Approval rejected' |
| 125 | }); |
| 126 | throw error; |
| 127 | } |
| 128 | |
| 129 | this.audit.log({ |
| 130 | event: 'approval_verified', |
| 131 | pluginId: payload.pluginId, |
| 132 | actionId: payload.actionId, |
| 133 | risk: payload.risk, |
| 134 | outcome: 'verified' |
| 135 | }); |
| 136 | |
| 137 | const sourceCallback = approvalSourceCallback(payload); |
| 138 | const routed = await this.router.dispatchCallback(sourceCallback, { |
| 139 | isOwner: true, |
| 140 | approved: true |
| 141 | }); |
| 142 | if (!routed || routed.requiresApproval || routed.denied) { |
| 143 | throw new Error('Approved action could not be dispatched'); |
| 144 | } |
| 145 | |
| 146 | const res = await this.telegram.editMessageText( |
| 147 | chatId, |
| 148 | messageId, |
| 149 | routed.text, |
| 150 | routed.reply_markup |
| 151 | ); |
| 152 | return { |
| 153 | ok: true, |
| 154 | approved: true, |
| 155 | actionId: payload.actionId, |
| 156 | callbackAnswered, |
| 157 | edited: true, |
| 158 | res |
| 159 | }; |
| 160 | } |
| 161 | |
| 162 | const routed = await this.router.dispatchCallback(data, { isOwner: true }); |
| 163 | if (routed?.requiresApproval) { |
| 164 | const approval = routed.approval; |
| 165 | const repoId = approval?.input?.repoId; |
| 166 | const runId = approval?.input?.runId; |
| 167 | if (!approval || !Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(runId))) { |
| 168 | throw new Error('Approval metadata is missing'); |
| 169 | } |
| 170 | if (ownerId === undefined || ownerId === null) throw new Error('Approval owner is required'); |
| 171 | if (!this.approvals.isReplayProtected()) { |
| 172 | throw new Error('Durable approval replay protection is not configured'); |
| 173 | } |
| 174 | |
| 175 | const token = await this.approvals.issue({ |
| 176 | pluginId: routed.pluginId, |
| 177 | actionId: routed.actionId, |
| 178 | risk: routed.risk, |
| 179 | ownerId: String(ownerId), |
| 180 | input: approval.input |
| 181 | }); |
| 182 | this.audit.log({ |
| 183 | event: 'approval_issued', |
| 184 | pluginId: routed.pluginId, |
| 185 | actionId: routed.actionId, |
| 186 | risk: routed.risk, |
| 187 | outcome: 'pending' |
| 188 | }); |
| 189 | |
| 190 | const view = approvalView({ |
| 191 | label: approval.label || routed.actionId, |
| 192 | token, |
| 193 | repoId, |
| 194 | runId |
| 195 | }); |
| 196 | const res = await this.telegram.editMessageText( |
| 197 | chatId, |
| 198 | messageId, |
| 199 | view.text, |
| 200 | view.reply_markup |
| 201 | ); |
| 202 | return { |
| 203 | ok: true, |
| 204 | approvalRequired: true, |
| 205 | actionId: routed.actionId, |
| 206 | risk: routed.risk, |
| 207 | callbackAnswered, |
| 208 | edited: true, |
| 209 | res |
| 210 | }; |
| 211 | } |
| 212 | if (routed?.denied) { |
| 213 | return { |
| 214 | ok: false, |
| 215 | denied: true, |
| 216 | actionId: routed.actionId, |
| 217 | reason: routed.reason, |
| 218 | callbackAnswered |
| 219 | }; |
| 220 | } |
| 221 | |
| 222 | const view = routed || await this.menu.renderByCallback(data); |
| 223 | const res = await this.telegram.editMessageText( |
| 224 | chatId, |
| 225 | messageId, |
| 226 | view.text, |
| 227 | view.reply_markup |
| 228 | ); |
| 229 | |
| 230 | return { ok: true, edited: true, callbackAnswered, res }; |
| 231 | } catch (err) { |
| 232 | if (err.message && err.message.includes('message is not modified')) { |
| 233 | return { ok: true, unmodified: true, callbackAnswered }; |
| 234 | } |
| 235 | return { ok: false, error: err.message, callbackAnswered }; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | async handleMessage(message) { |
| 240 | const chatId = message.chat?.id; |
| 241 | const text = (message.text || '').trim(); |
| 242 | |
| 243 | if (!chatId) return { ok: false, error: 'Missing chat_id' }; |
| 244 | |
| 245 | if (text === '/start' || text === '/menu' || text.startsWith('/start')) { |
| 246 | const view = this.menu.getMainMenuView(); |
| 247 | const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup); |
| 248 | return { ok: true, command: 'start', res }; |
| 249 | } |
| 250 | |
| 251 | if (text === '/repos') { |
| 252 | const routed = await this.router.dispatchCommand(text, { isOwner: true }); |
| 253 | if (routed?.requiresApproval || routed?.denied || !routed) { |
| 254 | return { |
| 255 | ok: false, |
| 256 | command: 'repos', |
| 257 | error: routed?.reason || 'GitHub repository route unavailable' |
| 258 | }; |
| 259 | } |
| 260 | |
| 261 | const res = await this.telegram.sendMessage(chatId, routed.text, routed.reply_markup); |
| 262 | return { ok: true, command: 'repos', res }; |
| 263 | } |
| 264 | |
| 265 | if (text === '/status') { |
| 266 | const view = this.menu.getServiceStatusView(); |
| 267 | const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup); |
| 268 | return { ok: true, command: 'status', res }; |
| 269 | } |
| 270 | |
| 271 | const defaultText = `💡 برای دسترسی به منوهای پروژهها و ابزارهای Nexa Dev، دکمه زیر را لمس کنید:`; |
| 272 | const view = { |
| 273 | text: defaultText, |
| 274 | reply_markup: { |
| 275 | inline_keyboard: [ |
| 276 | [{ text: '⚡ باز کردن منوی اصلی Nexa Dev', callback_data: 'menu:main' }] |
| 277 | ] |
| 278 | } |
| 279 | }; |
| 280 | const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup); |
| 281 | return { ok: true, default: true, res }; |
| 282 | } |
| 283 | } |