Skip to content

Commit

Permalink
fix: 添加用户备注信息
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Jul 12, 2024
1 parent 318de1a commit 453230e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 更新日志

### V1.6.59(2024-07-12)
1、Dify 和 FastGPT 添加备注参数,可以获取用户的备注信息,更加个性化

### V1.6.58(2024-07-10)
1、Dify 和 FastGPT 每次调用都会携带用户信息和群聊信息,可以使用环境变量获取,实现真正的个性化机器人服务,更像真人

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-web-panel",
"version": "1.6.58",
"version": "1.6.59",
"description": "智能微秘书插件",
"exports": {
".": {
Expand Down
4 changes: 2 additions & 2 deletions src/common/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions src/handlers/on-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -82,6 +83,7 @@ async function dispatchFriendFilterByMsgType(that, msg) {
id: contact.id,
roomName: '',
isMention: false,
userAlias,
name,
msgContent: { type: 1, content }
})
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -247,6 +251,7 @@ async function dispatchRoomFilterByMsgType(that, room, msg) {
roomName,
isMention: mentionSelf,
name: contactName,
userAlias,
msgContent: { type: 1, content }
})
if (gpt4vReplys.length) {
Expand All @@ -261,6 +266,7 @@ async function dispatchRoomFilterByMsgType(that, room, msg) {
content,
isFriend,
name: contactName,
userAlias,
id: contactId,
roomId: room.id,
avatar: contactAvatar,
Expand Down Expand Up @@ -357,6 +363,7 @@ async function dispatchRoomFilterByMsgType(that, room, msg) {
roomId: room.id,
avatar: contactAvatar,
room,
userAlias,
roomName,
isMention: true
})
Expand Down
6 changes: 3 additions & 3 deletions src/proxy/bot/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/service/event-dispatch-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:
Expand All @@ -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) {
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/service/gpt4vService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions src/service/msg-filter-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = [
Expand All @@ -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)
Expand All @@ -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配置信息
Expand All @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions src/service/msg-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand All @@ -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: '' }]
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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')) {
Expand All @@ -415,14 +416,15 @@ 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
}
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
}
})
Expand Down Expand Up @@ -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
// 如果关闭了 或者关键词没有匹配上 直接不需要总结回复
Expand All @@ -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('没有获取到任何聊天记录内容,无法进行总结,请确认已经开启聊天记录')
Expand Down

0 comments on commit 453230e

Please sign in to comment.