[patch] 修复抖音消息格式 过滤抖音系统消息 失效后发送结构体给后端
This commit is contained in:
@@ -813,6 +813,30 @@ class DouYinMessageHandler:
|
||||
self._log(f"❌ 发送客服列表到后端异常: {e}", "ERROR")
|
||||
return False
|
||||
|
||||
def _send_cookie_expired_message(self):
|
||||
"""向后端发送Cookie失效通知"""
|
||||
try:
|
||||
self._log("🔴 准备发送Cookie失效通知给后端", "INFO")
|
||||
from WebSocket.backend_singleton import get_backend_client
|
||||
backend = get_backend_client()
|
||||
|
||||
if backend:
|
||||
message = {
|
||||
"type": "connect_message",
|
||||
"store_id": self.store_id,
|
||||
"status": False,
|
||||
"content": "cookies失效,需要重新登录"
|
||||
}
|
||||
backend.send_message(message)
|
||||
self._log("✅ 已向后端发送Cookie失效通知", "SUCCESS")
|
||||
print(f"🔥 [DY] Cookie失效通知已发送: store_id={self.store_id}")
|
||||
else:
|
||||
self._log("❌ 后端客户端为空,无法发送Cookie失效通知", "ERROR")
|
||||
except Exception as e:
|
||||
self._log(f"❌ 发送Cookie失效通知失败: {e}", "ERROR")
|
||||
import traceback
|
||||
self._log(f"错误详情: {traceback.format_exc()}", "DEBUG")
|
||||
|
||||
def start_event_loop(self):
|
||||
"""启动事件循环线程"""
|
||||
if self._loop is not None and not self._loop.is_closed():
|
||||
@@ -885,7 +909,14 @@ class DouYinMessageHandler:
|
||||
self._log(f"解析后的JSON数据: {data}", "DEBUG")
|
||||
|
||||
if data.get('code') != 0:
|
||||
error_msg = data.get('message', '未知错误')
|
||||
error_code = data.get('code')
|
||||
error_msg = data.get('message', data.get('msg', data.get('extraMsg', '未知错误')))
|
||||
|
||||
# 🔥 检测到cookie失效(code=10005表示登录过期)
|
||||
if error_code == 10005 or '登录过期' in error_msg or '请重新登录' in error_msg:
|
||||
self._log(f"❌ 检测到Cookie失效: {error_msg}", "ERROR")
|
||||
self._send_cookie_expired_message()
|
||||
|
||||
raise Exception(f"获取配置失败: {error_msg}")
|
||||
|
||||
if 'data' not in data:
|
||||
@@ -1077,15 +1108,48 @@ class DouYinMessageHandler:
|
||||
self._log(f"Error details: {traceback.format_exc()}", "DEBUG")
|
||||
|
||||
async def _process_pending_message(self, sender_id, message_content):
|
||||
"""处理待发送的消息"""
|
||||
"""处理待发送的消息(修复:message_content是message_dict字典,需要提取text字段)"""
|
||||
try:
|
||||
# 获取用户信息
|
||||
user_info = self.user_tokens[sender_id]
|
||||
talk_id = user_info.get("talk_id", 0)
|
||||
p_id = user_info.get("p_id", 0)
|
||||
|
||||
# 🔥 统一消息类型检测逻辑(与拼多多保持一致)
|
||||
# 🔥 修复:从 message_dict 中提取文本内容和其他字段
|
||||
if isinstance(message_content, dict):
|
||||
# message_content 是字典(message_dict)
|
||||
message_text = message_content.get('text', '')
|
||||
avatar_url = message_content.get('avatar', '')
|
||||
|
||||
# 确定消息类型
|
||||
msg_type = self._determine_message_type(message_content)
|
||||
|
||||
# 根据消息类型提取对应的内容
|
||||
if msg_type == 'video':
|
||||
content = message_content.get('video_url', message_text)
|
||||
elif msg_type == 'image':
|
||||
content = message_content.get('image_url', message_text)
|
||||
elif msg_type == 'order':
|
||||
order_id = message_content.get('order_id', '')
|
||||
goods_id = self.get_goods_id(order_id) if order_id else ''
|
||||
if goods_id:
|
||||
content = f"商品id:{goods_id} 订单号:{order_id}"
|
||||
else:
|
||||
content = f"订单号:{order_id}"
|
||||
msg_type = 'order_card'
|
||||
elif msg_type == 'goods':
|
||||
goods_id = message_content.get('goods_id', '')
|
||||
content = f"商品卡片ID:{goods_id}"
|
||||
msg_type = 'product_card'
|
||||
else:
|
||||
content = message_text
|
||||
else:
|
||||
# 兼容字符串类型(旧代码可能传字符串)
|
||||
content = message_content
|
||||
avatar_url = ''
|
||||
msg_type = 'text'
|
||||
|
||||
# 🔥 统一消息类型检测逻辑(与拼多多保持一致)
|
||||
lc = str(content).lower()
|
||||
|
||||
# 检测消息类型,使用与拼多多相同的逻辑
|
||||
@@ -1095,14 +1159,13 @@ class DouYinMessageHandler:
|
||||
msg_type = "video"
|
||||
elif any(keyword in lc for keyword in ['goods.html', 'item.html', 'item.jd.com', '商品卡片id:']):
|
||||
msg_type = "product_card"
|
||||
else:
|
||||
msg_type = "text"
|
||||
|
||||
# 🔥 使用工厂方法创建消息模板(与拼多多保持一致)
|
||||
message_template = PlatformMessage.create_text_message(
|
||||
content=content,
|
||||
sender_id=str(sender_id),
|
||||
store_id=self.store_id
|
||||
store_id=self.store_id,
|
||||
pin_image=avatar_url if avatar_url else None
|
||||
)
|
||||
# 动态设置检测到的消息类型
|
||||
message_template.msg_type = msg_type
|
||||
@@ -1469,7 +1532,29 @@ class DouYinMessageHandler:
|
||||
content_data = msg_content["8"]
|
||||
self._log(f"📄 原始内容数据: {content_data}", "DEBUG")
|
||||
|
||||
if message_dict and message_dict.get('text') != "客服水滴智能优品接入":
|
||||
# 🔥 过滤系统消息:客服接入、客服离开等系统通知
|
||||
text_content = message_dict.get('text', '') if message_dict else ''
|
||||
|
||||
# 检查是否为系统消息
|
||||
is_system_message = False
|
||||
if text_content:
|
||||
# 使用正则表达式匹配系统消息模式
|
||||
system_patterns = [
|
||||
r'客服.*接入', # 客服XX接入
|
||||
r'客服.*离开', # 客服XX离开
|
||||
r'客服.*转接', # 客服XX转接
|
||||
r'会话.*结束', # 会话已结束
|
||||
r'.*已转接.*', # XX已转接给XX
|
||||
r'系统消息', # 系统消息
|
||||
]
|
||||
|
||||
for pattern in system_patterns:
|
||||
if re.search(pattern, text_content):
|
||||
is_system_message = True
|
||||
self._log(f"🔕 过滤系统消息: '{text_content}'", "DEBUG")
|
||||
break
|
||||
|
||||
if message_dict and text_content and not is_system_message:
|
||||
self._log(f"💬 成功解析用户消息: '{message_dict}'", "SUCCESS")
|
||||
|
||||
# 提取会话信息
|
||||
|
||||
Reference in New Issue
Block a user