11
This commit is contained in:
87
api/middleware/schedule/handlers/chatHandler.js
Normal file
87
api/middleware/schedule/handlers/chatHandler.js
Normal file
@@ -0,0 +1,87 @@
|
||||
const BaseHandler = require('./baseHandler');
|
||||
const ConfigManager = require('../services/configManager');
|
||||
const command = require('../command');
|
||||
const config = require('../config');
|
||||
|
||||
/**
|
||||
* 自动沟通处理器
|
||||
* 负责自动回复HR消息
|
||||
*/
|
||||
class ChatHandler extends BaseHandler {
|
||||
/**
|
||||
* 处理自动沟通任务
|
||||
* @param {object} task - 任务对象
|
||||
* @returns {Promise<object>} 执行结果
|
||||
*/
|
||||
async handle(task) {
|
||||
return await this.execute(task, async () => {
|
||||
return await this.doChat(task);
|
||||
}, {
|
||||
checkAuth: true,
|
||||
checkOnline: true,
|
||||
recordDeviceMetrics: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行沟通逻辑
|
||||
*/
|
||||
async doChat(task) {
|
||||
const { sn_code, taskParams } = task;
|
||||
const { platform = 'boss' } = taskParams;
|
||||
|
||||
console.log(`[自动沟通] 开始 - 设备: ${sn_code}`);
|
||||
|
||||
// 1. 获取账户配置
|
||||
const accountConfig = await this.getAccountConfig(sn_code, ['platform_type', 'chat_strategy']);
|
||||
|
||||
if (!accountConfig) {
|
||||
return {
|
||||
chatCount: 0,
|
||||
message: '未找到账户配置'
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 解析沟通策略配置
|
||||
const chatStrategy = ConfigManager.parseChatStrategy(accountConfig.chat_strategy);
|
||||
|
||||
// 3. 检查沟通时间范围
|
||||
const timeRange = ConfigManager.getTimeRange(chatStrategy);
|
||||
if (timeRange) {
|
||||
const timeRangeValidator = require('../services/timeRangeValidator');
|
||||
const timeCheck = timeRangeValidator.checkTimeRange(timeRange);
|
||||
|
||||
if (!timeCheck.allowed) {
|
||||
return {
|
||||
chatCount: 0,
|
||||
message: timeCheck.reason
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 创建沟通指令
|
||||
const chatCommand = {
|
||||
command_type: 'autoChat',
|
||||
command_name: '自动沟通',
|
||||
command_params: JSON.stringify({
|
||||
sn_code,
|
||||
platform: platform || accountConfig.platform_type || 'boss',
|
||||
autoReply: chatStrategy.auto_reply || false,
|
||||
replyTemplate: chatStrategy.reply_template || ''
|
||||
}),
|
||||
priority: config.getTaskPriority('auto_chat') || 6
|
||||
};
|
||||
|
||||
// 5. 执行沟通指令
|
||||
const result = await command.executeCommands(task.id, [chatCommand], this.mqttClient);
|
||||
|
||||
console.log(`[自动沟通] 完成 - 设备: ${sn_code}`);
|
||||
|
||||
return {
|
||||
chatCount: result.chatCount || 0,
|
||||
message: '沟通完成'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ChatHandler;
|
||||
Reference in New Issue
Block a user