Skip to content

Commit 93c1cd3

Browse files
author
Marchccc
committed
no message
1 parent dc930eb commit 93c1cd3

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "koishi-plugin-api-handler",
33
"description": "识别以特定前缀开头的消息,并通过POST请求调用API,将API的响应直接作为回复发送。",
4-
"version": "1.0.2",
4+
"version": "1.0.6",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",
77
"files": [

src/index.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { Context, Schema, Logger } from 'koishi'
22

33
export const usage = `
4-
## koishi-plugin-api-handler v1.0.2
4+
## koishi-plugin-api-handler v1.0.6
55
66
1. 配置API地址。
77
88
2. 设置消息前缀,例如:tx。
99
10-
3. 当用户发送的消息以该前缀开头(如tx1234或Tx1234)时,系统将通过POST方式向API发送请求,包括参数:token、message、session。
10+
3. 当用户发送的消息以该前缀开头(如tx1234或Tx1234)时,系统将通过POST方式向API发送请求,包括参数:token、message、channelId
1111
1212
4. 服务器需返回一个字符串,该字符串将作为机器人的回复消息。
1313
14+
注意:只有群内@机器人的消息才会处理。
15+
1416
`
1517

1618
export const name = 'koishi-plugin-api-handler'
@@ -60,23 +62,36 @@ export function handlePrefixes(sessionContent: string, config: Config) {
6062

6163
export function apply(ctx: Context, config: Config) {
6264
ctx.middleware(async (session, next) => {
63-
// console.log(config);
64-
console.log(session);
65-
console.log(session.event.message);
66-
console.log(session.content);
67-
let match_prefix = handlePrefixes(session.content, config)
65+
66+
const content = session.content;
67+
const botId = session.selfId;
68+
69+
logger.info('原始消息:' + content);
70+
logger.info('机器人QQ:' + botId);
71+
72+
// 使用正则表达式确保准确匹配特定的提到格式
73+
const mentionRegex = new RegExp(`<at id="${botId}"/>`);
74+
const isMentioned = content && mentionRegex.test(content);
75+
if (!isMentioned) {
76+
return next()
77+
}
78+
79+
const cleanContent = content.replace(/<at id="\d+"\/>/g, '').trim();
80+
logger.info('文本消息:' + cleanContent);
81+
logger.info('群组:' + session.channelId)
82+
83+
let match_prefix = handlePrefixes(cleanContent, config)
6884
if (match_prefix) {
69-
logger.info(match_prefix + ':' + session.content)
85+
logger.info(match_prefix)
7086
const res = await ctx.http.post(config.api, {
7187
token: config.token,
72-
message: session.content,
73-
session: session,
88+
message: cleanContent,
89+
channelId: session.channelId,
7490
})
7591
.catch((err) => {
7692
return { error: err.message }
7793
})
7894
if (res !== undefined) {
79-
// console.log(res);
8095
return res;
8196
}
8297

0 commit comments

Comments
 (0)