main.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. const { WechatyBuilder } = require("wechaty")
  2. const { getGPTMessage } = require('../API/ChatGPT')
  3. const { getXunfeiMessage } = require('../API/xunfei')
  4. const sqlite3 = require('sqlite3')
  5. //sqlite数据库路径
  6. let sqliteDbPath = "./db/data.db"
  7. //打开数据库
  8. let db = new sqlite3.Database(sqliteDbPath)
  9. const wechaty = WechatyBuilder.build()
  10. function getConfigValue(configName) {
  11. return new Promise((resolve, reject) => {
  12. const query = 'SELECT value FROM wxconfig WHERE config = ?'
  13. db.get(query, [configName], (err, row) => {
  14. if (err) {
  15. reject(err)
  16. } else {
  17. const configValue = row ? row.value : null
  18. // 处理字符串 'null',如果是 'null' 则返回 null
  19. resolve(configValue === 'null' ? null : configValue)
  20. }
  21. })
  22. })
  23. }
  24. // 读取配置信息并设置相应的变量
  25. async function loadConfigValues() {
  26. try {
  27. autoReplySingle = await getConfigValue('autoReplySingle') === 'true'
  28. prefix = await getConfigValue('prefix')
  29. suffix = await getConfigValue('suffix')
  30. model = await getConfigValue('model')
  31. whiteRoomString = await getConfigValue('whiteRoom')
  32. keyWordsString = await getConfigValue('keyWords')
  33. blackNameString = await getConfigValue('blackName')
  34. atReply = await getConfigValue('atReply') === 'true'
  35. // 处理转义符
  36. suffix = suffix !== null ? suffix.replace(/\\n/g, '\n') : ''
  37. prefix = prefix !== null ? prefix.replace(/\\n/g, '\n') : ''
  38. // 处理用逗号分隔的字符串形式的数组
  39. whiteRoom = whiteRoomString !== null ? whiteRoomString.split(",").map(item => item.trim()) : []
  40. keyWords = keyWordsString !== null ? keyWordsString.split(",").map(item => item.trim()) : []
  41. blackName = blackNameString !== null ? blackNameString.split(",").map(item => item.trim()) : []
  42. } catch (error) {
  43. console.error('Error loading config values:', error)
  44. }
  45. }
  46. // 调用函数加载配置信息
  47. loadConfigValues()
  48. //选择模型
  49. async function sendMessageToAPI(message) {
  50. if (model==='xunfei'){
  51. const content = await getXunfeiMessage(message)
  52. return content
  53. } else if (model==='chatgpt') {
  54. const content = await getGPTMessage(message)
  55. return content
  56. } else {
  57. const content = '请在设置页面中选择语言模型'
  58. return content
  59. }
  60. }
  61. //获取时间
  62. function getCurrentTime() {
  63. const options = {
  64. year: 'numeric',
  65. month: '2-digit',
  66. day: '2-digit',
  67. hour: '2-digit',
  68. minute: '2-digit',
  69. second: '2-digit',
  70. }
  71. const currentTime = new Date().toLocaleString('zh-CN', options)
  72. return currentTime
  73. }
  74. //停止函数运行
  75. let isRunning = false
  76. async function stopWx() {
  77. if (isRunning) {
  78. isRunning = false
  79. await wechaty.stop()
  80. Status.status = 0
  81. }
  82. }
  83. let Status = { status: null }
  84. let User = {name: null}
  85. async function wxlogin() {
  86. if (isRunning) {
  87. isRunning = false
  88. await wechaty.stop()
  89. Status.status = 0
  90. }
  91. isRunning = true
  92. return new Promise((resolve, reject) => {
  93. let qrcodeUrl
  94. // 解除之前绑定的所有事件处理程序
  95. wechaty.removeAllListeners()
  96. wechaty
  97. .on('scan', (qrcode, status) => {
  98. qrcodeUrl = `https://my.tv.sohu.com/user/a/wvideo/getQRCode.do?text=${encodeURIComponent(qrcode)}`
  99. Status.status = status
  100. // 将 qrcodeUrl 提前返回
  101. resolve(qrcodeUrl)
  102. })
  103. .on('login', async (user) => {
  104. Status.status = 200
  105. // 获取登录用户的信息
  106. const contact = await wechaty.Contact.find({ id: user.id })
  107. const name = await contact.name()
  108. const avatarFileBox = await contact.avatar()
  109. User.name = name
  110. // 将头像保存到本地
  111. const avatarFilePath = `./wechat/avatar/avatar.jpg`
  112. await avatarFileBox.toFile(avatarFilePath,true)
  113. // 有程序运行后配置未加载的问题,这里重新加载一遍
  114. loadConfigValues()
  115. })
  116. .on('logout', async () => {
  117. Status.status = null
  118. isRunning = false
  119. await wechaty.stop()
  120. })
  121. .on('message',async (message) => {
  122. if (message.self()) {
  123. return
  124. } else {
  125. if (message.type() === wechaty.Message.Type.Text) {
  126. const content = message.text()
  127. const room = message.room()
  128. const talker = message.talker()
  129. const talkername = message.talker().payload.name
  130. const foundWords = keyWords.filter(word => content.includes(word))
  131. if (room) {
  132. const roomname = message.room().payload.topic
  133. if (whiteRoom.length === 0 || whiteRoom.includes(roomname)) {
  134. //在群聊中被@
  135. if (await message.mentionSelf()) {
  136. console.log('机器人被@')
  137. if (atReply) {
  138. const apiMessage = await sendMessageToAPI(content)
  139. const senmsg = '@' + talkername + ' ' + prefix + apiMessage + suffix
  140. room.say(senmsg)
  141. //写入数据库
  142. writeToDatabase({
  143. time: getCurrentTime(),
  144. type: '群聊',
  145. recmsg: content,
  146. senmsg: senmsg,
  147. name: talkername,
  148. roomname: roomname,
  149. })
  150. return
  151. }
  152. } else if (foundWords.length > 0) {
  153. console.log('发现关键字')
  154. const apiMessage = await sendMessageToAPI(content)
  155. const senmsg = '@' + talkername + ' ' + prefix + apiMessage + suffix
  156. room.say(senmsg)
  157. //写入数据库
  158. writeToDatabase({
  159. time: getCurrentTime(),
  160. type: '群聊',
  161. recmsg: content,
  162. senmsg: senmsg,
  163. name: talkername,
  164. roomname: roomname,
  165. })
  166. return
  167. }
  168. } else {
  169. return
  170. }
  171. } else {
  172. if (autoReplySingle) {
  173. if (blackName.includes(talkername)) {
  174. return
  175. } else {
  176. const apiMessage = await sendMessageToAPI(content)
  177. const senmsg = prefix + apiMessage + suffix
  178. talker.say(senmsg)
  179. writeToDatabase({
  180. time: getCurrentTime(),
  181. type: '私聊',
  182. recmsg: content,
  183. senmsg: senmsg,
  184. name: message.talker().payload.name,
  185. roomname: null,
  186. })
  187. return
  188. }
  189. }
  190. }
  191. } else {
  192. return
  193. }
  194. }
  195. }
  196. )
  197. wechaty.start()
  198. wechaty.on('error', (error) => {
  199. reject(error)
  200. })
  201. })
  202. }
  203. //向数据库写入数据
  204. function writeToDatabase(data) {
  205. const { time, type, recmsg, senmsg, name, roomname } = data
  206. const insertQuery = `INSERT INTO message (time, type, recmsg, senmsg, name, roomname) VALUES (?, ?, ?, ?, ?, ?)`
  207. db.run(insertQuery, [time, type, recmsg, senmsg, name, roomname], (error) => {
  208. if (error) {
  209. console.error('数据库写入失败:', error)
  210. }
  211. })
  212. }
  213. // 更新设置到数据库
  214. function updateConfigValue(configName, configValue) {
  215. const query = 'INSERT OR REPLACE INTO wxconfig (config, value) VALUES (?, ?)'
  216. db.run(query, [configName, configValue], (err) => {
  217. if (err) {
  218. console.error('数据库写入失败:', err)
  219. }
  220. })
  221. }
  222. function setWx(key,value) {
  223. updateConfigValue(key,value)
  224. }
  225. module.exports = {
  226. wxlogin,
  227. Status,
  228. setWx,
  229. stopWx,
  230. loadConfigValues,
  231. User
  232. }