Telegram MTProto Reference
面向 Telegram MTProto API(TL 二进制协议,如 Telethon、gotd、GramJS、Pyrogram 等 SDK 底层模型)的核心对象、实体与媒体速查。TL 语法与 Protobuf 对比见 TL Schema笔记TL Schema| 维度 | Telegram TL | Protobuf | · | 定义文件 | | | · | 类型系统 | Constructor / Type | Message / Enum | · | RPC 定义 | 内置 Functions | Service / RPC |打开:/notes/platform/telegram/mtproto/tl-schema,与 Bot API 对比见 Telegram FAQ笔记Telegram FAQ| 维度 | Bot API | MTProto(Telegram API) | · | 协议与传输 | HTTP JSON(Webhook / ) | TL 二进制 RPC + 持久连接 Updates | · | 支持账号 | 仅 Bot 账号 | User 账号(Userbot / Client)与 Bot 账号 |打开:/notes/platform/telegram/faq。
Peer 与 InputPeer
MTProto 中不同对象拥有各自独立的 ID 序列,数值可能重叠,因此收发对象用多态类型 Peer / InputPeer 区分:
接收类型 Peer | 发送参数 InputPeer | 对应对象 | 说明 |
|---|---|---|---|
peerUser (user_id) | inputPeerUser (user_id, access_hash) | 用户 / Bot | 需要对应实体的 access_hash |
peerChat (chat_id) | inputPeerChat (chat_id) | 普通群组 | 基础群组,无需 access_hash |
peerChannel (channel_id) | inputPeerChannel (channel_id, access_hash) | 频道、超级群、Gigagroup、Monoforum | 通过 Channel flags 区分具体形态 |
Channel构造器关键 flags:broadcast:频道megagroup:超级群组gigagroup:广播式超级群forum:开启 Forum Topics 的超级群monoforum:频道私信(Channel Direct Messages)
access_hash按账号会话隔离,必须通过对话列表、更新事件或用户名解析获取并本地缓存,不能凭空构造。- Peer bare ID 与 Bot API dialog ID 的精确范围及换算见 Telegram Reference笔记Telegram ReferenceBot API Reference - HTTP JSON、Entity、Media、、Bot 限制 · MTProto Reference - /、、/、 · Telegram FAQ - Bot API vs MTProto 对比、相册多事件聚合、UTF-16 偏移、Privacy Mode章节:Chat ID打开:/notes/platform/telegram/reference。
message 关键字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | int | 消息 ID(私聊/普通群账号内递增;频道/超级群在所属 channel 内独立递增) |
peer_id | Peer | 消息所属会话 |
from_id | Peer? | 发送者(频道帖子或匿名管理员可能缺省) |
date / edit_date | int | Unix 时间戳(秒) |
message | string | 正文或媒体说明(MTProto 不拆分 text 与 caption) |
entities | Vector<MessageEntity>? | 富文本实体(偏移与长度按 UTF-16 code units 计算) |
media | MessageMedia? | 附件与富媒体对象 |
reply_to | MessageReplyHeader? | 回复目标与话题信息(reply_to_msg_id、reply_to_top_id、forum_topic) |
saved_peer_id | Peer? | Saved Messages 子对话或 Monoforum 话题标识 |
grouped_id | long? | 相册 / Media Group 标识(与 messages.sendMultiMedia 对应) |
fwd_from | MessageFwdHeader? | 转发来源信息 |
replies | MessageReplies? | 频道帖子评论区或群组回复计数 |
reactions | MessageReactions? | 消息回应 |
out / post / pinned / noforwards | flags | 常见布尔状态位(发出、频道帖、置顶、禁止转发) |
MessageEntity
| Constructor | 附加字段 / Flags | 说明 |
|---|---|---|
messageEntityMention | - | @username 提及 |
messageEntityHashtag | - | #hashtag |
messageEntityCashtag | - | $cashtag |
messageEntityBotCommand | - | /command |
messageEntityUrl | - | 明文 URL |
messageEntityEmail | - | 邮箱地址 |
messageEntityPhone | - | 电话号码 |
messageEntityBankCard | - | 银行卡号 |
messageEntityBold | - | 粗体 |
messageEntityItalic | - | 斜体 |
messageEntityUnderline | - | 下划线 |
messageEntityStrike | - | 删除线 |
messageEntitySpoiler | - | 防剧透遮罩 |
messageEntityBlockquote | collapsed flag | 引用块 / 可折叠引用 |
messageEntityCode | - | 行内代码 |
messageEntityPre | language | 代码块 |
messageEntityTextUrl | url | 文本超链接 |
messageEntityMentionName | user_id: long | 接收到的按用户 ID 提及 |
inputMessageEntityMentionName | user_id: InputUser | 发送时构造按用户 ID 提及 |
messageEntityCustomEmoji | document_id: long | 自定义表情 |
messageEntityFormattedDate | date: int + 格式 flags | 格式化时间(relative、short_time/long_time、short_date/long_date、day_of_week) |
messageEntityDiffInsert / Replace / Delete | old_text(Replace) | 文本变更 diff 展示 |
messageEntityUnknown | - | 未知实体兜底 |
MessageMedia 与 DocumentAttribute
除照片外,绝大多数二进制媒体(视频、圆形视频、GIF、音频、语音、贴纸、普通文件)均以 messageMediaDocument 表示,需要结合 messageMediaDocument 的 flags(video、round、voice、spoiler)和 Document.attributes 进一步判定。
MessageMedia
| Constructor | 说明 |
|---|---|
messageMediaPhoto | 图片(live_photo flag + video: Document 表示实况图;spoiler 表示遮罩) |
messageMediaDocument | 通用文件及视频/音频/语音/动图/贴纸等容器 |
messageMediaGeo / messageMediaGeoLive | 静态地理位置 / 实时共享位置 |
messageMediaVenue | 地点卡片 |
messageMediaContact | 联系人名片 |
messageMediaWebPage | 网页预览(含完整 WebPage 对象) |
messageMediaPoll | 原生投票及结果 |
messageMediaDice | 随机表情 |
messageMediaGame | Telegram 游戏 |
messageMediaInvoice | 支付账单 |
messageMediaStory | 转发或提及的 Story |
messageMediaPaidMedia | Telegram Stars 付费媒体 |
messageMediaToDo | 待办清单 |
messageMediaGiveaway / messageMediaGiveawayResults | 抽奖与中奖公示 |
messageMediaVideoStream | 直播 Story 关联群组通话 |
messageMediaEmpty / messageMediaUnsupported | 空媒体 / 客户端不支持的媒体 |
DocumentAttribute
| Constructor | 关键字段 / Flags | 对应媒体类型 |
|---|---|---|
documentAttributeVideo | round_message、nosound、supports_streaming、duration、w、h | 普通视频或圆形视频(round_message=true) |
documentAttributeAnimated | - | GIF / 无声动图 |
documentAttributeAudio | voice、duration、title、performer、waveform | 语音消息(voice=true)或音乐/音频 |
documentAttributeSticker | mask、alt、stickerset | 贴纸或面具贴纸 |
documentAttributeCustomEmoji | free、text_color、alt、stickerset | 自定义表情文档 |
documentAttributeImageSize | w、h | 以文档方式发送的图片尺寸 |
documentAttributeFilename | file_name | 原始文件名 |
documentAttributeHasStickers | - | 标记媒体上贴有贴纸 |
Media Group(grouped_id)
- 发送:调用
messages.sendMultiMedia,传入multi_media: Vector<InputSingleMedia>。所有媒体必须先通过messages.uploadMedia分别上传,不能直接传inputMediaUploaded*构造器。 - 接收:同一相册的 2~10 条消息作为独立
message下发,携带相同的grouped_id: long;需要客户端/采集器按(peer_id, grouped_id)自行聚合。 - 文件引用刷新:访问或重发已有媒体时若遇到
FILE_REFERENCE_*_EXPIRED,需重新拉取原消息刷新file_reference。
参考
- core.telegram.org/mtproto链接core.telegram.org/mtproto面向 Telegram MTProto API(TL 二进制协议,如 Telethon、gotd、GramJS、Pyrogram 等 SDK 底层模型)的核心对象、实体与媒体速查。TL 语法与 Protobuf 对比见 TL Schema,与 Bot API 对比见 Telegram FAQ。笔记:Telegram MTProto Reference
- core.telegram.org/schema
- core.telegram.org/constructor/message
- core.telegram.org/type/MessageEntity
- core.telegram.org/type/MessageMedia
- core.telegram.org/type/DocumentAttribute
- core.telegram.org/method/messages.sendMultiMedia
- core.telegram.org/api/bots/ids
关联信息
反向链接、本文链接的其他页面和外部资料。
反向链接
- Telegram笔记 · MTProto Reference
Telegram Reference - 接入方式、Chat 类型、Chat ID 换算 · Telegram FAQ - Bot API vs MTProto、相册多事件、UTF-16 实体偏移 · Bot API Reference / Bot FAQ / Bot Awesome
- Telegram Bot API Reference笔记 · MTProto Reference
面向 Telegram Bot API(HTTP JSON)的字段、实体、媒体与收发约束速查。Bot 常见解析坑与相册聚合见 Bot FAQ,底层协议见 MTProto Reference,跨协议对比见 Telegram FAQ。 · 接收方式(二选一互斥):
- Telegram FAQ笔记 · Telegram MTProto Reference
| 维度 | Bot API | MTProto(Telegram API) | · | 协议与传输 | HTTP JSON(Webhook / ) | TL 二进制 RPC + 持久连接 Updates | · | 支持账号 | 仅 Bot 账号 | User 账号(Userbot / Client)与 Bot 账号 |
- Telegram Reference笔记 · MTProto Reference
Bot API Reference - HTTP JSON、Entity、Media、、Bot 限制 · MTProto Reference - /、、/、 · Telegram FAQ - Bot API vs MTProto 对比、相册多事件聚合、UTF-16 偏移、Privacy Mode
站内链接
- Telegram FAQ笔记 · Telegram FAQ
| 维度 | Bot API | MTProto(Telegram API) | · | 协议与传输 | HTTP JSON(Webhook / ) | TL 二进制 RPC + 持久连接 Updates | · | 支持账号 | 仅 Bot 账号 | User 账号(Userbot / Client)与 Bot 账号 |
- Telegram Reference笔记 · Telegram Reference · chat id
Bot API Reference - HTTP JSON、Entity、Media、、Bot 限制 · MTProto Reference - /、、/、 · Telegram FAQ - Bot API vs MTProto 对比、相册多事件聚合、UTF-16 偏移、Privacy Mode
- TL Schema笔记 · TL Schema
| 维度 | Telegram TL | Protobuf | · | 定义文件 | | | · | 类型系统 | Constructor / Type | Message / Enum | · | RPC 定义 | 内置 Functions | Service / RPC |
参考资料
其他外链8 条
- core.telegram.org/api/bots/idscore.telegram.org/api/bots/ids
- core.telegram.org/constructor/messagecore.telegram.org/constructor/message
- core.telegram.org/method/messages.sendMultiMediacore.telegram.org/method/messages.sendMultiMedia
- core.telegram.org/mtprotocore.telegram.org/mtproto
- core.telegram.org/schemacore.telegram.org/schema
- core.telegram.org/type/DocumentAttributecore.telegram.org/type/DocumentAttribute
- core.telegram.org/type/MessageEntitycore.telegram.org/type/MessageEntity
- core.telegram.org/type/MessageMediacore.telegram.org/type/MessageMedia