發佈 0.1.8:支援傳圖/傳檔給使用者,新增連網開關
摘要: 1. 交件匣機制:每輪 prompt 附系統規則,Codex 把要給使用者的檔案放進 工作目錄的 .tgcodex-outbox/,回覆後 bot 自動傳到 Telegram(圖片走 sendPhoto、其餘或超過 10MB 走 sendDocument)並清空,一輪上限 10 個檔案。 2. bot 設定新增「允許 Codex 連網」開關(-c sandbox_workspace_write.network_access), 開啟後可用 codex 的 imagegen 技能生圖,已實測產出 PNG 進交件匣。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+30
-1
@@ -86,6 +86,35 @@ function createTelegram(token) {
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// 上傳檔案到 Telegram:圖片走 sendPhoto,其餘(或 sendPhoto 失敗時)走 sendDocument
|
||||
async function sendFile(chatId, filePath, { caption, replyTo } = {}) {
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const name = path.basename(filePath);
|
||||
const isImage = /\.(png|jpe?g|gif|webp)$/i.test(name);
|
||||
const size = fs.statSync(filePath).size;
|
||||
|
||||
const upload = async (method, field) => {
|
||||
const form = new FormData();
|
||||
form.append('chat_id', String(chatId));
|
||||
if (caption) form.append('caption', caption.slice(0, 1000));
|
||||
if (replyTo) form.append('reply_parameters', JSON.stringify({ message_id: replyTo, allow_sending_without_reply: true }));
|
||||
form.append(field, new Blob([fs.readFileSync(filePath)]), name);
|
||||
const res = await fetch(`${API}/${method}`, { method: 'POST', body: form });
|
||||
const data = await res.json();
|
||||
if (!data.ok) throw new Error(`${method} 失敗:${data.description}`);
|
||||
return data.result;
|
||||
};
|
||||
|
||||
// sendPhoto 上限 10MB,且某些格式會被拒,失敗就退回用檔案傳
|
||||
if (isImage && size <= 9.5 * 1024 * 1024) {
|
||||
try {
|
||||
return await upload('sendPhoto', 'photo');
|
||||
} catch { /* fall through */ }
|
||||
}
|
||||
return upload('sendDocument', 'document');
|
||||
}
|
||||
|
||||
async function downloadFile(fileId, destPath) {
|
||||
const fs = require('fs');
|
||||
const info = await tg('getFile', { file_id: fileId });
|
||||
@@ -96,7 +125,7 @@ function createTelegram(token) {
|
||||
return destPath;
|
||||
}
|
||||
|
||||
return { tg, mdToTgHtml, sendMessage, sendSplit, editOrSplit, react, downloadFile };
|
||||
return { tg, mdToTgHtml, sendMessage, sendSplit, editOrSplit, react, downloadFile, sendFile };
|
||||
}
|
||||
|
||||
module.exports = { createTelegram };
|
||||
|
||||
Reference in New Issue
Block a user