from __future__ import annotations def format_reply_context( reply_to_message_id: str | None = None, reply_to_text: str | None = None, reply_to_sender: str | None = None, ) -> str: """格式化引用回复上下文。 生成如 "[Replying to +8613800138000 id:msg_001]" 格式的上下文行, 附加在出站消息文本前。 """ if not reply_to_message_id and not reply_to_text: return "" parts: list[str] = ["[Replying to"] if reply_to_sender: parts.append(f" {reply_to_sender}") if reply_to_message_id: parts.append(f" id:{reply_to_message_id}") if reply_to_text: preview = reply_to_text[:100].replace("\n", " ") parts.append(f' "{preview}"') parts.append("]\n") return " ".join(parts) if len(parts) > 2 else "" def describe_reply_context(data: dict) -> str: """从消息数据中提取并格式化回复上下文。""" reply_to_guid = data.get("replyToGuid") reply_to_text = data.get("threadOriginatorText") reply_to_sender = data.get("threadOriginator") return format_reply_context( reply_to_message_id=reply_to_guid, reply_to_text=reply_to_text, reply_to_sender=reply_to_sender, )