-
Notifications
You must be signed in to change notification settings - Fork 433
Open
Description
🐛 问题描述
在使用 Telegram Bot 通知功能时,由于网络原因(Telegram API 在国内被墙),无法正常发送通知消息。即使系统已配置代理,CodePilot 也无法连接到 Telegram API。
🔍 问题分析
经过代码审查,发现 src/lib/bridge/adapters/telegram-utils.ts 中的 callTelegramApi 函数直接使用原生 fetch() 访问 Telegram API,没有支持代理配置:
export async function callTelegramApi(
botToken: string,
method: string,
params: Record<string, unknown>,
): Promise<TelegramSendResult> {
const url = `${TELEGRAM_API}/bot${botToken}/${method}`;
const res = await fetch(url, { // ← 直接访问,没有代理选项
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params),
});
// ...
}🌍 环境信息
- 操作系统: macOS
- 网络环境: 国内,需要代理访问 Telegram API
- 代理软件: Clash Verge(HTTP 端口 7897,SOCKS5 端口 7897)
- CodePilot 版本: 0.27.1
✅ 验证步骤
- 配置 Telegram Bot(Bot Token 和 Chat ID)
- 点击"验证"按钮 - 可能成功(可能使用了不同的代码路径)
- 触发实际的消息发送(如任务完成通知)- 失败(无法连接到
api.telegram.org)
💡 建议解决方案
方案 1:添加代理配置选项(推荐)
在 Telegram 设置中添加代理配置字段:
// 新增配置项
const TELEGRAM_KEYS = [
'telegram_bot_token',
'telegram_chat_id',
'telegram_enabled',
// ... 其他配置
'telegram_proxy_enabled', // 新增:是否启用代理
'telegram_proxy_url', // 新增:代理地址 (如 http://127.0.0.1:7897)
];然后修改 callTelegramApi 函数,使用 proxy-agent 库:
import { ProxyAgent } from 'proxy-agent';
const proxyAgent = new ProxyAgent();
export async function callTelegramApi(...) {
const config = getTelegramConfig();
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params),
// 添加代理支持
dispatcher: proxyAgent,
});
}方案 2:支持环境变量
自动读取系统环境变量(HTTP_PROXY、HTTPS_PROXY),无需用户手动配置。
方案 3:使用系统代理
使用 Electron 的 session.setProxy 方法为整个应用设置代理。
📚 参考资料
- Node.js
undicifetch 支持代理: https://undici.nodejs.org/#/docs/api/ProxyAgent proxy-agent库: https://github.com/TooTallNate/proxy-agent- Electron 代理设置: https://www.electronjs.org/docs/latest/api/session#sessionsetproxyconfig
🔥 影响范围
所有需要使用代理访问 Telegram API 的用户,特别是在国内网络环境下。
🙏 额外说明
我已验证通过代理可以正常访问 Telegram API(使用 curl -x http://127.0.0.1:7897 https://api.telegram.org 成功),因此确认问题在于 CodePilot 没有使用代理配置。
期待您的回复和修复!🚀
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels