Browse Source

second commit

Pchen. 1 year ago
parent
commit
c0e558d588
5 changed files with 27 additions and 387 deletions
  1. 1 1
      app.js
  2. 12 22
      router.js
  3. 0 0
      wechat/ChatGPT.js
  4. 0 297
      wechat/main - 副本.js
  5. 14 67
      wechat/main.js

+ 1 - 1
app.js

@@ -9,7 +9,7 @@ const app = express()
 const cors = require('cors')
 app.use(cors())
 
-router.use(express.static('./public'));
+router.use(express.static('./public'))
 
 app.use(express.json())
 

+ 12 - 22
router.js

@@ -1,5 +1,5 @@
 const express = require('express')
-const { sendMessageToAPI, setApiKey, setApiUrl, setapp_code ,setmodel} = require('./wechat/getmessage')
+const { sendMessageToAPI, setApiKey, setApiUrl, setapp_code ,setmodel} = require('./wechat/ChatGPT')
 const sqlite3 = require('sqlite3')
 const jsonwebtoken = require('jsonwebtoken')
 const path = require('path')
@@ -8,13 +8,7 @@ const {
     wxlogin,
     Status,
     User,
-    setAutoReplySingle,
-    setwhiteRoom,
-    setatReply,
-    setkeyWords,
-    setblackName,
-    setSuffix,
-    setPrefix,
+    setWx,
     stopWx,
     loadConfigValues
 } = require('./wechat/main')
@@ -26,14 +20,10 @@ var db = new sqlite3.Database(sqliteDbPath)
 
 const router = express.Router()
 
-//托管静态资源文件目录
-router.use(express.static('./public'))
-
-
 // 定义中间件.unless指定哪些接口不需要进行token身份认证
 const { expressjwt: jwt } = require("express-jwt")
 const checkTokenMiddleware = jwt({ secret: secretKey, algorithms: ["HS256"] }).unless({
-    path: [/^\/userlogin/, /^\/register/,/^\/getavatar/],
+    path: [/^\/userlogin/, /^\/register/],
 })
 
 // 验证token
@@ -210,16 +200,16 @@ router.post('/getwxconfig', async (req, res) => {
 })
 
 //设置微信机器人
