Personal Telegram developer console for GitHub
feat: make file upload the primary workspace draft input
davoodmehraban89 committed
Oct 3, 2026 at 14:37 UTC
6dd1ae822137f753a05028347c5907f9c925ba01
1 file changed
+34
-15
src/plugins/github/workspace.js
+34
-15
@@ -37,6 +37,8 @@ function parseCallback(data) {
37
if (match) return { actionId: 'github.workspace.create', kind: 'edit', refId: match[1] };
38
match = value.match(new RegExp(`^ws:(${HANDLE_10})$`));
39
if (match) return { actionId: 'github.workspace.view', kind: 'workspace', workspaceId: match[1] };
40
+ match = value.match(new RegExp(`^wfile:(${HANDLE_10})$`));
41
+ if (match) return { actionId: 'github.workspace.input.prepare', kind: 'input', workspaceId: match[1], mode: 'draft_file' };
42
match = value.match(new RegExp(`^wtext:(${HANDLE_10})$`));
43
if (match) return { actionId: 'github.workspace.input.prepare', kind: 'input', workspaceId: match[1], mode: 'draft' };
44
match = value.match(new RegExp(`^wmsg:(${HANDLE_10})$`));
@@ -60,9 +62,10 @@ function parseCallback(data) {
62
function workspaceButtons(workspace) {
63
return [
64
[
63
- { text: '📝 متن Draft', callback_data: `wtext:${workspace.id}` },
64
- { text: '✍️ پیام Commit', callback_data: `wmsg:${workspace.id}` }
65
+ { text: '📎 بارگذاری فایل', callback_data: `wfile:${workspace.id}` },
66
+ { text: '📝 Paste متن', callback_data: `wtext:${workspace.id}` }
67
],
68
+ [{ text: '✍️ پیام Commit', callback_data: `wmsg:${workspace.id}` }],
69
[
70
{ text: '🔍 Diff', callback_data: `wdiff:${workspace.id}` },
71
{ text: '✅ آماده Commit', callback_data: `wready:${workspace.id}` }
@@ -183,7 +186,7 @@ export function createWorkspaceController({ github, workspaces } = {}) {
186
? `<code>${escapeHtml(short(workspace.commitMessage, 120))}</code>`
187
: '<i>ثبت نشده</i>';
188
return {
186
- text: `📝 <b>Repository Workspace</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\nوضعیت: <b>${escapeHtml(workspace.state || 'DRAFT')}</b>\nBase: <code>${escapeHtml(String(workspace.baseHeadSha || '').slice(0, 10))}</code>\nتغییر محلی: <b>${changed ? 'دارد' : 'ندارد'}</b>\nپیام Commit: ${commitMessage}\n\nتا قبل از تأیید نهایی، هیچ تغییری در GitHub انجام نمیشود.`,
189
+ text: `📝 <b>Repository Workspace</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\nوضعیت: <b>${escapeHtml(workspace.state || 'DRAFT')}</b>\nBase: <code>${escapeHtml(String(workspace.baseHeadSha || '').slice(0, 10))}</code>\nتغییر محلی: <b>${changed ? 'دارد' : 'ندارد'}</b>\nپیام Commit: ${commitMessage}\n\nروش پیشنهادی: فایل متنی جایگزین را بارگذاری کنید؛ Paste متن همچنان بهعنوان مسیر جایگزین در دسترس است. تا قبل از تأیید نهایی، هیچ تغییری در GitHub انجام نمیشود.`,
190
reply_markup: { inline_keyboard: workspaceButtons(workspace) }
191
};
192
};
@@ -191,38 +194,50 @@ export function createWorkspaceController({ github, workspaces } = {}) {
194
const prepareInput = async (workspaceId, mode, ownerId) => {
195
const workspace = await requireWorkspace(workspaceId, ownerId);
196
await workspaces.setPendingInput({ ownerId, workspaceId, mode });
197
+ const fileMode = mode === 'draft_file';
198
const draftMode = mode === 'draft';
199
return {
196
- text: draftMode
197
- ? `📝 <b>متن جدید Draft را ارسال کنید</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\n\nکل محتوای فایل جایگزین میشود. سقف: 3,000 کاراکتر Unicode و 12,000 بایت UTF-8. پیام بعدی فقط یکبار مصرف میشود.`
198
- : `✍️ <b>پیام Commit را ارسال کنید</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\n\nپیام باید غیرخالی و حداکثر 120 کاراکتر باشد. پیام بعدی فقط یکبار مصرف میشود.`,
200
+ text: fileMode
201
+ ? `📎 <b>فایل متنی جایگزین را ارسال کنید</b>\nمقصد در مخزن: <code>${escapeHtml(workspace.path)}</code>\n\nفایل ارسالشده فقط بهعنوان محتوای جایگزین Draft استفاده میشود؛ نام فایل مقصد را تغییر نمیدهد. سقف: 12,000 بایت و UTF-8 معتبر. فایلهای باینری، LFS یا محتوای غیرمجاز رد میشوند.`
202
+ : draftMode
203
+ ? `📝 <b>متن جدید Draft را ارسال کنید</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\n\nکل محتوای فایل جایگزین میشود. سقف: 3,000 کاراکتر Unicode و 12,000 بایت UTF-8. پیام بعدی فقط یکبار مصرف میشود.`
204
+ : `✍️ <b>پیام Commit را ارسال کنید</b>\nفایل: <code>${escapeHtml(workspace.path)}</code>\n\nپیام باید غیرخالی و حداکثر 120 کاراکتر باشد. پیام بعدی فقط یکبار مصرف میشود.`,
205
reply_markup: { inline_keyboard: [[{ text: '↩️ بازگشت به Draft', callback_data: `ws:${workspace.id}` }]] }
206
};
207
};
208
203
- const applyPendingInput = async ({ pending, text }, ownerId) => {
209
+ const applyPendingInput = async ({ pending, text, file }, ownerId) => {
210
const workspaceId = String(pending?.workspaceId || '');
211
const mode = String(pending?.mode || '');
206
- if (!new RegExp(`^${HANDLE_10}$`).test(workspaceId) || !['draft', 'commit_message'].includes(mode)) {
212
+ if (!new RegExp(`^${HANDLE_10}$`).test(workspaceId) || !['draft', 'draft_file', 'commit_message'].includes(mode)) {
213
throw new Error('Pending workspace input is invalid');
214
}
215
216
+ if (mode === 'draft_file' && (!file || typeof file.text !== 'string')) {
217
+ throw new Error('A UTF-8 Telegram document is required for this workspace input');
218
+ }
219
+
220
const consumed = await workspaces.consumePendingInput({ ownerId, workspaceId, mode });
221
if (!consumed) throw new Error('Pending workspace input was already consumed or expired');
222
const workspace = await requireWorkspace(workspaceId, ownerId);
223
214
- if (mode === 'draft') {
215
- const policy = validateEditableFile({ path: workspace.path, text });
224
+ if (mode === 'draft' || mode === 'draft_file') {
225
+ const inputText = mode === 'draft_file' ? file.text : text;
226
+ const inputBytes = mode === 'draft_file' ? Number(file.byteLength) : undefined;
227
+ const policy = validateEditableFile({ path: workspace.path, text: inputText, byteLength: inputBytes });
228
if (!policy.editable) {
229
return {
218
- text: `⚠️ <b>Draft تغییر نکرد</b>\n${escapeHtml(policy.reason || 'متن ارسالشده مجاز نیست.')}\n\nاین ورودی مصرف شد؛ برای تلاش دوباره «متن Draft» را بزنید.`,
230
+ text: `⚠️ <b>Draft تغییر نکرد</b>\n${escapeHtml(policy.reason || 'محتوای ارسالشده مجاز نیست.')}\n\nاین ورودی مصرف شد؛ برای تلاش دوباره ${mode === 'draft_file' ? '«بارگذاری فایل»' : '«Paste متن»'} را بزنید.`,
231
reply_markup: { inline_keyboard: [[{ text: '📝 بازگشت به Draft', callback_data: `ws:${workspace.id}` }]] }
232
};
233
}
234
const digest = await digestText(policy.text);
235
const updated = await workspaces.updateDraft({ ownerId, workspaceId, text: policy.text, digest });
236
+ const source = mode === 'draft_file'
237
+ ? `\nفایل ورودی: <code>${escapeHtml(short(file.name || 'uploaded file', 100))}</code>`
238
+ : '';
239
return {
225
- text: `✅ <b>Draft محلی ذخیره شد</b>\nفایل: <code>${escapeHtml(updated.path)}</code>\nکاراکتر: <b>${policy.charLength}</b> · بایت UTF-8: <b>${policy.byteLength}</b>\n\nهنوز هیچ تغییری در GitHub انجام نشده است.`,
240
+ text: `✅ <b>Draft محلی ذخیره شد</b>\nفایل مقصد: <code>${escapeHtml(updated.path)}</code>${source}\nکاراکتر: <b>${policy.charLength}</b> · بایت UTF-8: <b>${policy.byteLength}</b>\n\nهنوز هیچ تغییری در GitHub انجام نشده است.`,
241
reply_markup: { inline_keyboard: workspaceButtons(updated) }
242
};
243
}
@@ -375,12 +390,16 @@ export function createWorkspaceController({ github, workspaces } = {}) {
390
391
resolveMessage(payload, context = {}) {
392
const pending = payload?.pending;
378
- if (!pending || !['draft', 'commit_message'].includes(String(pending.mode || ''))) return null;
393
+ if (!pending || !['draft', 'draft_file', 'commit_message'].includes(String(pending.mode || ''))) return null;
394
if (!new RegExp(`^${HANDLE_10}$`).test(String(pending.workspaceId || ''))) return null;
395
return {
396
actionId: 'github.workspace.input.apply',
382
- execute: async () => applyPendingInput({ pending, text: String(payload?.text ?? '') }, ownerFrom(context))
397
+ execute: async () => applyPendingInput({
398
+ pending,
399
+ text: String(payload?.text ?? ''),
400
+ file: payload?.file && typeof payload.file === 'object' ? payload.file : null
401
+ }, ownerFrom(context))
402
};
403
}
404
};
386
-}
405
+}
\ No newline at end of file