xiaozuoassistant 0.2.43 → 0.2.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -80,8 +80,46 @@ export class FeishuChannel extends BaseChannel {
|
|
|
80
80
|
if (sender.sender_type !== 'user') {
|
|
81
81
|
return;
|
|
82
82
|
}
|
|
83
|
+
// 在群聊中,只有当机器人被 @ 时才响应
|
|
84
|
+
if (event.chat_type === 'group') {
|
|
85
|
+
const mentions = event.mentions || [];
|
|
86
|
+
// 1. 如果群聊中没有任何人被 @,则忽略(防止机器人被动读取群消息时刷屏)
|
|
87
|
+
if (mentions.length === 0) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
// 2. 检查被 @ 的列表中是否包含自己
|
|
91
|
+
// 由于飞书返回的 mentions 对象里,如果被 @ 的是当前收到事件的机器人自己,
|
|
92
|
+
// 通常其 is_self 属性为 true,或者 name 匹配,或者 id 为空/不完整。
|
|
93
|
+
// 最稳妥的方法是依靠飞书 SDK 在下发事件时设置的 m.name === botName,
|
|
94
|
+
// 或者直接看是否有一个 `m.id.open_id` 未定义(因为机器人本身没有用户 open_id),
|
|
95
|
+
// 或者检查 is_self === true
|
|
96
|
+
const isMentionedSelf = mentions.some((m) => {
|
|
97
|
+
return m.name === botName || m.is_self === true || (!m.id || Object.keys(m.id).length === 0);
|
|
98
|
+
});
|
|
99
|
+
if (!isMentionedSelf) {
|
|
100
|
+
return; // 被 @ 的是别人,不是本机器人,忽略
|
|
101
|
+
}
|
|
102
|
+
}
|
|
83
103
|
try {
|
|
84
|
-
|
|
104
|
+
let content = JSON.parse(event.content).text;
|
|
105
|
+
// 清理飞书消息中的 @ 占位符 (例如 "@_user_1")
|
|
106
|
+
if (event.mentions && event.mentions.length > 0) {
|
|
107
|
+
// 如果是群聊且未被@,且我们拿到了消息(说明有获取全群消息的权限),我们必须拦截掉
|
|
108
|
+
if (event.chat_type === 'group') {
|
|
109
|
+
// 简单判断:如果文本中没有出现 @ 相关的占位符,且这是群聊,说明不是对机器人说的
|
|
110
|
+
if (!content.includes('@_user_')) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// 将所有的 "@_user_x" 占位符从文本中删掉,以免干扰大模型
|
|
115
|
+
event.mentions.forEach((m) => {
|
|
116
|
+
content = content.replace(m.key, '').trim();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
// 如果清理掉 @ 之后内容为空,说明用户只发了一个 "@机器人"
|
|
120
|
+
if (!content.trim()) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
85
123
|
// Use chat_id for group chats, open_id/user_id for p2p?
|
|
86
124
|
// Actually message.chat_id is universal for where the message comes from.
|
|
87
125
|
const sessionId = `feishu:${botName}:${event.chat_id}`;
|