Personal Telegram developer console for GitHub
feat/plugin-foundation-v1
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 { AuditLogger } from './core/audit.js'; |
| 7 | import { PluginRouter } from './core/router.js'; |
| 8 | import { createGitHubPlugin } from './plugins/github/plugin.js'; |
| 9 | |
| 10 | export class NexaDevBot { |
| 11 | constructor(env = {}) { |
| 12 | this.env = env; |
| 13 | this.telegram = new TelegramClient(env.TELEGRAM_BOT_TOKEN || ''); |
| 14 | this.github = new GitHubService(env.GITHUB_TOKEN || '', env.GITHUB_OWNER || 'davoodmehraban89'); |
| 15 | this.menu = new MenuManager(this.github); |
| 16 | |
| 17 | this.githubPlugin = createGitHubPlugin({ |
| 18 | github: this.github, |
| 19 | menu: this.menu |
| 20 | }); |
| 21 | this.registry = new PluginRegistry([this.githubPlugin]); |
| 22 | this.policy = new ActionPolicy(); |
| 23 | this.audit = new AuditLogger({ |
| 24 | secrets: [ |
| 25 | env.TELEGRAM_BOT_TOKEN, |
| 26 | env.TELEGRAM_WEBHOOK_SECRET, |
| 27 | env.GITHUB_TOKEN |
| 28 | ] |
| 29 | }); |
| 30 | this.router = new PluginRouter({ |
| 31 | registry: this.registry, |
| 32 | policy: this.policy, |
| 33 | audit: this.audit |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Process incoming Telegram webhook update. |
| 39 | * Owner authorization is enforced at the Worker boundary before this bot is |
| 40 | * constructed, so routed actions use an owner-authorized internal context. |
| 41 | */ |
| 42 | async handleUpdate(update) { |
| 43 | if (!update) return { ok: false, error: 'Empty update' }; |
| 44 | |
| 45 | // 1. Handle Inline Keyboard Callback Queries (In-Place Message Editing) |
| 46 | if (update.callback_query) { |
| 47 | return this.handleCallbackQuery(update.callback_query); |
| 48 | } |
| 49 | |
| 50 | // 2. Handle Text Messages and Commands |
| 51 | if (update.message) { |
| 52 | return this.handleMessage(update.message); |
| 53 | } |
| 54 | |
| 55 | return { ok: true, ignored: true }; |
| 56 | } |
| 57 | |
| 58 | async handleCallbackQuery(query) { |
| 59 | const callbackId = query.id; |
| 60 | const data = query.data || 'menu:main'; |
| 61 | const message = query.message; |
| 62 | const chatId = message?.chat?.id; |
| 63 | const messageId = message?.message_id; |
| 64 | |
| 65 | // Callback acknowledgement is best-effort. A stale/expired callback must not |
| 66 | // block rendering and editing the existing menu message. |
| 67 | let callbackAnswered = true; |
| 68 | try { |
| 69 | await this.telegram.answerCallbackQuery(callbackId); |
| 70 | } catch { |
| 71 | callbackAnswered = false; |
| 72 | } |
| 73 | |
| 74 | if (!chatId || !messageId) { |
| 75 | return { ok: false, error: 'Missing message context for callback', callbackAnswered }; |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | // GitHub callbacks pass through the plugin policy boundary. Non-GitHub |
| 80 | // callbacks remain on the existing compatibility router in Foundation v1. |
| 81 | const routed = await this.router.dispatchCallback(data, { isOwner: true }); |
| 82 | if (routed?.requiresApproval) { |
| 83 | return { |
| 84 | ok: false, |
| 85 | approvalRequired: true, |
| 86 | actionId: routed.actionId, |
| 87 | risk: routed.risk, |
| 88 | callbackAnswered |
| 89 | }; |
| 90 | } |
| 91 | if (routed?.denied) { |
| 92 | return { |
| 93 | ok: false, |
| 94 | denied: true, |
| 95 | actionId: routed.actionId, |
| 96 | reason: routed.reason, |
| 97 | callbackAnswered |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | const view = routed || await this.menu.renderByCallback(data); |
| 102 | |
| 103 | // Edit the existing message in-place to prevent chat flooding |
| 104 | const res = await this.telegram.editMessageText( |
| 105 | chatId, |
| 106 | messageId, |
| 107 | view.text, |
| 108 | view.reply_markup |
| 109 | ); |
| 110 | |
| 111 | return { ok: true, edited: true, callbackAnswered, res }; |
| 112 | } catch (err) { |
| 113 | // Ignore 'message is not modified' error in Telegram |
| 114 | if (err.message && err.message.includes('message is not modified')) { |
| 115 | return { ok: true, unmodified: true, callbackAnswered }; |
| 116 | } |
| 117 | return { ok: false, error: err.message, callbackAnswered }; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | async handleMessage(message) { |
| 122 | const chatId = message.chat?.id; |
| 123 | const text = (message.text || '').trim(); |
| 124 | |
| 125 | if (!chatId) return { ok: false, error: 'Missing chat_id' }; |
| 126 | |
| 127 | // Command: /start or /menu |
| 128 | if (text === '/start' || text === '/menu' || text.startsWith('/start')) { |
| 129 | const view = this.menu.getMainMenuView(); |
| 130 | const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup); |
| 131 | return { ok: true, command: 'start', res }; |
| 132 | } |
| 133 | |
| 134 | // GitHub shortcut command routed through the plugin kernel. |
| 135 | if (text === '/repos') { |
| 136 | const routed = await this.router.dispatchCommand(text, { isOwner: true }); |
| 137 | if (routed?.requiresApproval || routed?.denied || !routed) { |
| 138 | return { |
| 139 | ok: false, |
| 140 | command: 'repos', |
| 141 | error: routed?.reason || 'GitHub repository route unavailable' |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | const res = await this.telegram.sendMessage(chatId, routed.text, routed.reply_markup); |
| 146 | return { ok: true, command: 'repos', res }; |
| 147 | } |
| 148 | |
| 149 | // Command: /status |
| 150 | if (text === '/status') { |
| 151 | const view = this.menu.getServiceStatusView(); |
| 152 | const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup); |
| 153 | return { ok: true, command: 'status', res }; |
| 154 | } |
| 155 | |
| 156 | // Default response: Offer interactive menu |
| 157 | const defaultText = `💡 برای دسترسی به منوهای پروژهها و ابزارهای Nexa Dev، دکمه زیر را لمس کنید:`; |
| 158 | const view = { |
| 159 | text: defaultText, |
| 160 | reply_markup: { |
| 161 | inline_keyboard: [ |
| 162 | [{ text: '⚡ باز کردن منوی اصلی Nexa Dev', callback_data: 'menu:main' }] |
| 163 | ] |
| 164 | } |
| 165 | }; |
| 166 | const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup); |
| 167 | return { ok: true, default: true, res }; |
| 168 | } |
| 169 | } |