Skip to content

Commit a85c960

Browse files
author
Marchccc
committed
no message
1 parent 0fb00bf commit a85c960

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
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.1.0",
4+
"version": "1.1.2",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",
77
"files": [

src/index.ts

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

33
export const usage = `
4-
## koishi-plugin-api-handler v1.1.0
4+
## koishi-plugin-api-handler v1.1.2
55
66
1. 配置API地址。
77
@@ -12,6 +12,7 @@ export const usage = `
1212
4. 服务器需返回一个字符串,该字符串将作为机器人的回复消息。
1313
1414
注意:只有群内@机器人的消息才会处理。
15+
注意:正则配置适用于群内和私聊。
1516
1617
1718
微信配合adapter-wechat4u使用。
@@ -29,6 +30,10 @@ export interface Config {
2930
prefix_2: string,
3031
prefix_3?: string,
3132
prefix_4?: string,
33+
regex_1: string,
34+
regex_2: string,
35+
regex_3?: string,
36+
regex_4?: string,
3237
wx_api?: string,
3338
wx_token?: string,
3439
bot_name?: string,
@@ -42,6 +47,10 @@ export const Config: Schema<Config> = Schema.intersect([
4247
prefix_2: Schema.string().description('匹配消息前缀2'),
4348
prefix_3: Schema.string().description('匹配消息前缀3'),
4449
prefix_4: Schema.string().description('匹配消息前缀4'),
50+
regex_1: Schema.string().description('正则匹配消息1').description('正则匹配消息,符合则将用户消息发送到API,由API返回回复内容字符串'),
51+
regex_2: Schema.string().description('正则匹配消息2'),
52+
regex_3: Schema.string().description('正则匹配消息3'),
53+
regex_4: Schema.string().description('正则匹配消息4'),
4554
}).description('基础设置'),
4655
Schema.object({
4756
wx_api: Schema.string().description('API地址'),
@@ -73,7 +82,7 @@ export function startsWithPrefix(str: string, prefix: string) {
7382
}
7483

7584
export function handlePrefixes(sessionContent: string, config: Config) {
76-
for (let i = 1; i <= 4; i++) { // 假设有四个前缀
85+
for (let i = 1; i <= 4; i++) {
7786
let prefixKey = `prefix_${i}`;
7887
if (config[prefixKey]) {
7988
if (startsWithPrefix(sessionContent, config[prefixKey])) {
@@ -84,6 +93,18 @@ export function handlePrefixes(sessionContent: string, config: Config) {
8493
}
8594
}
8695

96+
export function handleRegex(sessionContent: string, config: Config) {
97+
for (let i = 1; i <= 4; i++) {
98+
let prefixKey = `regex_${i}`;
99+
if (config[prefixKey]) {
100+
if ((new RegExp(config[prefixKey])).test(sessionContent)) {
101+
return `匹配正则${i}`;
102+
}
103+
}
104+
}
105+
106+
}
107+
87108
export function apply(ctx: Context, config: Config) {
88109
ctx.middleware(async (session, next) => {
89110

@@ -155,14 +176,34 @@ export function apply(ctx: Context, config: Config) {
155176

156177
// 私聊直接返回AI内容
157178
if (isPrivateChat) {
179+
180+
// 私聊也处理正则
181+
let match_regex = handleRegex(cleanContent, config)
182+
if (match_regex) {
183+
logger.info(match_regex)
184+
const res = await ctx.http.post(config.api, {
185+
token: config.token,
186+
message: cleanContent,
187+
channelId: session.channelId,
188+
})
189+
.catch((err) => {
190+
return { error: err.message }
191+
})
192+
if (res !== undefined) {
193+
return res;
194+
}
195+
}
196+
158197
return next()
159198
}
160199

161200
// 群内@,如果匹配到前缀,请求API结果
162201
// 如未匹配到,返回AI内容
163202
let match_prefix = handlePrefixes(cleanContent, config)
164-
if (match_prefix) {
203+
let match_regex = handleRegex(cleanContent, config)
204+
if (match_prefix || match_regex) {
165205
logger.info(match_prefix)
206+
logger.info(match_regex)
166207
const res = await ctx.http.post(config.api, {
167208
token: config.token,
168209
message: cleanContent,

tsconfig.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)