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

使用Telethon调用Telegram API获取群组成员数时遇AttributeError问题

解决Telethon调用GetFullChannelRequest报错AttributeError的问题

错误原因

你遇到的AttributeError: 'coroutine' object has no attribute 'full_chat',是因为客户端未完成完整的登录流程,导致调用client(GetFullChannelRequest(...))返回的是协程对象,而非实际的频道完整信息。

修正方案

方案1:使用with语句管理客户端(推荐)

这种写法会自动处理连接、登录和断开,代码更简洁安全:

from telethon.sync import TelegramClient
from telethon.tl.functions.channels import GetFullChannelRequest

# 替换为你的实际参数
api_id = 你的API_ID
api_hash = '你的API_HASH'
channel_name = '@目标频道用户名'

with TelegramClient('session_name', api_id, api_hash) as client:
    channel_entity = client.get_entity(channel_name)
    full_channel = client(GetFullChannelRequest(channel=channel_entity))
    print(full_channel.full_chat.participants_count)

方案2:手动调用start()完成登录

如果不想用with语句,需确保调用client.start()完成登录流程:

from telethon.sync import TelegramClient
from telethon.tl.functions.channels import GetFullChannelRequest

api_id = 你的API_ID
api_hash = '你的API_HASH'
channel_name = '@目标频道用户名'

client = TelegramClient('session_name', api_id, api_hash)
client.start()  # 自动处理登录验证(如需要验证码会提示输入)

channel_entity = client.get_entity(channel_name)
full_channel = client(GetFullChannelRequest(channel=channel_entity))
print(full_channel.full_chat.participants_count)

client.disconnect()

额外注意点

  • 移除代码中无用的from telethon import client导入,避免混淆。
  • 确保你的API_ID和API_HASH是从Telegram官方平台申请的有效凭证。

内容的提问来源于stack exchange,提问作者Osman Bernhard

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 09:50:28