Telegram API能否获取频道Boost数及高级订阅者占比?求Python实现方案
Telegram API 频道Boost数量与高级订阅者占比获取方案
一、功能可行性说明
Telegram API(含Bot API和底层TDLib)完全支持获取频道Boost数量,以及统计高级(Premium)订阅者占比:
- Boost数量:Bot API直接提供接口返回总数,TDLib可获取更详细的Boost明细
- 高级订阅者占比:Bot API需逐个查询成员状态(效率较低),TDLib支持批量获取成员信息,更适合统计占比
二、Python实现代码
1. 获取频道Boost数量(基于python-telegram-bot,Bot API)
先安装依赖:
pip install python-telegram-bot==20.7
代码示例:
from telegram import Bot from telegram.error import TelegramError import asyncio async def get_channel_boost_count(bot_token: str, channel_id: str): bot = Bot(token=bot_token) try: chat = await bot.get_chat(chat_id=channel_id) return chat.boost_count except TelegramError as e: print(f"获取Boost数量失败: {e}") return None # 调用示例(替换为你的机器人token和频道ID) boost_count = asyncio.run(get_channel_boost_count("YOUR_BOT_TOKEN", "-100XXXXXXXXX")) print(f"当前频道Boost数量: {boost_count}")
注意:机器人必须被添加为频道管理员,且拥有
manage_chat权限。
2. 获取高级订阅者占比(基于TDLib)
TDLib是Telegram底层库,支持更全面的成员数据查询,适合统计大量成员的Premium状态。
先安装依赖:
pip install pytdlib
代码示例:
from pytdlib.client import Client def get_premium_subscriber_ratio(api_id: int, api_hash: str, channel_id: str): client = Client( api_id=api_id, api_hash=api_hash, database_encryption_key="your_encryption_key" ) # 获取频道总成员数 total_members = client.get_chat_member_count(chat_id=int(channel_id)) if total_members == 0: return 0.0 premium_count = 0 offset = 0 limit = 100 # 每次查询100个成员,避免触发限制 while True: members = client.get_chat_members( chat_id=int(channel_id), offset=offset, limit=limit ) if not members: break # 统计Premium用户 for member in members: if member.user.is_premium: premium_count += 1 offset += limit return premium_count / total_members * 100 # 调用示例(替换为你的API ID、Hash和频道ID) ratio = get_premium_subscriber_ratio(123456, "your_api_hash", "-100XXXXXXXXX") print(f"高级订阅者占比: {ratio:.2f}%")
注意:
- 需要在Telegram开发者平台申请API ID和Hash
- 若频道成员过万,建议优化分页逻辑或使用异步查询避免超时
官方Boost文档补充说明
你参考的官方文档是底层API规范,Bot API是其简化封装:
- Bot API的
getChat接口直接返回boost_count字段,无需处理底层Boost列表逻辑 - 若需查看每个Boost的来源、有效期等明细,可调用Bot API的
getChatBoosts接口
内容的提问来源于stack exchange,提问作者RuuZak1
相关产品推荐
相关产品推荐

