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

使用Python+Telethon能否获取Telegram群组内成员的入群日期?

可以实现该需求。你现有代码拿不到入群日期,是因为直接读取的是用户User对象的属性,Telethon中群成员的入群时间存储在ChannelParticipant对象的date字段中,调整获取成员和写入的逻辑即可:

注意事项

  • 该方法仅对超级群、公开频道生效,普通小型私有群组无法批量获取成员入群时间,需通过遍历群内历史入群通知类服务消息的方式匹配对应成员的入群时间。
  • 读取到的date字段默认是UTC标准时间,可根据需要自行转换为对应时区的本地时间。

修改后的完整导出代码

import csv
from datetime import datetime

# 获取参与者对象的参考代码,替换你原有的all_participants获取逻辑即可
# all_participants = []
# for p in client.iter_participants(target_group):
#     all_participants.append(p)

with open("members.csv","w",encoding='UTF-8') as f:
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    # 表头新增JoinDate列
    writer.writerow(['username','user id', 'access hash','name','group', 'group id', 'JoinDate'])
    for participant in all_participants:
        user = participant.user
        # 处理入群日期,转为标准可读字符串格式
        join_date = participant.date.strftime("%Y-%m-%d %H:%M:%S") if participant.date else ""
        
        username = user.username if user.username else ""
        first_name = user.first_name if user.first_name else ""
        last_name = user.last_name if user.last_name else ""
        name = (first_name + ' ' + last_name).strip()
        
        # 写入行新增入群日期字段
        writer.writerow([username, user.id, user.access_hash, name, target_group.title, target_group.id, join_date])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 20:06:04