1
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
const db = require('../dbProxy.js');
|
const db = require('../dbProxy.js');
|
||||||
const logProxy = require('../logProxy.js');
|
const logProxy = require('../logProxy.js');
|
||||||
const deviceManager = require('../schedule/core/deviceManager.js');
|
const deviceManager = require('../schedule/core/deviceManager.js');
|
||||||
|
const chatManager = require('../job/managers/chatManager');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MQTT 消息分发器
|
* MQTT 消息分发器
|
||||||
@@ -323,12 +324,79 @@ class MqttDispatcher {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`[MQTT Boss 消息] 收到设备 ${sn_code} 的新消息,payload 长度:`, typeof payload === 'string' ? payload.length : 'object');
|
// 按你给的结构解析:取第一条消息的文本等关键信息
|
||||||
|
const firstMsg = Array.isArray(payload.messages) && payload.messages.length > 0
|
||||||
|
? payload.messages[0]
|
||||||
|
: null;
|
||||||
|
|
||||||
// TODO: 这里可以根据业务需要:
|
const fromUidObj = firstMsg && firstMsg.from && firstMsg.from.uid;
|
||||||
// - 写入 chat_message / chat_reply_intent_log 等表
|
const toUidObj = firstMsg && firstMsg.to && firstMsg.to.uid;
|
||||||
// - 触发 AI 自动回复流程
|
const text = firstMsg && firstMsg.body && typeof firstMsg.body.text === 'string'
|
||||||
// 当前先只是打印日志,预留扩展点。
|
? firstMsg.body.text
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const normalized = {
|
||||||
|
sn_code,
|
||||||
|
type: payload.type || null,
|
||||||
|
version: payload.version || null,
|
||||||
|
from_uid: fromUidObj && typeof fromUidObj.low === 'number' ? String(fromUidObj.low) : null,
|
||||||
|
to_uid: toUidObj && typeof toUidObj.low === 'number' ? String(toUidObj.low) : null,
|
||||||
|
text,
|
||||||
|
raw: payload
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('[MQTT Boss 消息] 解析结果:', {
|
||||||
|
sn_code: normalized.sn_code,
|
||||||
|
type: normalized.type,
|
||||||
|
version: normalized.version,
|
||||||
|
from_uid: normalized.from_uid,
|
||||||
|
to_uid: normalized.to_uid,
|
||||||
|
text: normalized.text
|
||||||
|
});
|
||||||
|
|
||||||
|
// 落库到 chat_message 表(与自动沟通的落库格式保持一致)
|
||||||
|
try {
|
||||||
|
const chatMessageModel = db.getModel('chat_message');
|
||||||
|
const platform = 'boss';
|
||||||
|
const friendId = normalized.from_uid ? Number(normalized.from_uid) : 0;
|
||||||
|
const mid =
|
||||||
|
firstMsg && firstMsg.mid && typeof firstMsg.mid.low === 'number'
|
||||||
|
? firstMsg.mid.low
|
||||||
|
: 0;
|
||||||
|
const base = {
|
||||||
|
sn_code,
|
||||||
|
platform,
|
||||||
|
friendId,
|
||||||
|
encryptFriendId: '',
|
||||||
|
fetch_time: new Date()
|
||||||
|
};
|
||||||
|
if (friendId && chatMessageModel) {
|
||||||
|
const existing = await chatMessageModel.findOne({
|
||||||
|
where: { sn_code, platform, friendId, mid }
|
||||||
|
});
|
||||||
|
const row = { ...base, mid, message_data: firstMsg };
|
||||||
|
if (existing) {
|
||||||
|
await existing.update(row);
|
||||||
|
} else {
|
||||||
|
await chatMessageModel.create(row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[MQTT Boss 消息] 写入 chat_message 失败:', e.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用现有 AI 自动回复流程(基于 get_chat_detail + getReplyContentFromDetail)
|
||||||
|
try {
|
||||||
|
if (normalized.from_uid && this.mqttClient) {
|
||||||
|
const result = await chatManager.auto_reply_with_ai(sn_code, this.mqttClient, {
|
||||||
|
friendId: Number(normalized.from_uid),
|
||||||
|
platform: 'boss'
|
||||||
|
});
|
||||||
|
console.log('[MQTT Boss 消息] AI 自动回复结果:', result);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[MQTT Boss 消息] AI 自动回复失败:', e.message);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[MQTT Boss 消息] 处理 Boss 消息失败:', error);
|
console.error('[MQTT Boss 消息] 处理 Boss 消息失败:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user