|
1 | 1 | import { Context, Schema, Logger } from 'koishi' |
2 | 2 |
|
3 | 3 | export const usage = ` |
4 | | -## koishi-plugin-api-handler v1.0.2 |
| 4 | +## koishi-plugin-api-handler v1.0.6 |
5 | 5 |
|
6 | 6 | 1. 配置API地址。 |
7 | 7 |
|
8 | 8 | 2. 设置消息前缀,例如:tx。 |
9 | 9 |
|
10 | | -3. 当用户发送的消息以该前缀开头(如tx1234或Tx1234)时,系统将通过POST方式向API发送请求,包括参数:token、message、session。 |
| 10 | +3. 当用户发送的消息以该前缀开头(如tx1234或Tx1234)时,系统将通过POST方式向API发送请求,包括参数:token、message、channelId |
11 | 11 |
|
12 | 12 | 4. 服务器需返回一个字符串,该字符串将作为机器人的回复消息。 |
13 | 13 |
|
| 14 | +注意:只有群内@机器人的消息才会处理。 |
| 15 | +
|
14 | 16 | ` |
15 | 17 |
|
16 | 18 | export const name = 'koishi-plugin-api-handler' |
@@ -60,23 +62,36 @@ export function handlePrefixes(sessionContent: string, config: Config) { |
60 | 62 |
|
61 | 63 | export function apply(ctx: Context, config: Config) { |
62 | 64 | 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) |
68 | 84 | if (match_prefix) { |
69 | | - logger.info(match_prefix + ':' + session.content) |
| 85 | + logger.info(match_prefix) |
70 | 86 | const res = await ctx.http.post(config.api, { |
71 | 87 | token: config.token, |
72 | | - message: session.content, |
73 | | - session: session, |
| 88 | + message: cleanContent, |
| 89 | + channelId: session.channelId, |
74 | 90 | }) |
75 | 91 | .catch((err) => { |
76 | 92 | return { error: err.message } |
77 | 93 | }) |
78 | 94 | if (res !== undefined) { |
79 | | - // console.log(res); |
80 | 95 | return res; |
81 | 96 | } |
82 | 97 |
|
|
0 commit comments