Files
telegram-management-system/FIX_SUMMARY.md
你的用户名 237c7802e5
Some checks failed
Deploy / deploy (push) Has been cancelled
Initial commit: Telegram Management System
Full-stack web application for Telegram management
- Frontend: Vue 3 + Vben Admin
- Backend: NestJS
- Features: User management, group broadcast, statistics

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 15:37:50 +08:00

3.3 KiB
Raw Permalink Blame History

获取对话列表修复总结

问题描述

用户反馈"获取对话列表失败",点击聊天功能后无法加载对话列表。

根本原因

  1. 主要问题BaseClient.js 中的 getDialogs 方法使用了硬编码的错误参数

    • offsetPeer: "username" - 错误!应该是 InputPeer 对象
    • 其他参数也使用了不合理的默认值(如 43
  2. 次要问题

    • MTProxy 代理配置问题
    • 参数类型转换问题(字符串需要转换为数字)
    • 缺少 getMessages 方法

修复内容

1. 修复 getDialogs 方法 (/backend/src/client/BaseClient.js)

// 修复前
async getDialogs(){
    const result = await this.invoke(
        new Api.messages.GetDialogs({
            offsetDate: 43,
            offsetId: 43,
            offsetPeer: "username",  // 错误!
            limit: 100,
            hash: 0,
            excludePinned: true,
            folderId: 43,
        })
    );
}

// 修复后
async getDialogs(options = {}){
    try {
        const params = {
            offsetDate: Number(options.offsetDate) || 0,
            offsetId: Number(options.offsetId) || 0,
            offsetPeer: options.offsetPeer || new Api.InputPeerEmpty(),
            limit: Number(options.limit) || 100,
            hash: Number(options.hash) || 0,
            excludePinned: Boolean(options.excludePinned) || false,
            folderId: options.folderId ? Number(options.folderId) : undefined
        };
        
        // 移除 undefined 的参数
        Object.keys(params).forEach(key => {
            if (params[key] === undefined) {
                delete params[key];
            }
        });
        
        const result = await this.invoke(
            new Api.messages.GetDialogs(params)
        );
        return result;
    } catch (error) {
        this.logger.error("getDialogs error: " + error.message);
        throw error;
    }
}

2. 修复代理配置问题 (/backend/src/client/ClientBus.js)

// 只有当代理有用户名和密码时才使用代理
if(proxyObj.username && proxyObj.password) {
    tgClientParam.proxy={
        useWSS: false,
        ip:proxyObj.ip,
        port:proxyObj.port,
        username:proxyObj.username,
        password:proxyObj.password,
        socksType: 5,
        timeout:10,
        MTProxy: false,
    };
} else {
    this.logger.info("代理没有用户名密码,不使用代理连接");
}

3. 修复路由中的自动上线功能 (/backend/src/routers/TgAccountRouter.js)

  • 添加了自动上线逻辑,当账号未连接时自动连接
  • 修复了对话列表数据的解析逻辑

测试结果

从日志中可以看到:

  • 成功连接到 Telegram
  • 成功获取对话列表12个对话
  • 自动上线功能正常工作
  • getMessages 方法尚未实现(这是下一步需要添加的功能)

关键修复点

  1. offsetPeer 必须是 InputPeer 对象,不能是字符串
  2. 参数类型必须正确:数字参数不能传字符串
  3. 代理配置:当代理缺少认证信息时应该跳过代理
  4. 默认值要合理:使用 0 而不是随意的数字如 43

后续工作

  1. 实现 getMessages 方法以支持消息获取
  2. 实现 sendMessage 方法以支持发送消息
  3. 优化错误处理和用户提示