You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用gogram+tdlib操作Telegram频道失败的问题求助

问题解决方案
  • 修正频道ID的使用方式
    Telegram私有频道ID以-100开头,但gogram的部分API需要你传入去掉-100后的纯数字ID,并且明确指定为频道类型的Peer,而非直接传原始数字。比如你报错中的-10020055****,需提取后面的20055****,构造ChannelPeer对象传入:

    channelPeer := &telegram.ChannelPeer{
        ChannelID: 20055****, // 去掉-100后的频道数字ID
    }
    // 加入频道
    err = client.JoinChannel(channelPeer)
    if err != nil {
        fmt.Println(err)
        return
    }
    // 获取频道信息
    tgchannel, err := client.GetChannel(channelPeer)
    if err != nil {
        fmt.Println(err)
        return
    }
    

    直接传数字ID时,库会默认识别为用户ID,导致找不到对应peer报错。

  • 优先使用频道username(如果公开)
    如果频道有公开的username,直接调用GetChannelByUsername方法可跳过ID转换问题,同时tdlib会自动缓存频道信息,避免缓存缺失报错:

    tgchannel, err := client.GetChannelByUsername("your_channel_username")
    if err != nil {
        fmt.Println(err)
        return
    }
    
  • 等待tdlib完成会话同步
    登录后不要立即执行频道操作,需等待tdlib完成授权状态同步到AuthorizationStateReady,确保本地缓存加载完成。可通过注册更新处理器实现:

    client.AddHandler(func(update *telegram.Update) {
        if authState, ok := update.Update.(*telegram.UpdateAuthorizationState); ok {
            if authState.AuthorizationState.GetAuthorizationStateEnum() == telegram.AuthorizationStateReady {
                go func() {
                    // 在这里执行JoinChannel或GetChannel操作
                    channelPeer := &telegram.ChannelPeer{ChannelID: 20055****}
                    err = client.JoinChannel(channelPeer)
                    if err != nil {
                        fmt.Println(err)
                        return
                    }
                    tgchannel, err := client.GetChannel(channelPeer)
                    if err != nil {
                        fmt.Println(err)
                        return
                    }
                    // 处理频道数据
                }()
            }
        }
    })
    
    client.Idle()
    

内容的提问来源于stack exchange,提问作者mirko sailorm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:18:25