
在 telegram bot api 中,`forward_from` 字段嵌套在 `message` 对象内,直接访问 `$update['forward_from']['id']` 会导致空值;必须通过 `$update['message']['forward_from']['id']` 才能安全读取转发来源用户的 id。
Telegram 的 Webhook 更新(Update)结构是分层嵌套的 JSON 对象,并非所有字段都位于根层级。当你收到一条包含转发消息的更新时,forward_from 并非 $update 的直接子键,而是位于 $update['message'](或 $update['channel_post']、$update['edited_message'] 等具体消息类型对象)之下。
✅ 正确写法(适用于普通私聊/群聊中的转发消息):
$update = json_decode(file_get_contents('php://input'), TRUE);
// 安全检查:确保是消息类型且存在 forward_from
if (isset($update['message']['forward_from']['id'])) {
$originalUserId = $update['message']['forward_from']['id'];
echo "原始发送者 ID: " . $originalUserId;
} else {
echo "该消息未被转发,或转发者已启用隐私设置(隐藏转发来源)";
}⚠️ 注意事项:
- 若用户在 Telegram 中启用了「隐藏我的转发消息来源」(即关闭“显示我是谁转发的”),则 forward_from 字段将完全不存在,即使消息被转发也不会出现 —— 这是 Telegram 的强制隐私策略,无法绕过。
- 某些消息类型(如频道帖子、编辑后的消息)需检查对应键名:
- 频道转发:$update['channel_post']['forward_from']['id']
- 编辑的消息:$update['edited_message']['forward_from']['id']
建议统一使用 get_message_object() 辅助函数提取实际消息体。
- 始终进行 isset() 或 array_key_exists() 判断,避免 PHP Notice 错误。
? 总结:Telegram Update 结构严谨,切勿假设字段扁平化。务必依据 官方 Update 对象文档 定位字段路径,并始终做空值防护。正确路径是 $update['message']['forward_from']['id'],而非 $update['forward_from']['id']。










