| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- const API = require("../../../../lib/API.js");
- const db = require("../../../../plugin/DataBase/db.js");
- const { BaseStdResponse } = require("../../../../BaseStdResponse.js");
- const OneBotV11 = require("../../../../plugin/OneBot/OneBotV11.js");
- class AIReply extends API {
- constructor() {
- super();
- this.setPath('/Kefu/Order/Bot/AIReply')
- this.setMethod('POST')
- this.noEncrypt()
- }
- async onRequest(req, res) {
- const onebot = OneBotV11.getOneBotConfig()
- if (!onebot.enabled) {
- return res.json({ ...BaseStdResponse.ERR, msg: 'OneBot v11 未启用' })
- }
- const token = req.headers['x-onebot-access-token'] || req.headers['authorization'] || ''
- if (onebot.callbackToken && !String(token).includes(onebot.callbackToken)) {
- return res.status(401).json({
- ...BaseStdResponse.ACCESS_DENIED,
- msg: 'AI 回调 token 校验失败'
- })
- }
- const { id, content } = req.body || {}
- if ([id, content].some(value => value === '' || value === null || value === undefined)) {
- return res.json({
- ...BaseStdResponse.MISSING_PARAMETER,
- msg: '缺少 id 或 content'
- })
- }
- const selectSql = 'SELECT msg, state FROM work_order WHERE id = ?'
- const rows = await db.query(selectSql, [id])
- if (!rows || rows.length !== 1 || rows[0].state === 2) {
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '工单不存在或已关闭'
- })
- }
- const now = Date.now()
- const msg = rows[0].msg || []
- msg.push({
- time: now,
- content: String(content).trim(),
- uuid: 'e4fe0277-0b1a-41a1-b25f-8b6e4cec3281',
- type: 'ai',
- files: []
- })
- const updateSql = 'UPDATE work_order SET msg = ?, update_time = ?, state = 3 WHERE id = ?'
- await db.query(updateSql, [msg, now, id])
- return res.json({
- ...BaseStdResponse.OK
- })
- }
- }
- module.exports.AIReply = AIReply
|