Skip to content

Commit 6c6e5ad

Browse files
author
Marchccc
committed
no message
1 parent 93cc2f7 commit 6c6e5ad

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
insert_final_newline = true
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
* text eol=lf
2+
3+
*.png -text
4+
*.jpg -text
5+
*.ico -text
6+
*.gif -text
7+
*.webp -text

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
lib
2+
dist
3+
external
4+
5+
node_modules
6+
npm-debug.log
7+
yarn-debug.log
8+
yarn-error.log
9+
tsconfig.tsbuildinfo
10+
11+
.eslintcache
12+
.DS_Store
13+
.idea
14+
.vscode
15+
*.suo
16+
*.ntvs*
17+
*.njsproj
18+
*.sln

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
tsconfig.tsbuildinfo

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# koishi-plugin-api-handler
2+
3+
[![npm](https://img.shields.io/npm/v/koishi-plugin-echoapi?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-echoapi)
4+
25
识别以特定前缀开头的消息,并通过POST请求调用API,将API的响应直接作为回复发送。

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "koishi-plugin-echoapi",
3+
"description": "检测消息内容,并在内容符合预设规则时调用指定 API 生成回应。",
4+
"version": "0.0.1",
5+
"main": "lib/index.js",
6+
"typings": "lib/index.d.ts",
7+
"files": [
8+
"lib",
9+
"dist"
10+
],
11+
"license": "MIT",
12+
"scripts": {},
13+
"keywords": [
14+
"chatbot",
15+
"koishi",
16+
"plugin"
17+
],
18+
"devDependencies": {},
19+
"peerDependencies": {
20+
"koishi": "^4.17.9"
21+
}
22+
}

src/index.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Context, Schema, Logger } from 'koishi'
2+
3+
export const usage = `
4+
## koishi-plugin-api-handler v1.0
5+
6+
1. 配置API地址。
7+
8+
2. 设置消息前缀,例如:tx。
9+
10+
3. 当用户发送的消息以该前缀开头(如tx1234或Tx1234)时,系统将通过POST方式向API发送请求,包括参数:token、message、session。
11+
12+
4. 服务器需返回一个字符串,该字符串将作为机器人的回复消息。
13+
14+
`
15+
16+
export const name = 'koishi-plugin-api-handler'
17+
18+
export const logger = new Logger('koishi-plugin-api-handler')
19+
20+
export interface Config {
21+
api: string,
22+
token: string,
23+
prefix_1: string,
24+
prefix_2: string,
25+
prefix_3?: string,
26+
prefix_4?: string,
27+
}
28+
29+
export const Config: Schema<Config> = Schema.object({
30+
api: Schema.string().required(true).description('API地址'),
31+
token: Schema.string().required(true).description('API接收的token,以POST传递,参数名为token'),
32+
prefix_1: Schema.string().required(true).description('消息开头匹配字符串,不区分大小写,完全包含则将用户消息发送到API,由API返回回复内容字符串'),
33+
prefix_2: Schema.string().description('匹配消息前缀2'),
34+
prefix_3: Schema.string().description('匹配消息前缀3'),
35+
prefix_4: Schema.string().description('匹配消息前缀4'),
36+
})
37+
38+
export function startsWithPrefix(str: string, prefix: string) {
39+
// 首先去掉字符串前后的空格
40+
str = str.trim();
41+
42+
// 构建正则表达式,不区分大小写
43+
const regex = new RegExp('^' + prefix, 'i');
44+
45+
// 使用正则表达式判断是否以指定前缀开头
46+
return regex.test(str);
47+
}
48+
49+
export function handlePrefixes(sessionContent: string, config: Config) {
50+
for (let i = 1; i <= 4; i++) { // 假设有四个前缀
51+
let prefixKey = `prefix_${i}`;
52+
if (config[prefixKey]) {
53+
if (startsWithPrefix(sessionContent, config[prefixKey])) {
54+
// 这里可以根据前缀执行不同的逻辑
55+
return `匹配前缀${i}`;
56+
}
57+
}
58+
}
59+
}
60+
61+
export function apply(ctx: Context, config: Config) {
62+
ctx.middleware(async (session, next) => {
63+
// console.log(config);
64+
// console.log(session);
65+
// console.log(session.content);
66+
let match_prefix = handlePrefixes(session.content, config)
67+
let error = null;
68+
if (match_prefix) {
69+
logger.info(match_prefix + ':' + session.content)
70+
const res = await ctx.http.post(config.api, {
71+
token: config.token,
72+
message: session.content,
73+
session: session,
74+
})
75+
.catch((err) => {
76+
return { error: err.message }
77+
})
78+
if (res !== undefined) {
79+
// console.log(res);
80+
return res;
81+
}
82+
83+
return 'Error';
84+
}
85+
return next()
86+
}, true)
87+
}

tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"rootDir": "src",
4+
"outDir": "lib",
5+
"target": "es2022",
6+
"module": "esnext",
7+
"declaration": true,
8+
"emitDeclarationOnly": true,
9+
"composite": true,
10+
"incremental": true,
11+
"skipLibCheck": true,
12+
"esModuleInterop": true,
13+
"moduleResolution": "bundler",
14+
"jsx": "react-jsx",
15+
"jsxImportSource": "@satorijs/element",
16+
"types": [
17+
"node",
18+
"yml-register/types"
19+
]
20+
},
21+
"include": [
22+
"src"
23+
]
24+
}

0 commit comments

Comments
 (0)