Personal Telegram developer console for GitHub
feat: wire signed Telegram approval flow
davoodmehraban89 committed
Oct 3, 2026 at 05:53 UTC
e2879b5a3c85091747cf5d95ac52452504ecb1f4
1 file changed
+109
-3
src/bot.js
+109
-3
@@ -3,16 +3,54 @@ 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 { AuditLogger } from './core/audit.js';
8
import { PluginRouter } from './core/router.js';
9
import { createGitHubPlugin } from './plugins/github/plugin.js';
10
11
+function approvalSourceCallback(payload = {}) {
12
+ const repoId = payload.input?.repoId;
13
+ const runId = payload.input?.runId;
14
+ if (!Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(runId))) {
15
+ throw new Error('Approval input is invalid');
16
+ }
17
+
18
+ if (payload.actionId === 'github.workflow.run.rerun_failed') {
19
+ return `rerun:${repoId}:${runId}`;
20
+ }
21
+ if (payload.actionId === 'github.workflow.run.cancel') {
22
+ return `cancelrun:${repoId}:${runId}`;
23
+ }
24
+ throw new Error('Approval action is not supported');
25
+}
26
+
27
+function approvalView({ label, token, repoId, runId }) {
28
+ return {
29
+ text:
30
+`⚠️ <b>تأیید عملیات لازم است</b>
31
+➖➖➖➖➖➖➖➖➖➖
32
+عملیات: <b>${label}</b>
33
+این عملیات روی GitHub تغییر ایجاد میکند و فقط پس از تأیید شما اجرا میشود.
34
+
35
+اگر مطمئن هستید، «تأیید و اجرا» را بزنید.`,
36
+ reply_markup: {
37
+ inline_keyboard: [
38
+ [{ text: '✅ تأیید و اجرا', callback_data: `approve:${token}` }],
39
+ [{ text: '↩️ انصراف', callback_data: `run:${repoId}:${runId}` }]
40
+ ]
41
+ }
42
+ };
43
+}
44
+
45
export class NexaDevBot {
46
constructor(env = {}) {
47
this.env = env;
48
this.telegram = new TelegramClient(env.TELEGRAM_BOT_TOKEN || '');
49
this.github = new GitHubService(env.GITHUB_TOKEN || '', env.GITHUB_OWNER || 'davoodmehraban89');
50
this.menu = new MenuManager(this.github);
51
+ this.approvals = new ApprovalService({
52
+ secret: env.APPROVAL_SIGNING_SECRET || ''
53
+ });
54
55
this.githubPlugin = createGitHubPlugin({
56
github: this.github,
@@ -24,7 +62,8 @@ export class NexaDevBot {
62
secrets: [
63
env.TELEGRAM_BOT_TOKEN,
64
env.TELEGRAM_WEBHOOK_SECRET,
27
- env.GITHUB_TOKEN
65
+ env.GITHUB_TOKEN,
66
+ env.APPROVAL_SIGNING_SECRET
67
]
68
});
69
this.router = new PluginRouter({
@@ -61,6 +100,7 @@ export class NexaDevBot {
100
const message = query.message;
101
const chatId = message?.chat?.id;
102
const messageId = message?.message_id;
103
+ const ownerId = query.from?.id;
104
105
// Callback acknowledgement is best-effort. A stale/expired callback must not
106
// block rendering and editing the existing menu message.
@@ -76,16 +116,82 @@ export class NexaDevBot {
116
}
117
118
try {
119
+ if (data.startsWith('approve:')) {
120
+ if (ownerId === undefined || ownerId === null) {
121
+ throw new Error('Approval owner is required');
122
+ }
123
+ const token = data.slice('approve:'.length);
124
+ const payload = await this.approvals.consume(token, {
125
+ ownerId: String(ownerId),
126
+ pluginId: 'github',
127
+ risk: 'WRITE'
128
+ });
129
+ const sourceCallback = approvalSourceCallback(payload);
130
+ const routed = await this.router.dispatchCallback(sourceCallback, {
131
+ isOwner: true,
132
+ approved: true
133
+ });
134
+ if (!routed || routed.requiresApproval || routed.denied) {
135
+ throw new Error('Approved action could not be dispatched');
136
+ }
137
+
138
+ const res = await this.telegram.editMessageText(
139
+ chatId,
140
+ messageId,
141
+ routed.text,
142
+ routed.reply_markup
143
+ );
144
+ return {
145
+ ok: true,
146
+ approved: true,
147
+ actionId: payload.actionId,
148
+ callbackAnswered,
149
+ edited: true,
150
+ res
151
+ };
152
+ }
153
+
154
// GitHub callbacks pass through the plugin policy boundary. Non-GitHub
155
// callbacks remain on the existing compatibility router in Foundation v1.
156
const routed = await this.router.dispatchCallback(data, { isOwner: true });
157
if (routed?.requiresApproval) {
158
+ const approval = routed.approval;
159
+ const repoId = approval?.input?.repoId;
160
+ const runId = approval?.input?.runId;
161
+ if (!approval || !Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(runId))) {
162
+ throw new Error('Approval metadata is missing');
163
+ }
164
+ if (ownerId === undefined || ownerId === null) {
165
+ throw new Error('Approval owner is required');
166
+ }
167
+
168
+ const token = await this.approvals.issue({
169
+ pluginId: routed.pluginId,
170
+ actionId: routed.actionId,
171
+ risk: routed.risk,
172
+ ownerId: String(ownerId),
173
+ input: approval.input
174
+ });
175
+ const view = approvalView({
176
+ label: approval.label || routed.actionId,
177
+ token,
178
+ repoId,
179
+ runId
180
+ });
181
+ const res = await this.telegram.editMessageText(
182
+ chatId,
183
+ messageId,
184
+ view.text,
185
+ view.reply_markup
186
+ );
187
return {
84
- ok: false,
188
+ ok: true,
189
approvalRequired: true,
190
actionId: routed.actionId,
191
risk: routed.risk,
88
- callbackAnswered
192
+ callbackAnswered,
193
+ edited: true,
194
+ res
195
};
196
}
197
if (routed?.denied) {