Telegram Schema
参考 aichat-console(tgi)的 Telegram 采集与归档实践并做结构化完善,同时兼顾 Bot API、MTProto / Userbot、TDLib 与 离线导出 四种来源的保真归档与增量同步。协议细节见 Telegram Reference笔记Telegram ReferenceBot API Reference - HTTP JSON、Entity、Media、、Bot 限制 · MTProto Reference - /、、/、 · Telegram FAQ - Bot API vs MTProto 对比、相册多事件聚合、UTF-16 偏移、Privacy Mode打开:/notes/platform/telegram/reference。
相比 aichat-console 原型的关键优化
- 同步位点类型化:新增
telegram_sync_state,将 Bot APIupdate_offset与 MTProto 全局pts/qts/seq/state_date明确拆列,同时在telegram_chat.pts保留频道/超级群独立pts游标(并保留cursor jsonb兜底扩展)。 - 补齐 Telegram 一等原语:
- 新增
telegram_topic显式建模 Forum 超级群话题(topic_id=message_thread_id/top_msg_id); telegram_message显式提列media_group_id(相册聚合)、entities(UTF-16 偏移富文本实体)、reaction、reply_markup、forwarded_from、post_author、views/forwards、edited_at/deleted_at。
- 新增
- 修订历史与附件强绑定:
telegram_media使用复合外键(tid, telegram_message_id, telegram_revision_id) references telegram_message_revision (tid, telegram_message_id, id)防止跨消息错绑修订版,同时并存跨 Bot 稳定的file_unique_id与账号作用域的file_id。 - 保真与派生解耦:业务侧 AI 分类、清洗、抽取结果留在分析域,不污染基础归档表;所有原始上游信封统一经 append-only
telegram_raw_update留痕并支持重放。
表清单
| 表 | 职责 |
|---|---|
telegram_account | 接入账号 / Bot / 导出源身份(BotApi / Mtproto / Tdlib / Export) |
telegram_contact | 账号视角观测到的用户与 Bot 档案(含 MTProto access_hash) |
telegram_chat | 私聊、群、超级群、频道主表(含 is_forum、access_state、folder_id、频道 pts) |
telegram_topic | Forum 超级群内的话题索引(topic_id、is_general、is_closed) |
telegram_sync_state | 账号级同步游标(update_offset + pts/qts/seq/state_date) |
telegram_raw_update | append-only 原始更新收件箱(幂等 dedupe_key + 完整 raw) |
telegram_message | 核心保真大宽表:双定位(chat_peer_id/source_message_id/thread_id/media_group_id)、发送人快照、回复/转发、text/entities/reaction/raw |
telegram_message_revision | append-only 编辑历史快照(保留编辑前原文、entities 与 raw) |
telegram_media | 媒体元数据与归档引用(file_unique_id + file_id + sha256 + storage_ref) |
关联信息
反向链接、本文链接的其他页面和外部资料。
反向链接
- Schema Design笔记 · Telegram
Rule - 命名、标识边界、枚举大小写、事实数据、PostgreSQL 约束与 i18n 规则。 · Pattern - CQRS(Command+Event+Message+Snapshot)、Task Lease、不可变事实数据、双轨模型与专项模式:
- Schema Pattern笔记 · Telegram Schema
Resource - 资源表、基础字段、来源字段与扩展字段。 · State - / / / reason / 软删除。 · Ownership - 、、、visibility、共享授权。 · Auth - AuthN、AuthZ、access token、permission grant、RBAC 边界。
站内链接
- Telegram Reference笔记 · Telegram Reference
Bot API Reference - HTTP JSON、Entity、Media、、Bot 限制 · MTProto Reference - /、、/、 · Telegram FAQ - Bot API vs MTProto 对比、相册多事件聚合、UTF-16 偏移、Privacy Mode