-router.post('/wxconfig',async(req,res) => {
-    const { autoReplySingle, autoReplyRoom, suffix, prefix, otherTypeReply ,atReply,keyWords,blackName,whiteRoom} = req.body
+router.post('/wxconfig', async (req, res) => {
+    const { autoReplySingle, suffix, prefix, atReply, keyWords, blackName, whiteRoom } = req.body
     try {
-        setAutoReplySingle(autoReplySingle)
-        setSuffix(suffix)
-        setPrefix(prefix)
-        setwhiteRoom(whiteRoom)
-        setatReply(atReply)
-        setkeyWords(keyWords)
-        setblackName(blackName)
+        setWx('autoReplySingle', autoReplySingle)
+        setWx('suffix', suffix)
+        setWx('prefix', prefix)
+        setWx('whiteRoom', whiteRoom)
+        setWx('atReply', atReply)
+        setWx('keyWords', keyWords)
+        setWx('blackName', blackName)
         loadConfigValues()
         res.send({ status: 200, msg: '设置成功!' })
     } catch (error) {

+ 0 - 0
wechat/getmessage.js → wechat/ChatGPT.js


+ 0 - 297
wechat/main - 副本.js

@@ -1,297 +0,0 @@
-const { WechatyBuilder } = require("wechaty")
-const { sendMessageToAPI } = require('./getmessage')
-const sqlite3 = require('sqlite3')
-
-//sqlite数据库路径
-let sqliteDbPath = "./db/data.db"
-//打开数据库
-let db = new sqlite3.Database(sqliteDbPath)
-
-const wechaty = WechatyBuilder.build()
-
-function getConfigValue(configName) {
-    return new Promise((resolve, reject) => {
-        const query = 'SELECT value FROM wxconfig WHERE config = ?'
-        db.get(query, [configName], (err, row) => {
-            if (err) {
-                reject(err)
-            } else {
-                const configValue = row ? row.value : null
-                // 处理字符串 'null',如果是 'null' 则返回 null
-                resolve(configValue === 'null' ? null : configValue)
-            }
-        })
-    })
-}
-
-// 读取配置信息并设置相应的变量
-async function loadConfigValues() {
-    try {
-        autoReplySingle = await getConfigValue('autoReplySingle') === 'true'
-        prefix = await getConfigValue('prefix')
-        suffix = await getConfigValue('suffix')
-        whiteRoomString = await getConfigValue('whiteRoom')
-        keyWordsString = await getConfigValue('keyWords')
-        blackNameString = await getConfigValue('blackName')
-        atReply = await getConfigValue('atReply') === 'true'
-
-        // 处理转义符
-        suffix = suffix !== null ? suffix.replace(/\\n/g, '\n') : ''
-        otherTypeReply = otherTypeReply !== null ? otherTypeReply.replace(/\\n/g, '\n') : ''
-        prefix = prefix !== null ? prefix.replace(/\\n/g, '\n') : ''
-
-        // 处理用逗号分隔的字符串形式的数组
-        whiteRoom = whiteRoomString !== null ? whiteRoomString.split(",").map(item => item.trim()) : []
-        keyWords = keyWordsString !== null ? keyWordsString.split(",").map(item => item.trim()) : []
-        blackName = blackNameString !== null ? blackNameString.split(",").map(item => item.trim()) : []
-
-        console.log('Config values loaded successfully.')
-    } catch (error) {
-        console.error('Error loading config values:', error)
-    }
-}
-
-
-
-// 调用函数加载配置信息
-loadConfigValues()
-
-//获取时间
-function getCurrentTime() {
-    const options = {
-        year: 'numeric',
-        month: '2-digit',
-        day: '2-digit',
-        hour: '2-digit',
-        minute: '2-digit',
-        second: '2-digit',
-    }
-
-    const currentTime = new Date().toLocaleString('zh-CN', options)
-    return currentTime
-}
-
-//停止函数运行
-let isRunning = false
-
-async function stopWx() {
-    if (isRunning) {
-        isRunning = false
-        await wechaty.stop();
-        Status.status = 0;
-        console.log('Wechaty stopped.');
-    } else {
-        console.log('Wechaty is not running.');
-    }
-}
-
-
-let Status = { status: null }
-let User = {name: null}
-
-
-async function wxlogin() {
-    if (isRunning) {
-        isRunning = false
-        await wechaty.stop();
-        Status.status = 0;
-        console.log('Wechaty stopped.');
-    } else {
-        console.log('Wechaty is not running.');
-    }
-
-    isRunning = true
-    return new Promise((resolve, reject) => {
-        let qrcodeUrl
-
-        // 解除之前绑定的所有事件处理程序
-        wechaty.removeAllListeners();
-
-        wechaty
-            .on('scan', (qrcode, status) => {
-                qrcodeUrl = `https://my.tv.sohu.com/user/a/wvideo/getQRCode.do?text=${encodeURIComponent(qrcode)}`
-                console.log('Scan QR Code to log in:', status)
-                Status.status = status
-                // 将 qrcodeUrl 提前返回
-                resolve(qrcodeUrl)
-            })
-
-            .on('login', async (user) => {
-                console.log(`User ${user} logged in`);
-                Status.status = 200
-                // 获取登录用户的信息
-                const contact = await wechaty.Contact.find({ id: user.id });
-                const name = await contact.name();
-                const avatarFileBox = await contact.avatar();
-                User.name = name
-                // 将头像保存到本地
-                const avatarFilePath = `./wechat/avatar/avatar.jpg`
-                await avatarFileBox.toFile(avatarFilePath,true)
-                // 有程序运行后配置未加载的问题,这里重新加载一遍
-                loadConfigValues()
-            })
-
-            .on('logout', async () => {
-                Status.status = null
-                isRunning = false
-                await wechaty.stop()
-            })
-
-            .on('message',async (message) => {
-                if (message.type() === wechaty.Message.Type.Text) {
-                    const content = message.text()
-                    console.log(content)
-                    const room = message.room()
-                    const talker = message.talker()
-                    const talkername = message.talker().payload.name
-                    const foundWords = keyWords.filter(word => content.includes(word))
-
-                    if (room) {
-                        const roomname = message.room().payload.topic
-                        if (whiteRoom.length === 0 || whiteRoom.includes(roomname)) {
-                            //在群聊中被@
-                            if (await message.mentionSelf()) {
-                                console.log('机器人被@')
-                                if (atReply) {
-                                    const apiMessage = await sendMessageToAPI(content)
-                                    const senmsg = '@' + talkername + '  ' + prefix + apiMessage + suffix
-                                    room.say(senmsg)
-                                    //写入数据库
-                                    writeToDatabase({
-                                        time: getCurrentTime(),
-                                        type: '群聊',
-                                        recmsg: content,
-                                        senmsg: senmsg,
-                                        name: talkername,
-                                        roomname: roomname,
-                                    })
-                                    return
-                                }
-                            } else if (foundWords.length > 0) {
-                                console.log('发现关键字')
-                                const apiMessage = await sendMessageToAPI(content)
-                                const senmsg = '@' + talkername + '  ' + prefix + apiMessage + suffix
-                                room.say(senmsg)
-                                //写入数据库
-                                writeToDatabase({
-                                    time: getCurrentTime(),
-                                    type: '群聊',
-                                    recmsg: content,
-                                    senmsg: senmsg,
-                                    name: talkername,
-                                    roomname: roomname,
-                                })
-                                return
-                            }
-                        } else {
-                            return
-                        }
-
-                    } else {
-                        if (autoReplySingle) {
-                             if (blackName.includes(talkername)) {
-                                console.log('发送者在黑名单中')
-                                return
-                             } else {
-                                const apiMessage = await sendMessageToAPI(content)
-                                const senmsg = prefix + apiMessage + suffix
-                                talker.say(senmsg)
-                                //向数据库写入消息
-                                writeToDatabase({
-                                    time: getCurrentTime(),
-                                    type: '私聊',
-                                    recmsg: content,
-                                    senmsg: senmsg,
-                                    name: message.talker().payload.name,
-                                    roomname: null,
-                                })
-                                return
-                            }
-                        }
-                    }
-                } else {
-                    console.log('不受支持的消息类型')
-                    return
-                } 
-            }
-        )
-        wechaty.start()
-        
-        wechaty.on('error', (error) => {
-            reject(error)
-        })
-    })
-}
-
-//向数据库写入数据
-function writeToDatabase(data) {
-    const { time, type, recmsg, senmsg, name, roomname } = data
-
-    const insertQuery = `INSERT INTO message (time, type, recmsg, senmsg, name, roomname) VALUES (?, ?, ?, ?, ?, ?)`
-    db.run(insertQuery, [time, type, recmsg, senmsg, name, roomname], (error) => {
-        if (error) {
-            console.error('数据库写入失败:', error)
-        } else {
-            console.log('数据库写入成功')
-        }
-    })
-}
-
-// 更新设置到数据库
-function updateConfigValue(configName, configValue) {
-    const query = 'INSERT OR REPLACE INTO wxconfig (config, value) VALUES (?, ?)'
-    db.run(query, [configName, configValue], (err) => {
-        if (err) {
-            console.error('数据库写入失败:', err)
-        } else {
-            console.log('数据库写入成功')
-        }
-    })
-}
-
-// 设置是否自动回复
-function setAutoReplySingle(value) {
-    updateConfigValue('autoReplySingle', value)
-}
-
-function setatReply(value) {
-    updateConfigValue('atReply', value)
-}
-
-function setblackName(value) {
-    updateConfigValue('blackName', value)
-}
-
-function setkeyWords(value) {
-    updateConfigValue('keyWords', value)
-}
-
-function setwhiteRoom(value) {
-    updateConfigValue('whiteRoom', value)
-}
-
-// 修改前缀
-function setSuffix(value) {
-    updateConfigValue('suffix', value)
-}
-
-// 修改后缀
-function setPrefix(value) {
-    updateConfigValue('prefix', value)
-}
-
-
-module.exports = {
-    wxlogin,
-    Status,
-    setAutoReplySingle,
-    setwhiteRoom,
-    setatReply,
-    setkeyWords,
-    setblackName,
-    setSuffix,
-    setPrefix,
-    stopWx,
-    loadConfigValues,
-    User
-}

+ 14 - 67
wechat/main.js

@@ -1,5 +1,5 @@
 const { WechatyBuilder } = require("wechaty")
-const { sendMessageToAPI } = require('./getmessage')
+const { sendMessageToAPI } = require('./ChatGPT')
 const sqlite3 = require('sqlite3')
 
 //sqlite数据库路径
@@ -44,7 +44,6 @@ async function loadConfigValues() {
         keyWords = keyWordsString !== null ? keyWordsString.split(",").map(item => item.trim()) : []
         blackName = blackNameString !== null ? blackNameString.split(",").map(item => item.trim()) : []
 
-        console.log('Config values loaded successfully.')
     } catch (error) {
         console.error('Error loading config values:', error)
     }
@@ -76,12 +75,9 @@ let isRunning = false
 async function stopWx() {
     if (isRunning) {
         isRunning = false
-        await wechaty.stop();
-        Status.status = 0;
-        console.log('Wechaty stopped.');
-    } else {
-        console.log('Wechaty is not running.');
-    }
+        await wechaty.stop()
+        Status.status = 0
+    } 
 }
 
 
@@ -92,36 +88,30 @@ let User = {name: null}
 async function wxlogin() {
     if (isRunning) {
         isRunning = false
-        await wechaty.stop();
-        Status.status = 0;
-        console.log('Wechaty stopped.');
-    } else {
-        console.log('Wechaty is not running.');
-    }
-
+        await wechaty.stop()
+        Status.status = 0
+    } 
     isRunning = true
     return new Promise((resolve, reject) => {
         let qrcodeUrl
 
         // 解除之前绑定的所有事件处理程序
-        wechaty.removeAllListeners();
+        wechaty.removeAllListeners()
 
         wechaty
             .on('scan', (qrcode, status) => {
                 qrcodeUrl = `https://my.tv.sohu.com/user/a/wvideo/getQRCode.do?text=${encodeURIComponent(qrcode)}`
-                console.log('Scan QR Code to log in:', status)
                 Status.status = status
                 // 将 qrcodeUrl 提前返回
                 resolve(qrcodeUrl)
             })
 
             .on('login', async (user) => {
-                console.log(`User ${user} logged in`);
                 Status.status = 200
                 // 获取登录用户的信息
-                const contact = await wechaty.Contact.find({ id: user.id });
-                const name = await contact.name();
-                const avatarFileBox = await contact.avatar();
+                const contact = await wechaty.Contact.find({ id: user.id })
+                const name = await contact.name()
+                const avatarFileBox = await contact.avatar()
                 User.name = name
                 // 将头像保存到本地
                 const avatarFilePath = `./wechat/avatar/avatar.jpg`
@@ -138,12 +128,10 @@ async function wxlogin() {
 
             .on('message',async (message) => {
                 if (message.self()) {
-                    console.log('自己发送消息:'+message.text())
                     return
                 }   else  {
                     if (message.type() === wechaty.Message.Type.Text) {
                         const content = message.text()
-                        console.log(content)
                         const room = message.room()
                         const talker = message.talker()
                         const talkername = message.talker().payload.name
@@ -193,13 +181,11 @@ async function wxlogin() {
                         } else {
                             if (autoReplySingle) {
                                 if (blackName.includes(talkername)) {
-                                    console.log('发送者在黑名单中')
                                     return
                                 } else {
                                     const apiMessage = await sendMessageToAPI(content)
                                     const senmsg = prefix + apiMessage + suffix
                                     talker.say(senmsg)
-                                    //向数据库写入消息
                                     writeToDatabase({
                                         time: getCurrentTime(),
                                         type: '私聊',
@@ -213,7 +199,6 @@ async function wxlogin() {
                             }
                         }
                     } else {
-                        console.log('不受支持的消息类型')
                         return
                     } 
                 }
@@ -236,8 +221,6 @@ function writeToDatabase(data) {
     db.run(insertQuery, [time, type, recmsg, senmsg, name, roomname], (error) => {
         if (error) {
             console.error('数据库写入失败:', error)
-        } else {
-            console.log('数据库写入成功')
         }
     })
 }
@@ -248,54 +231,18 @@ function updateConfigValue(configName, configValue) {
     db.run(query, [configName, configValue], (err) => {
         if (err) {
             console.error('数据库写入失败:', err)
-        } else {
-            console.log('数据库写入成功')
         }
     })
 }
 
-// 设置是否自动回复
-function setAutoReplySingle(value) {
-    updateConfigValue('autoReplySingle', value)
-}
-
-function setatReply(value) {
-    updateConfigValue('atReply', value)
-}
-
-function setblackName(value) {
-    updateConfigValue('blackName', value)
-}
-
-function setkeyWords(value) {
-    updateConfigValue('keyWords', value)
-}
-
-function setwhiteRoom(value) {
-    updateConfigValue('whiteRoom', value)
-}
-
-// 修改前缀
-function setSuffix(value) {
-    updateConfigValue('suffix', value)
-}
-
-// 修改后缀
-function setPrefix(value) {
-    updateConfigValue('prefix', value)
+function setWx(key,value) {
+    updateConfigValue(key,value)
 }
 
-
 module.exports = {
     wxlogin,
     Status,
-    setAutoReplySingle,
-    setwhiteRoom,
-    setatReply,
-    setkeyWords,
-    setblackName,
-    setSuffix,
-    setPrefix,
+    setWx,
     stopWx,
     loadConfigValues,
     User