Personal Telegram developer console for GitHub

fix: show expired approval feedback in Telegram (#17)

* test: surface expired approval in Telegram * fix: show rejected approval feedback in Telegram

davoodmehraban89 committed Oct 3, 2026 at 08:35 UTC 2c131ea5fc564faae20cd7aff1ac00831f7fe185
2 files changed +204 -1
src/bot.js
+43 -1
@@ -43,6 +43,34 @@ function approvalView({ label, token, repoId, runId }) {
43 };
44 }
45
46 +function approvalRejectedView(message, error) {
47 + const reason = String(error?.message || 'Approval rejected');
48 + const expired = /expired/i.test(reason);
49 + const used = /already used|replay/i.test(reason);
50 + const fallback = message?.reply_markup?.inline_keyboard
51 + ?.flat()
52 + .map(button => String(button?.callback_data || ''))
53 + .find(callback => callback && !callback.startsWith('approve:')) || 'menu:main';
54 +
55 + let detail = 'این تأیید معتبر نیست و هیچ تغییری اجرا نشد.';
56 + if (expired) detail = 'مهلت این تأیید تمام شده است و هیچ تغییری اجرا نشد.';
57 + if (used) detail = 'این تأیید قبلاً استفاده شده است و دوباره اجرا نمی‌شود.';
58 +
59 + return {
60 + text:
61 +`⚠️ <b>عملیات اجرا نشد</b>
62 +➖➖➖➖➖➖➖➖➖➖
63 +${detail}
64 +
65 +برای ادامه به صفحهٔ Run برگردید و در صورت نیاز یک تأیید تازه ایجاد کنید.`,
66 + reply_markup: {
67 + inline_keyboard: [
68 + [{ text: '↩️ بازگشت به Run', callback_data: fallback }]
69 + ]
70 + }
71 + };
72 +}
73 +
74 export class NexaDevBot {
75 constructor(env = {}) {
76 this.env = env;
@@ -123,7 +151,21 @@ export class NexaDevBot {
151 outcome: 'rejected',
152 error: error?.message || 'Approval rejected'
153 });
126 - throw error;
154 + const view = approvalRejectedView(message, error);
155 + const res = await this.telegram.editMessageText(
156 + chatId,
157 + messageId,
158 + view.text,
159 + view.reply_markup
160 + );
161 + return {
162 + ok: false,
163 + approvalRejected: true,
164 + error: error?.message || 'Approval rejected',
165 + callbackAnswered,
166 + edited: true,
167 + res
168 + };
169 }
170
171 this.audit.log({
test/approval-expiry-ui.test.js new
+161
@@ -0,0 +1,161 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import worker from '../src/index.js';
4 +
5 +function makeReplayNamespace() {
6 + const used = new Set();
7 + return {
8 + idFromName(name) {
9 + return String(name);
10 + },
11 + get(id) {
12 + return {
13 + async fetch(request) {
14 + const body = await request.json();
15 + const expiresAt = Number(body?.expiresAt);
16 + if (!Number.isFinite(expiresAt)) {
17 + return Response.json({ accepted: false, reason: 'invalid_expiry' }, { status: 400 });
18 + }
19 + if (expiresAt < Date.now()) {
20 + return Response.json({ accepted: false, reason: 'expired' }, { status: 410 });
21 + }
22 + const key = String(id);
23 + if (used.has(key)) {
24 + return Response.json({ accepted: false, reason: 'already_used' }, { status: 409 });
25 + }
26 + used.add(key);
27 + return Response.json({ accepted: true });
28 + }
29 + };
30 + }
31 + };
32 +}
33 +
34 +const env = {
35 + TELEGRAM_WEBHOOK_SECRET: 'right',
36 + OWNER_TELEGRAM_ID: '123',
37 + TELEGRAM_BOT_TOKEN: 'telegram-test-token',
38 + GITHUB_TOKEN: 'github-test-token',
39 + GITHUB_OWNER: 'davoodmehraban89',
40 + APPROVAL_SIGNING_SECRET: 'approval-test-secret',
41 + APPROVAL_REPLAY_GUARD: makeReplayNamespace()
42 +};
43 +
44 +function webhook(update) {
45 + return new Request('https://example.test/telegram/webhook', {
46 + method: 'POST',
47 + headers: {
48 + 'content-type': 'application/json',
49 + 'X-Telegram-Bot-Api-Secret-Token': 'right'
50 + },
51 + body: JSON.stringify(update)
52 + });
53 +}
54 +
55 +function json(data, status = 200) {
56 + return new Response(JSON.stringify(data), {
57 + status,
58 + headers: { 'content-type': 'application/json' }
59 + });
60 +}
61 +
62 +function installMock() {
63 + const telegramCalls = [];
64 + const githubCalls = [];
65 + const originalFetch = globalThis.fetch;
66 +
67 + globalThis.fetch = async (url, options = {}) => {
68 + const value = String(url);
69 + if (value.startsWith('https://api.telegram.org/')) {
70 + const method = value.split('/').at(-1);
71 + const body = options.body ? JSON.parse(options.body) : {};
72 + telegramCalls.push({ method, body });
73 + return json({ ok: true, result: { message_id: body.message_id || 700, ...body } });
74 + }
75 + if (value === 'https://api.github.com/repositories/1001') {
76 + githubCalls.push({ url: value, method: options.method || 'GET' });
77 + return json({ id: 1001, name: 'demo', full_name: 'davoodmehraban89/demo' });
78 + }
79 + if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs/55/rerun-failed-jobs') {
80 + githubCalls.push({ url: value, method: options.method || 'GET' });
81 + return new Response('', { status: 201 });
82 + }
83 + throw new Error(`Unexpected URL: ${value}`);
84 + };
85 +
86 + return {
87 + telegramCalls,
88 + githubCalls,
89 + restore() {
90 + globalThis.fetch = originalFetch;
91 + }
92 + };
93 +}
94 +
95 +async function sendUpdate(update) {
96 + const response = await worker.fetch(webhook(update), env);
97 + assert.equal(response.status, 200);
98 + return response.json();
99 +}
100 +
101 +test('expired approval gives visible Telegram feedback and no GitHub write', async () => {
102 + const mock = installMock();
103 + const originalNow = Date.now;
104 + let now = 1_800_000_000_000;
105 + Date.now = () => now;
106 +
107 + try {
108 + const menuMessage = { message_id: 900, chat: { id: 123 } };
109 + const issued = await sendUpdate({
110 + callback_query: {
111 + id: 'issue-expiring-approval',
112 + from: { id: 123 },
113 + data: 'rerun:1001:55',
114 + message: menuMessage
115 + }
116 + });
117 + assert.equal(issued.approvalRequired, true);
118 +
119 + const approvalEdit = mock.telegramCalls
120 + .filter(call => call.method === 'editMessageText')
121 + .at(-1);
122 + const approveButton = approvalEdit.body.reply_markup.inline_keyboard
123 + .flat()
124 + .find(button => String(button.callback_data || '').startsWith('approve:'));
125 + assert.ok(approveButton);
126 +
127 + const editCountBeforeExpiryClick = mock.telegramCalls
128 + .filter(call => call.method === 'editMessageText').length;
129 +
130 + now += 121_000;
131 + const expired = await sendUpdate({
132 + callback_query: {
133 + id: 'expired-approval-click',
134 + from: { id: 123 },
135 + data: approveButton.callback_data,
136 + message: {
137 + ...menuMessage,
138 + text: approvalEdit.body.text,
139 + reply_markup: approvalEdit.body.reply_markup
140 + }
141 + }
142 + });
143 +
144 + assert.equal(expired.ok, false);
145 + assert.equal(mock.githubCalls.filter(call => call.method === 'POST').length, 0);
146 +
147 + const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
148 + assert.equal(edits.length, editCountBeforeExpiryClick + 1, 'expired approval must visibly update the Telegram message');
149 + const feedback = edits.at(-1);
150 + assert.match(feedback.body.text, /منقضی|اعتبار|اجرا نشد/);
151 + assert.ok(
152 + feedback.body.reply_markup.inline_keyboard
153 + .flat()
154 + .some(button => button.callback_data === 'run:1001:55'),
155 + 'feedback must provide a safe route back to the workflow run'
156 + );
157 + } finally {
158 + Date.now = originalNow;
159 + mock.restore();
160 + }
161 +});