调整消息内容
This commit is contained in:
@@ -163,24 +163,41 @@ class WxMessage:
|
||||
"""从JSON数据创建消息对象"""
|
||||
data = json_data.get("Data", {})
|
||||
to_user = data.get("ToUserName", {}).get("string", "")
|
||||
from_user = data.get("FromUserName", {}).get("string", "")
|
||||
|
||||
# 获取原始内容和发信人
|
||||
# 获取原始内容
|
||||
content_str = data.get("Content", {}).get("string", "")
|
||||
sender = data.get("FromUserName", {}).get("string", "")
|
||||
|
||||
|
||||
# 判断是否是群聊消息
|
||||
is_group_chat = from_user.endswith("@chatroom")
|
||||
|
||||
# 如果是群聊消息,需要调整发送者和接收者
|
||||
actual_sender = from_user
|
||||
if is_group_chat and content_str:
|
||||
# 从消息内容中提取真正的发送人
|
||||
# 简化逻辑:直接通过冒号分割获取发送人
|
||||
parts = content_str.split(':', 1) # 只分割第一个冒号
|
||||
if len(parts) > 1:
|
||||
# 提取发送人ID(冒号前的部分)
|
||||
potential_sender = parts[0].strip()
|
||||
if potential_sender: # 确保发送人ID不为空
|
||||
actual_sender = potential_sender
|
||||
# 群聊消息中,接收者是群ID
|
||||
to_user = from_user
|
||||
|
||||
# 创建MessageContent对象时传入发信人信息
|
||||
message_content = MessageContent(content_str, sender=sender)
|
||||
message_content = MessageContent(content_str, sender=actual_sender)
|
||||
|
||||
return cls(
|
||||
type_name=json_data.get("TypeName", ""),
|
||||
appid=json_data.get("Appid", ""),
|
||||
wxid=json_data.get("Wxid", ""),
|
||||
msg_id=data.get("MsgId", 0),
|
||||
sender=sender,
|
||||
to_user=to_user,
|
||||
roomid=to_user if to_user.endswith("@chatroom") else "",
|
||||
sender=actual_sender, # 使用提取出的实际发送人
|
||||
to_user=to_user, # 群聊时,接收者为群ID
|
||||
roomid=from_user if is_group_chat else "", # 如果是群聊,roomid就是from_user
|
||||
msg_type=MessageType(data.get("MsgType", 0)),
|
||||
content=message_content, # 使用包含发信人信息的MessageContent
|
||||
content=message_content,
|
||||
create_time=data.get("CreateTime", 0),
|
||||
push_content=data.get("PushContent"),
|
||||
new_msg_id=data.get("NewMsgId", 0),
|
||||
|
||||
Reference in New Issue
Block a user