65 lines
2.1 KiB
Plaintext
65 lines
2.1 KiB
Plaintext
require('dotenv').config({ path: '/opt/guanghulab-dingtalk/dingtalk-bot/.env' });
|
||
var express = require('express');
|
||
var axios = require('axios');
|
||
var app = express();
|
||
var PORT = process.env.PORT || 3007;
|
||
|
||
// 引入路由器(不用 AI 引擎)
|
||
var router = require('./message-router');
|
||
|
||
app.use(require('cors')());
|
||
app.use(require('body-parser').json());
|
||
|
||
// GET验证
|
||
app.get('/dingtalk/callback', (req, res) => {
|
||
console.log('[DingTalk] GET验证请求');
|
||
res.status(200).json({ success: true });
|
||
});
|
||
|
||
// POST接收消息并按分类回复
|
||
app.post('/dingtalk/callback', async (req, res) => {
|
||
console.log('[DingTalk] POST收到消息');
|
||
|
||
res.status(200).json({ success: true });
|
||
|
||
const sessionWebhook = req.body.sessionWebhook;
|
||
const senderNick = req.body.senderNick || '未知用户';
|
||
const text = req.body.text || {};
|
||
const content = text.content || '';
|
||
|
||
if (!sessionWebhook) {
|
||
console.log('[DingTalk] 没有 sessionWebhook,无法回复');
|
||
return;
|
||
}
|
||
|
||
// 调用路由器分类
|
||
const classification = router.classifyMessage(content);
|
||
console.log('[DingTalk] 分类:', classification);
|
||
|
||
// 根据分类生成不同回复
|
||
let replyContent = '';
|
||
if (classification.type === 'syslog') {
|
||
replyContent = `📡 收到 SYSLOG:${classification.broadcastId || '未知编号'}`;
|
||
} else if (classification.type === 'question') {
|
||
replyContent = `❓ 收到问题,秋秋正在思考中...\n\n问题:${content}`;
|
||
} else {
|
||
replyContent = `@${senderNick} 收到你的消息:${content}`;
|
||
}
|
||
|
||
try {
|
||
await axios.post(sessionWebhook, {
|
||
msgtype: 'text',
|
||
text: { content: replyContent }
|
||
});
|
||
console.log('[DingTalk] 回复成功');
|
||
} catch (err) {
|
||
console.error('[DingTalk] 回复失败:', err.message);
|
||
}
|
||
});
|
||
|
||
app.listen(PORT, '0.0.0.0', () => {
|
||
console.log('🚀 服务启动 http://localhost:' + PORT);
|
||
});
|
||
process.stdin.resume();
|
||
process.on('uncaughtException', (err) => { console.error('❌', err.message); });
|