Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchccc committed Aug 13, 2024
1 parent dc930eb commit 93c1cd3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-api-handler",
"description": "识别以特定前缀开头的消息,并通过POST请求调用API,将API的响应直接作为回复发送。",
"version": "1.0.2",
"version": "1.0.6",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
37 changes: 26 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Context, Schema, Logger } from 'koishi'

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

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

export function apply(ctx: Context, config: Config) {
ctx.middleware(async (session, next) => {
// console.log(config);
console.log(session);
console.log(session.event.message);
console.log(session.content);
let match_prefix = handlePrefixes(session.content, config)

const content = session.content;
const botId = session.selfId;

logger.info('原始消息:' + content);
logger.info('机器人QQ:' + botId);

// 使用正则表达式确保准确匹配特定的提到格式
const mentionRegex = new RegExp(`<at id="${botId}"/>`);
const isMentioned = content && mentionRegex.test(content);
if (!isMentioned) {
return next()
}

const cleanContent = content.replace(/<at id="\d+"\/>/g, '').trim();
logger.info('文本消息:' + cleanContent);
logger.info('群组:' + session.channelId)

let match_prefix = handlePrefixes(cleanContent, config)
if (match_prefix) {
logger.info(match_prefix + ':' + session.content)
logger.info(match_prefix)
const res = await ctx.http.post(config.api, {
token: config.token,
message: session.content,
session: session,
message: cleanContent,
channelId: session.channelId,
})
.catch((err) => {
return { error: err.message }
})
if (res !== undefined) {
// console.log(res);
return res;
}

Expand Down

0 comments on commit 93c1cd3

Please sign in to comment.