@lih-x-x/kmr 1.0.16 → 1.0.18
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.
- package/dist/index.js +32 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,7 +13,26 @@ function createLarkClient(config) {
|
|
|
13
13
|
appType: lark.AppType.SelfBuild
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
function
|
|
16
|
+
async function getBotOpenId(client) {
|
|
17
|
+
try {
|
|
18
|
+
const res = await client.request({
|
|
19
|
+
method: "GET",
|
|
20
|
+
url: "/open-apis/bot/v3/info"
|
|
21
|
+
});
|
|
22
|
+
console.log(`[bot] bot info response:`, JSON.stringify(res?.data || res, null, 2));
|
|
23
|
+
const openId = res.data?.bot?.open_id || res.bot?.open_id || "";
|
|
24
|
+
if (openId) {
|
|
25
|
+
console.log(`[bot] \u673A\u5668\u4EBA open_id: ${openId}`);
|
|
26
|
+
} else {
|
|
27
|
+
console.warn(`[bot] \u672A\u80FD\u83B7\u53D6\u673A\u5668\u4EBA open_id\uFF0C\u7FA4\u804A @\u5224\u65AD\u53EF\u80FD\u4E0D\u51C6\u786E`);
|
|
28
|
+
}
|
|
29
|
+
return openId;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
console.error(`[bot] \u83B7\u53D6\u673A\u5668\u4EBA\u4FE1\u606F\u5931\u8D25:`, err.message);
|
|
32
|
+
return "";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function createEventDispatcher(botOpenId, onMessage) {
|
|
17
36
|
const dispatcher = new lark.EventDispatcher({});
|
|
18
37
|
const processedMessages = /* @__PURE__ */ new Set();
|
|
19
38
|
dispatcher.register({
|
|
@@ -47,10 +66,17 @@ function createEventDispatcher(onMessage) {
|
|
|
47
66
|
const content = JSON.parse(message.content);
|
|
48
67
|
let text = content.text || "";
|
|
49
68
|
const chatId = message.chat_id;
|
|
50
|
-
const
|
|
69
|
+
const mentions = message.mentions || [];
|
|
70
|
+
if (mentions.length > 0) {
|
|
71
|
+
console.log(`[recv] mentions:`, JSON.stringify(mentions));
|
|
72
|
+
}
|
|
73
|
+
const isMentioned = mentions.length > 0 && (!botOpenId || mentions.some((m) => {
|
|
74
|
+
const mentionOpenId = m.id?.open_id || m.id_str || "";
|
|
75
|
+
return mentionOpenId === botOpenId;
|
|
76
|
+
}));
|
|
51
77
|
const isGroup = message.chat_type === "group";
|
|
52
|
-
if (
|
|
53
|
-
for (const mention of
|
|
78
|
+
if (mentions.length > 0) {
|
|
79
|
+
for (const mention of mentions) {
|
|
54
80
|
if (mention.key) {
|
|
55
81
|
text = text.replace(mention.key, "").trim();
|
|
56
82
|
}
|
|
@@ -1101,7 +1127,8 @@ async function main() {
|
|
|
1101
1127
|
const taskCreator = new TaskCreator(client);
|
|
1102
1128
|
const userResolver = new UserResolver(client);
|
|
1103
1129
|
const pendingConfirmations = /* @__PURE__ */ new Map();
|
|
1104
|
-
const
|
|
1130
|
+
const botOpenId = await getBotOpenId(client);
|
|
1131
|
+
const dispatcher = createEventDispatcher(botOpenId, async (messageId, text, chatId, senderId, meta) => {
|
|
1105
1132
|
const parsed = parseMessage(text);
|
|
1106
1133
|
console.log(`[route] \u6D88\u606F\u8DEF\u7531: type=${parsed.type}, messageId=${messageId}, isGroup=${meta.isGroup}, isMentioned=${meta.isMentioned}`);
|
|
1107
1134
|
if (meta.isGroup && !meta.isMentioned && parsed.type === "document_link" /* DOCUMENT_LINK */) {
|