diff --git a/CHANGELOG.md b/CHANGELOG.md index 7805c26..0a59924 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 更新日志 +### V1.6.59(2024-07-12) +1、Dify 和 FastGPT 添加备注参数,可以获取用户的备注信息,更加个性化 + ### V1.6.58(2024-07-10) 1、Dify 和 FastGPT 每次调用都会携带用户信息和群聊信息,可以使用环境变量获取,实现真正的个性化机器人服务,更像真人 diff --git a/package.json b/package.json index ea56862..3ed2b8b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wechaty-web-panel", - "version": "1.6.58", + "version": "1.6.59", "description": "智能微秘书插件", "exports": { ".": { diff --git a/src/common/reply.js b/src/common/reply.js index 88138ba..a34d50f 100644 --- a/src/common/reply.js +++ b/src/common/reply.js @@ -12,8 +12,8 @@ export async function getContactTextReply(that, contact, msg) { * @param {*} name 发消息者昵称 * @param {*} id 发消息者id */ -export async function getRoomTextReply({ that, content, name, id, avatar, room, isMention, roomName, roomId, isFriend }) { - let result = await service.filterRoomMsg({ that, msg: content, name, id, avatar, room, isMention, roomName, roomId, isFriend }) +export async function getRoomTextReply({ that, content, name, id, avatar, room, isMention, roomName, userAlias, roomId, isFriend }) { + let result = await service.filterRoomMsg({ that, msg: content, userAlias, name, id, avatar, room, isMention, roomName, roomId, isFriend }) return result } diff --git a/src/handlers/on-message.js b/src/handlers/on-message.js index fcc9d4c..97a54ac 100644 --- a/src/handlers/on-message.js +++ b/src/handlers/on-message.js @@ -47,6 +47,7 @@ async function dispatchFriendFilterByMsgType(that, msg) { const type = msg.type() const contact = msg.talker() // 发消息人 const name = await contact.name() + const userAlias = await contact.alias() || ''; const isOfficial = contact.type() === that.Contact.Type.Official let content = '' let replys = [] @@ -82,6 +83,7 @@ async function dispatchFriendFilterByMsgType(that, msg) { id: contact.id, roomName: '', isMention: false, + userAlias, name, msgContent: { type: 1, content } }) @@ -156,6 +158,7 @@ async function dispatchFriendFilterByMsgType(that, msg) { id: contact.id, uniqueId: contact.id, roomName: '', + userAlias, isMention: false, name, msgContent: { type: 3, id: msg.id } @@ -214,6 +217,7 @@ async function dispatchRoomFilterByMsgType(that, room, msg) { let contactId = contact.id let contactAvatar = await contact.avatar() const userSelfName = that.currentUser?.name() || that.userSelf()?.name() + const userAlias = await room?.alias(contact) || await contact.alias() || '' switch (type) { case that.Message.Type.Text: @@ -247,6 +251,7 @@ async function dispatchRoomFilterByMsgType(that, room, msg) { roomName, isMention: mentionSelf, name: contactName, + userAlias, msgContent: { type: 1, content } }) if (gpt4vReplys.length) { @@ -261,6 +266,7 @@ async function dispatchRoomFilterByMsgType(that, room, msg) { content, isFriend, name: contactName, + userAlias, id: contactId, roomId: room.id, avatar: contactAvatar, @@ -357,6 +363,7 @@ async function dispatchRoomFilterByMsgType(that, room, msg) { roomId: room.id, avatar: contactAvatar, room, + userAlias, roomName, isMention: true }) diff --git a/src/proxy/bot/dispatch.js b/src/proxy/bot/dispatch.js index 49e65b1..ec12707 100644 --- a/src/proxy/bot/dispatch.js +++ b/src/proxy/bot/dispatch.js @@ -10,7 +10,7 @@ import globalConfig from '../../db/global.js' * @param {botType: 机器人类别, content: 消息内容, uid: 说话的用户id, updateId: 更新的用户id, adminId: 对话实例id,用于分割不同配置, config: 机器人配置} * @returns */ -export async function dispatchBot({botType, content, id, uid, uname, roomId, roomName, adminId, config}) { +export async function dispatchBot({botType, userAlias, content, id, uid, uname, roomId, roomName, adminId, config}) { console.log('进入定制机器人回复'); try { const gptConfig = globalConfig.getGptConfigById(adminId); @@ -30,12 +30,12 @@ export async function dispatchBot({botType, content, id, uid, uname, roomId, roo case 8: // dify ai console.log('进入Dify聊天'); - res = await getDifyAiReply({ content, inputs: { uid, uname, roomId, roomName } }, id, adminId, config) + res = await getDifyAiReply({ content, inputs: { uid, ualias: userAlias, uname, roomId, roomName } }, id, adminId, config) replys = res break case 9: // fastGPT api - res = await getChatGPTReply({ content, variables: { uid, uname, roomId, roomName } }, id, adminId, config, true) + res = await getChatGPTReply({ content, variables: { uid, ualias: userAlias, uname, roomId, roomName } }, id, adminId, config, true) replys = res break case 11: diff --git a/src/service/event-dispatch-service.js b/src/service/event-dispatch-service.js index bbfc677..06c4b11 100644 --- a/src/service/event-dispatch-service.js +++ b/src/service/event-dispatch-service.js @@ -143,7 +143,7 @@ async function dispatchEventContent(that, eName, msg, name, id, avatar, room, ro * @param {*} name 发消息人 * @param {*} id 发消息人id */ -async function dispatchAiBot({ bot, msg, name, id, uid, uname, roomId, roomName }) { +async function dispatchAiBot({ bot, msg, name, id, uid, uname, roomId, userAlias, roomName }) { try { let res, replys switch (bot) { @@ -169,12 +169,12 @@ async function dispatchAiBot({ bot, msg, name, id, uid, uname, roomId, roomName break case 8: // dify ai - res = await getDifyReply({ content: msg, id, inputs: { uid, uname, roomId, roomName } }) + res = await getDifyReply({ content: msg, id, inputs: { uid, ualias: userAlias, uname, roomId, roomName } }) replys = res break case 9: // fast gpt - res = await getGptOfficialReply(msg, id, true, { uid, uname, roomId, roomName }) + res = await getGptOfficialReply(msg, id, true, { uid, uname, ualias: userAlias, roomId, roomName }) replys = res break case 11: @@ -193,7 +193,7 @@ async function dispatchAiBot({ bot, msg, name, id, uid, uname, roomId, roomName } } -async function dispatchSummerBot({ content, id, uid, uname, roomId, roomName, config}) { +async function dispatchSummerBot({ content, id, uid, uname, roomId, roomName, userAlias, config}) { try { let res, replys switch (config.botType) { @@ -204,12 +204,12 @@ async function dispatchSummerBot({ content, id, uid, uname, roomId, roomName, co break case 8: // dify ai - res = await getDifySimpleReply({content, id, inputs: { uid, uname, roomId, roomName }, config}) + res = await getDifySimpleReply({content, id, inputs: { uid, uname, ualias: userAlias, roomId, roomName }, config}) replys = res break case 9: // fast gpt - res = await getSimpleGptReply({content, uid: id, config, isFastGPT:true, variables: { uid, uname, roomId, roomName } }) + res = await getSimpleGptReply({content, uid: id, config, isFastGPT:true, variables: { uid, ualias: userAlias, uname, roomId, roomName } }) replys = res break case 11: diff --git a/src/service/gpt4vService.js b/src/service/gpt4vService.js index ebe2562..64968d6 100644 --- a/src/service/gpt4vService.js +++ b/src/service/gpt4vService.js @@ -5,7 +5,7 @@ import globalConfig from '../db/global.js' let gpt4vRes = '' -export async function getGpt4vChat({ room, roomId, roomName, isMention, msgContent, name, id, uniqueId, that }) { +export async function getGpt4vChat({ room, roomId, roomName, isMention, userAlias, msgContent, name, id, uniqueId, that }) { if (!gpt4vRes) { gpt4vRes = new BotManage(100, that) } diff --git a/src/service/msg-filter-service.js b/src/service/msg-filter-service.js index 6fdf2c1..45bb6bd 100644 --- a/src/service/msg-filter-service.js +++ b/src/service/msg-filter-service.js @@ -5,13 +5,13 @@ const WEIXINOFFICIAL = ['朋友推荐消息', '微信支付', '微信运动', ' const DELETEFRIEND = '开启了朋友验证' // 被人删除后,防止重复回复 const REMINDKEY = '提醒' const NEWADDFRIEND = '你已添加' -async function getMsgReply(resArray, { that, msg, name, contact, config, avatar, id, room, isMention, roomName, roomId, isFriend }) { +async function getMsgReply(resArray, { that, msg, name, contact, config, avatar, userAlias, id, room, isMention, roomName, roomId, isFriend }) { try { let msgArr = [] for (let i = 0; i < resArray.length; i++) { const item = resArray[i] if (item.bool) { - msgArr = (await msgFilter[item.method]({ that, msg, name, contact, config, avatar, id, room, isMention, roomName, roomId, isFriend })) || [] + msgArr = (await msgFilter[item.method]({ that, msg, name, contact, config, avatar, id, room, userAlias, isMention, roomName, roomId, isFriend })) || [] } if (msgArr.length > 0) { return msgArr @@ -36,6 +36,7 @@ async function filterFriendMsg(that, contact, msg) { const config = await allConfig() // 获取配置信息 const gptConfig = globalConfig.getAllGptConfig() // 获取gpt配置信息 const name = contact.name() + const userAlias = await contact.alias() || ''; const id = contact.id const avatar = await contact.avatar() const resArray = [ @@ -55,7 +56,7 @@ async function filterFriendMsg(that, contact, msg) { { bool: config.customBot && config.customBot.open, method: 'customBot' }, { bool: config.autoReply && config.botScope !== 'room', method: 'robotMsg' }, ] - const msgArr = await getMsgReply(resArray, { that, msg, contact, name, config, avatar, id }) + const msgArr = await getMsgReply(resArray, { that, msg, userAlias, contact, name, config, avatar, id }) return msgArr.length > 0 ? msgArr : [{ type: 1, content: '', url: '' }] } catch (e) { console.log('filterFriendMsg error', e) @@ -73,7 +74,7 @@ async function filterFriendMsg(that, contact, msg) { * 1 开启了好友验证 || 朋友推荐消息 || 发送的文字消息过长,大于40个字符 * 2 初次添加好友 */ -async function filterRoomMsg({that, msg, name, id, avatar, room, isMention, roomName, roomId, isFriend }) { +async function filterRoomMsg({that, msg, name, id, avatar, userAlias, room, isMention, roomName, roomId, isFriend }) { try { const config = await allConfig() // 获取配置信息 const gptConfig = globalConfig.getAllGptConfig() // 获取gpt配置信息 @@ -89,7 +90,7 @@ async function filterRoomMsg({that, msg, name, id, avatar, room, isMention, room { bool: config.customBot && config.customBot.open, method: 'customBot' }, { bool: config.autoReply && config.botScope !== 'friend', method: 'robotMsg' }, ] - const msgArr = await getMsgReply(resArray, { that, msg, name, config, avatar, id, room, roomName, roomId, isMention, isFriend }) + const msgArr = await getMsgReply(resArray, { that, msg, name, config, avatar, id, room, userAlias, roomName, roomId, isMention, isFriend }) return msgArr.length > 0 ? msgArr : [{ type: 1, content: '', url: '' }] } catch (e) { console.log('filterRoomMsg error', e) diff --git a/src/service/msg-filters.js b/src/service/msg-filters.js index 8ee15b5..817aa27 100644 --- a/src/service/msg-filters.js +++ b/src/service/msg-filters.js @@ -251,7 +251,7 @@ async function keywordsMsg({ msg, config, room, isMention }) { } } -async function robotMsg({ msg, name, id, config, isMention, roomName, room, roomId, isFriend }) { +async function robotMsg({ msg, name, id, config, isMention, roomName, userAlias, room, roomId, isFriend }) { // 如果群里没有提及不开启机器人聊天 if (room && !isMention && config.roomAt || room && !isMention && !config.roomAt && isFriend && config.friendNoReplyInRoom) { return [] @@ -260,7 +260,7 @@ async function robotMsg({ msg, name, id, config, isMention, roomName, room, room let msgArr = [] // 返回的消息列表 if (config.autoReply) { console.log('开启了机器人自动回复功能') - msgArr = await dispatch.dispatchAiBot({ bot: config.defaultBot, msg, name, uname: name, uid: id, roomName: roomName || '', roomId: roomId || '', id: `${roomId ? roomId + '_' : ''}${id}` }) + msgArr = await dispatch.dispatchAiBot({ bot: config.defaultBot, msg, name, userAlias, uname: name, uid: id, roomName: roomName || '', roomId: roomId || '', id: `${roomId ? roomId + '_' : ''}${id}` }) } else { console.log('没有开启机器人自动回复功能') msgArr = [{ type: 1, content: '', url: '' }] @@ -326,7 +326,7 @@ async function getCustomConfig({ name, id, room, roomId, roomName, type }) { } } -async function customChat({ msg, name, id, config, isMention, room, roomId, roomName }) { +async function customChat({ msg, name, id, config, isMention, room, userAlias, roomId, roomName }) { try { const gptConfigs = globalConfig.getAllGptConfig() if (gptConfigs && gptConfigs.length) { @@ -350,6 +350,7 @@ async function customChat({ msg, name, id, config, isMention, room, roomId, room uid: id, uname: name, roomId, + userAlias, roomName }) if (msgArr.length) return msgArr @@ -402,7 +403,7 @@ function preventWordCheck({ msg, config, isMention, room }) { return [] } -async function customBot({ that, msg, name, id, config, room, isMention }) { +async function customBot({ that, msg, name, userAlias, id, config, room, isMention }) { const item = config.customBot // 如果匹配到关键词 群消息要求是必须@,但是没@ 就不需要回复 || 当为群消息关键词只在好友私聊时触发 || 非群消息只在群中触发 if ((room && item.needAt === 1 && !isMention) || (room && item.needAt === undefined && !isMention) || (room && item.scope === 'friend') || (!room && item.scope === 'room')) { @@ -415,6 +416,7 @@ async function customBot({ that, msg, name, id, config, room, isMention }) { robotId: contactSelf.robotId, uid: id, uname: name, + ualias: userAlias, roomId: (room && room.id) || '', roomName: (room && topic) || '', word: msg @@ -422,7 +424,7 @@ async function customBot({ that, msg, name, id, config, room, isMention }) { item.moreData && item.moreData.length && item.moreData.forEach((mItem) => { - if (mItem.key !== 'uid' && mItem.key !== 'uname' && mItem.key !== 'word' && mItem.key !== 'roomId' && mItem.key !== 'roomName' && mItem.key !== 'robotId') { + if (mItem.key !== 'uid' && mItem.key !== 'uname' && mItem.key !== 'ualias' && mItem.key !== 'word' && mItem.key !== 'roomId' && mItem.key !== 'roomName' && mItem.key !== 'robotId') { data[mItem.key] = mItem.value } }) @@ -524,7 +526,7 @@ export async function keywordForward({ that, msg, name, id, config, room, isMent } -export async function summerChat({ that, msg, name, id, config, room, isMention, roomId, roomName }) { +export async function summerChat({ that, msg, name, id, config, room, userAlias, isMention, roomId, roomName }) { const { role } = config.userInfo const item = config.summerConfig // 如果关闭了 或者关键词没有匹配上 直接不需要总结回复 @@ -550,7 +552,7 @@ export async function summerChat({ that, msg, name, id, config, room, isMention, console.log('获取到的聊天内容', content) } console.log('开始总结聊天内容') - const res = await dispatch.dispatchSummerBot({content, config: item, uid: id, uname: name, roomId: roomId, roomName, id: room ? `${roomId ? roomId + '_' : ''}${id}`: id}) + const res = await dispatch.dispatchSummerBot({content, config: item, uid: id, userAlias, uname: name, roomId: roomId, roomName, id: room ? `${roomId ? roomId + '_' : ''}${id}`: id}) return res; } else { console.log('没有获取到任何聊天记录内容,无法进行总结,请确认已经开启聊天记录')