{"id":50368420,"url":"https://github.com/weaming/tg-reader","last_synced_at":"2026-05-30T05:30:32.338Z","repository":{"id":347994666,"uuid":"1196008585","full_name":"weaming/tg-reader","owner":"weaming","description":"Read messages from telegram public channel","archived":false,"fork":false,"pushed_at":"2026-03-30T10:40:17.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-30T12:10:17.273Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/weaming.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-30T09:27:01.000Z","updated_at":"2026-03-30T10:40:22.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/weaming/tg-reader","commit_stats":null,"previous_names":["weaming/tg-reader"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/weaming/tg-reader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Ftg-reader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Ftg-reader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Ftg-reader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Ftg-reader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weaming","download_url":"https://codeload.github.com/weaming/tg-reader/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Ftg-reader/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33681809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-05-30T05:30:30.815Z","updated_at":"2026-05-30T05:30:32.333Z","avatar_url":"https://github.com/weaming.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tg-reader\n\n以真实用户客户端身份连接 Telegram，加入目标频道，支持翻页拉取历史消息并实时监听新消息，将消息结构化写入 JSONL 文件。\n\n## 安装\n\n需要 Go 1.21+。\n\n```bash\ngo build -o tg-reader .\n```\n\n## 准备工作\n\n前往 [my.telegram.org](https://my.telegram.org) 创建应用，获取 `API ID` 和 `API Hash`。\n\n在项目目录创建 `.env`：\n\n```env\nTG_API_ID=12345678\nTG_API_HASH=abcdef1234567890abcdef1234567890\nTG_PHONE=+8613800138000\nTG_CHANNEL_USERNAME=channel_a,channel_b\n```\n\n`TG_CHANNEL_USERNAME` 支持英文逗号分隔多个频道。也可通过 `-channels` flag 在运行时覆盖。\n\n`ENV_FILE` 可指定环境变量配置文件路径。\n\n## 运行\n\n```bash\n./tg-reader [flags]\n```\n\n首次运行会要求输入手机验证码（及两步验证密码），认证成功后 session 保存至 `td.session`，后续无需重新登录。\n\n## 参数\n\n| 参数          | 默认值             | 说明                                                                                                                      |\n| ------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------- |\n| `-channels`   | —                  | 频道用户名，逗号分隔，覆盖 `TG_CHANNEL_USERNAME`                                                                          |\n| `-output`     | `./messages.jsonl` | 输出文件路径，多频道共用；被 `-output-dir` 覆盖时忽略                                                                     |\n| `-output-dir` | —                  | 输出目录，每个频道写入 `{username}.jsonl`（无 username 的频道用数字 ID）                                                  |\n| `-page-size`  | `100`              | 每次拉取历史消息条数，上限 100                                                                                            |\n| `-history`    | `false`            | 跳过「是否拉取历史消息」的交互确认，直接开始拉取                                                                          |\n| `-auto-stop`  | `false`            | 翻页遇到已存在的消息时自动停止，不弹确认；不加则用于手动回填历史空缺                                                      |\n| `-proxy`      | —                  | SOCKS5 代理，格式 `socks5://[user:pass@]host:port`；留空依次读取 `ALL_PROXY` 环境变量，最终兜底 `socks5://localhost:7890` |\n\n## 历史消息交互\n\n拉取历史消息时按频道串行进行，每页完成后提示：\n\n```\n[Enter]  继续下一页\na        自动拉取全部剩余历史（不再询问）\nq        停止翻页，写入已收集部分\n```\n\n首次运行（无已有记录）直接进入翻页，不询问是否拉取。有已有记录时默认询问（`[Y/n]`），或用 `-history` 跳过。\n\n翻页遇到已存在的消息时默认询问是否停止（`[Y/n]`）；回答 `n` 则跳过该条继续，用于回填中间空缺的消息；`-auto-stop` 则直接停止。\n\n## 输出格式\n\n每行一条 JSON，字段说明：\n\n```jsonc\n{\n  \"id\": 1234,\n  \"timestamp\": \"2026-03-30T10:00:00+08:00\",\n  \"edited_at\": \"2026-03-30T10:05:00+08:00\", // 有编辑时出现\n  \"text\": \"消息正文\",\n  \"channel_id\": 1001234567,\n  \"channel\": \"频道名称\",\n  \"views\": 8420,\n  \"forwards\": 312,\n  \"replies\": 47,\n  \"entities\": [\n    // 文本实体\n    {\"type\": \"url\", \"text\": \"https://...\"},\n    {\"type\": \"mention\", \"text\": \"@user\"},\n    {\"type\": \"hashtag\", \"text\": \"#tag\"},\n    {\"type\": \"text_url\", \"text\": \"点击这里\", \"url\": \"https://...\"},\n  ],\n  \"media\": {\n    // 附件（如有）\n    \"type\": \"webpage\", // photo/video/audio/voice/document/gif/sticker/webpage/poll/geo/contact/dice\n    \"url\": \"https://...\",\n    \"title\": \"文章标题\",\n    \"description\": \"摘要\",\n  },\n  \"forwarded_from\": \"原始频道名\", // 转发消息时出现\n  \"post_author\": \"编辑签名\", // 频道署名时出现\n  \"reply_to_id\": 1200, // 回复消息时出现\n}\n```\n\n所有时间字段均为 ISO 8601 格式，时区由 `TZ` 环境变量决定（如 `Asia/Hong_Kong`），未设置则默认香港时区（UTC+8）。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweaming%2Ftg-reader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweaming%2Ftg-reader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweaming%2Ftg-reader/lists"}