Telethon是否支持获取聊天所有邀请链接?如何调用核心API方法?
在Telethon中获取聊天所有邀请链接的方法
Telethon没有提供和Pyrogram的GetExportedChatInvites完全对应的高层封装方法,但可以直接调用Telegram核心API的messages.getExportedChatInvites来实现需求。
基础调用示例
通过GetExportedChatInvitesRequest直接调用核心API,获取指定聊天的邀请链接:
from telethon.tl.functions.messages import GetExportedChatInvitesRequest # chat_id 可传入聊天ID、用户名或实体对象 result = await client(GetExportedChatInvitesRequest( peer=chat_id, limit=100, # 单次请求的最大返回数量 revoked=False # False仅返回有效链接,True包含已撤销的链接 )) # 遍历输出结果 for invite in result.invites: print(f"邀请链接:{invite.link}") print(f"创建者ID:{invite.admin_id}") print(f"过期时间:{invite.expire_date}\n")
分页获取所有链接
如果聊天中的邀请链接数量超过单次请求的limit上限,需要通过分页参数循环调用以获取全部链接:
from telethon.tl.functions.messages import GetExportedChatInvitesRequest chat_id = "目标聊天的ID或用户名" all_invites = [] offset_date = None offset_link = "" while True: result = await client(GetExportedChatInvitesRequest( peer=chat_id, limit=100, revoked=False, offset_date=offset_date, offset_link=offset_link )) if not result.invites: break all_invites.extend(result.invites) # 更新分页标记,用于下一次请求 last_invite = result.invites[-1] offset_date = last_invite.date offset_link = last_invite.link # 处理所有获取到的邀请链接 for invite in all_invites: print(f"链接:{invite.link} | 创建时间:{invite.date}")
内容的提问来源于stack exchange,提问作者Даниил К
相关产品推荐
相关产品推荐

