diff --git a/src/onebot11/actions/ActionTemplate.ts b/src/onebot11/actions/ActionTemplate.ts new file mode 100644 index 0000000..8ecaa68 --- /dev/null +++ b/src/onebot11/actions/ActionTemplate.ts @@ -0,0 +1,26 @@ +// import { OB11Return } from '../types'; +// import BaseAction from "./BaseAction"; + +// export type ActionType = '' + +// export interface PayloadType { +// action: ActionType +// // 参数定义待完善 +// [k: string | number]: any +// } + +// export interface ReturnDataType { +// // 参数定义待完善 +// [k: string | number]: any +// } + + +// class ActionTemplate extends BaseAction { +// static ACTION_TYPE: ActionType = '' + +// async _handle(payload: PayloadType): Promise> { +// return +// } +// } + +// export default ActionTemplate \ No newline at end of file diff --git a/src/onebot11/actions/BaseAction.ts b/src/onebot11/actions/BaseAction.ts new file mode 100644 index 0000000..1d2c5e7 --- /dev/null +++ b/src/onebot11/actions/BaseAction.ts @@ -0,0 +1,25 @@ +import { BaseCheckResult } from "./types" +import { OB11Response } from "./utils" + +class BaseAction { + async check(jsonData: any): Promise { + return { + valid: true, + } + } + + async handle(jsonData: any) { + const result = await this.check(jsonData) + if (!result.valid) { + return OB11Response.error(result.message) + } + const resData = await this._handle(jsonData) + return resData + } + + async _handle(payload: unknown): Promise { + return + } +} + +export default BaseAction \ No newline at end of file diff --git a/src/onebot11/actions/GetFriendList.ts b/src/onebot11/actions/GetFriendList.ts new file mode 100644 index 0000000..49b44da --- /dev/null +++ b/src/onebot11/actions/GetFriendList.ts @@ -0,0 +1,23 @@ +import { OB11Response } from "./utils"; +import { OB11Return, OB11User } from '../types'; +import { OB11Constructor } from "../constructor"; +import { friends } from "../../common/data"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'get_friend_list' + +export interface PayloadType { + action: ActionType +} + +export type ReturnDataType = OB11User[] + +class GetFriendList extends BaseAction { + static ACTION_TYPE: ActionType = 'get_friend_list' + + async _handle(payload: PayloadType): Promise> { + return OB11Response.ok(OB11Constructor.friends(friends)); + } +} + +export default GetFriendList \ No newline at end of file diff --git a/src/onebot11/actions/GetGroupInfo.ts b/src/onebot11/actions/GetGroupInfo.ts new file mode 100644 index 0000000..9ac11be --- /dev/null +++ b/src/onebot11/actions/GetGroupInfo.ts @@ -0,0 +1,30 @@ +import { OB11Response } from "./utils"; +import { OB11Group, OB11Return } from '../types'; +import { getGroup, groups } from "../../common/data"; +import { OB11Constructor } from "../constructor"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'get_group_info' + +export interface PayloadType { + action: ActionType + group_id: number +} + +export type ReturnDataType = OB11Group[] + +class GetGroupInfo extends BaseAction { + static ACTION_TYPE: ActionType = 'get_group_info' + + async _handle(payload: PayloadType): Promise> { + const group = await getGroup(payload.group_id.toString()) + if (group) { + return OB11Response.ok(OB11Constructor.groups(groups)); + } + else { + return OB11Response.error(`群${payload.group_id}不存在`) + } + } +} + +export default GetGroupInfo \ No newline at end of file diff --git a/src/onebot11/actions/GetGroupList.ts b/src/onebot11/actions/GetGroupList.ts new file mode 100644 index 0000000..16eba73 --- /dev/null +++ b/src/onebot11/actions/GetGroupList.ts @@ -0,0 +1,23 @@ +import { OB11Response } from "./utils"; +import { OB11Group, OB11Return } from '../types'; +import { OB11Constructor } from "../constructor"; +import { groups } from "../../common/data"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'get_group_list' + +export interface PayloadType { + action: ActionType +} + +export type ReturnDataType = OB11Group[] + +class GetGroupList extends BaseAction { + static ACTION_TYPE: ActionType = 'get_group_list' + + async _handle(payload: PayloadType): Promise> { + return OB11Response.ok(OB11Constructor.groups(groups)); + } +} + +export default GetGroupList \ No newline at end of file diff --git a/src/onebot11/actions/GetGroupMemberInfo.ts b/src/onebot11/actions/GetGroupMemberInfo.ts new file mode 100644 index 0000000..696ecb9 --- /dev/null +++ b/src/onebot11/actions/GetGroupMemberInfo.ts @@ -0,0 +1,31 @@ +import { OB11Response } from "./utils"; +import { OB11GroupMember, OB11Return } from '../types'; +import { getGroupMember } from "../../common/data"; +import { OB11Constructor } from "../constructor"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'get_group_member_info' + +export interface PayloadType { + action: ActionType + group_id: number + user_id: number +} + +export type ReturnDataType = OB11GroupMember + +class GetGroupMemberInfo extends BaseAction { + static ACTION_TYPE: ActionType = 'get_group_member_info' + + async _handle(payload: PayloadType): Promise> { + const member = await getGroupMember(payload.group_id.toString(), payload.user_id.toString()) + if (member) { + return OB11Response.ok(OB11Constructor.groupMember(payload.group_id.toString(), member)) + } + else { + return OB11Response.error(`群成员${payload.user_id}不存在`) + } + } +} + +export default GetGroupMemberInfo \ No newline at end of file diff --git a/src/onebot11/actions/GetGroupMemberList.ts b/src/onebot11/actions/GetGroupMemberList.ts new file mode 100644 index 0000000..04abedc --- /dev/null +++ b/src/onebot11/actions/GetGroupMemberList.ts @@ -0,0 +1,34 @@ +import { OB11Response } from "./utils"; +import { OB11GroupMember, OB11Return } from '../types'; +import { getGroup } from "../../common/data"; +import { NTQQApi } from "../../ntqqapi/ntcall"; +import { OB11Constructor } from "../constructor"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'get_group_member_list' + +export interface PayloadType { + action: ActionType + group_id: number +} + +export type ReturnDataType = OB11GroupMember[] + +class GetGroupMemberList extends BaseAction { + static ACTION_TYPE: ActionType = 'get_group_member_list' + + async _handle(payload: PayloadType): Promise> { + const group = await getGroup(payload.group_id.toString()); + if (group) { + if (!group.members?.length) { + group.members = await NTQQApi.getGroupMembers(payload.group_id.toString()) + } + return OB11Response.ok(OB11Constructor.groupMembers(group)); + } + else { + return OB11Response.error(`群${payload.group_id}不存在`) + } + } +} + +export default GetGroupMemberList \ No newline at end of file diff --git a/src/onebot11/actions/GetLoginInfo.ts b/src/onebot11/actions/GetLoginInfo.ts new file mode 100644 index 0000000..6303bb6 --- /dev/null +++ b/src/onebot11/actions/GetLoginInfo.ts @@ -0,0 +1,23 @@ +import { OB11Response } from "./utils"; +import { OB11Return, OB11User } from '../types'; +import { OB11Constructor } from "../constructor"; +import { selfInfo } from "../../common/data"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'get_login_info' + +export interface PayloadType { + action: ActionType +} + +export type ReturnDataType = OB11User + +class GetLoginInfo extends BaseAction { + static ACTION_TYPE: ActionType = 'get_login_info' + + async _handle(payload: PayloadType): Promise> { + return OB11Response.ok(OB11Constructor.selfInfo(selfInfo)); + } +} + +export default GetLoginInfo \ No newline at end of file diff --git a/src/onebot11/actions/GetMsg.ts b/src/onebot11/actions/GetMsg.ts new file mode 100644 index 0000000..ab78177 --- /dev/null +++ b/src/onebot11/actions/GetMsg.ts @@ -0,0 +1,32 @@ +import { OB11Response } from "./utils"; +import { msgHistory } from "../../common/data"; +import { OB11Message, OB11Return } from '../types'; +import { OB11Constructor } from "../constructor"; +import { log } from "../../common/utils"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'get_msg' + +export interface PayloadType { + action: ActionType + message_id: string +} + +export type ReturnDataType = OB11Message + +class GetMsg extends BaseAction { + static ACTION_TYPE: ActionType = 'get_msg' + + async _handle(payload: PayloadType): Promise> { + log("history msg ids", Object.keys(msgHistory)); + const msg = msgHistory[payload.message_id.toString()] + if (msg) { + const msgData = await OB11Constructor.message(msg); + return OB11Response.ok(msgData) + } else { + return OB11Response.error("消息不存在") + } + } +} + +export default GetMsg \ No newline at end of file diff --git a/src/onebot11/actions/SendGroupMsg.ts b/src/onebot11/actions/SendGroupMsg.ts new file mode 100644 index 0000000..3b8919b --- /dev/null +++ b/src/onebot11/actions/SendGroupMsg.ts @@ -0,0 +1,24 @@ +import { OB11PostSendMsg, OB11Return } from '../types'; +import SendMsg from "./SendMsg"; +import BaseAction from './BaseAction'; + +export type ActionType = 'send_group_msg' + +export interface PayloadType extends OB11PostSendMsg { + action: ActionType +} + +export interface ReturnDataType { + message_id: string +} + +class SendGroupMsg extends BaseAction { + static ACTION_TYPE: ActionType = 'send_group_msg' + + async _handle(payload: PayloadType): Promise> { + // 偷懒借用现有逻辑 + return new SendMsg()._handle(payload as any) + } +} + +export default SendGroupMsg \ No newline at end of file diff --git a/src/onebot11/actions/SendMsg.ts b/src/onebot11/actions/SendMsg.ts new file mode 100644 index 0000000..981a581 --- /dev/null +++ b/src/onebot11/actions/SendMsg.ts @@ -0,0 +1,133 @@ +import { AtType, ChatType, Group } from "../../ntqqapi/types"; +import { friends, getGroup, getStrangerByUin, msgHistory } from "../../common/data"; +import { OB11Return, OB11MessageData, OB11MessageDataType, OB11PostSendMsg } from '../types'; +import { NTQQApi } from "../../ntqqapi/ntcall"; +import { Peer } from "../../ntqqapi/ntcall"; +import { SendMessageElement } from "../../ntqqapi/types"; +import { SendMsgElementConstructor } from "../../ntqqapi/constructor"; +import { uri2local } from "../utils"; +import { OB11Response } from "./utils"; +import { v4 as uuid4 } from 'uuid'; +import { log } from "../../common/utils"; +import BaseAction from "./BaseAction"; + +export type ActionType = 'send_msg' + +export interface PayloadType extends OB11PostSendMsg { + action: ActionType +} + +export interface ReturnDataType { + message_id: string +} + +class SendMsg extends BaseAction { + static ACTION_TYPE: ActionType = 'send_msg' + + async _handle(payload: PayloadType): Promise> { + const peer: Peer = { + chatType: ChatType.friend, + peerUid: "" + } + let group: Group | undefined = undefined; + if (payload?.group_id) { + group = await getGroup(payload.group_id.toString()) + if (!group) { + return OB11Response.error(`群${payload.group_id}不存在`) + } + peer.chatType = ChatType.group + // peer.name = group.name + peer.peerUid = group.groupCode + } + else if (payload?.user_id) { + const friend = friends.find(f => f.uin == payload.user_id.toString()) + if (friend) { + // peer.name = friend.nickName + peer.peerUid = friend.uid + } + else { + peer.chatType = ChatType.temp + const tempUser = getStrangerByUin(payload.user_id.toString()) + if (!tempUser) { + return OB11Response.error(`找不到私聊对象${payload.user_id}`) + } + // peer.name = tempUser.nickName + peer.peerUid = tempUser.uid + } + } + if (typeof payload.message === "string") { + payload.message = [{ + type: OB11MessageDataType.text, + data: { + text: payload.message + } + }] as OB11MessageData[] + } + else if (!Array.isArray(payload.message)) { + payload.message = [payload.message] + } + const sendElements: SendMessageElement[] = [] + for (let sendMsg of payload.message) { + switch (sendMsg.type) { + case OB11MessageDataType.text: { + const text = sendMsg.data?.text; + if (text) { + sendElements.push(SendMsgElementConstructor.text(sendMsg.data!.text)) + } + } break; + case OB11MessageDataType.at: { + let atQQ = sendMsg.data?.qq; + if (atQQ) { + atQQ = atQQ.toString() + if (atQQ === "all") { + sendElements.push(SendMsgElementConstructor.at(atQQ, atQQ, AtType.atAll, "全体成员")) + } + else { + const atMember = group?.members.find(m => m.uin == atQQ) + if (atMember) { + sendElements.push(SendMsgElementConstructor.at(atQQ, atMember.uid, AtType.atUser, atMember.cardName || atMember.nick)) + } + } + } + } break; + case OB11MessageDataType.reply: { + let replyMsgId = sendMsg.data.id; + if (replyMsgId) { + replyMsgId = replyMsgId.toString() + const replyMsg = msgHistory[replyMsgId] + if (replyMsg) { + sendElements.push(SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsgId, replyMsg.senderUin, replyMsg.senderUin)) + } + } + } break; + case OB11MessageDataType.image: { + const file = sendMsg.data?.file + if (file) { + const picPath = await (await uri2local(uuid4(), file)).path + if (picPath) { + sendElements.push(await SendMsgElementConstructor.pic(picPath)) + } + } + } break; + case OB11MessageDataType.voice: { + const file = sendMsg.data?.file + if (file) { + const voicePath = await (await uri2local(uuid4(), file)).path + if (voicePath) { + sendElements.push(await SendMsgElementConstructor.ptt(voicePath)) + } + } + } + } + } + log("send msg:", peer, sendElements) + try { + const returnMsg = await NTQQApi.sendMsg(peer, sendElements) + return OB11Response.ok({ message_id: returnMsg.msgId }) + } catch (e) { + return OB11Response.error(e.toString()) + } + } +} + +export default SendMsg \ No newline at end of file diff --git a/src/onebot11/actions/SendPrivateMsg.ts b/src/onebot11/actions/SendPrivateMsg.ts new file mode 100644 index 0000000..93c5af6 --- /dev/null +++ b/src/onebot11/actions/SendPrivateMsg.ts @@ -0,0 +1,24 @@ +import { OB11PostSendMsg, OB11Return } from '../types'; +import SendMsg from "./SendMsg"; +import BaseAction from './BaseAction'; + +export type ActionType = 'send_private_msg' + +export interface PayloadType extends OB11PostSendMsg { + action: ActionType +} + +export interface ReturnDataType { + message_id: string +} + +class SendPrivateMsg extends BaseAction { + static ACTION_TYPE: ActionType = 'send_private_msg' + + async _handle(payload: PayloadType): Promise> { + // 偷懒借用现有逻辑 + return new SendMsg()._handle(payload as any) + } +} + +export default SendPrivateMsg \ No newline at end of file diff --git a/src/onebot11/actions/index.ts b/src/onebot11/actions/index.ts new file mode 100644 index 0000000..590a545 --- /dev/null +++ b/src/onebot11/actions/index.ts @@ -0,0 +1,37 @@ +import { OB11Return } from '../types'; +import { OB11Response } from './utils' + +import GetMsg from './GetMsg' +import GetLoginInfo from './GetLoginInfo' +import GetFriendList from './GetFriendList' +import GetGroupList from './GetGroupList' +import GetGroupInfo from './GetGroupInfo' +import GetGroupMemberList from './GetGroupMemberList' +import GetGroupMemberInfo from './GetGroupMemberInfo' +import SendGroupMsg from './SendGroupMsg' +import SendPrivateMsg from './SendPrivateMsg' +import SendMsg from './SendMsg' + +export const actionHandles = { + [GetMsg.ACTION_TYPE]: new GetMsg(), + [GetLoginInfo.ACTION_TYPE]: new GetLoginInfo(), + [GetFriendList.ACTION_TYPE]: new GetFriendList(), + [GetGroupList.ACTION_TYPE]: new GetGroupList(), + [GetGroupInfo.ACTION_TYPE]: new GetGroupInfo(), + [GetGroupMemberList.ACTION_TYPE]: new GetGroupMemberList(), + [GetGroupMemberInfo.ACTION_TYPE]: new GetGroupMemberInfo(), + [SendGroupMsg.ACTION_TYPE]: new SendGroupMsg(), + [SendPrivateMsg.ACTION_TYPE]: new SendPrivateMsg(), + [SendMsg.ACTION_TYPE]: new SendMsg(), +} + +export async function handleAction( + jsonData: any, +): Promise> { + const handler = actionHandles[jsonData.action] + if (handler) { + return await handler.handle(jsonData) + } else { + return OB11Response.error(`未知的 action: ${jsonData.action}`) + } +} diff --git a/src/onebot11/actions/types.ts b/src/onebot11/actions/types.ts new file mode 100644 index 0000000..34b63e3 --- /dev/null +++ b/src/onebot11/actions/types.ts @@ -0,0 +1,12 @@ +export type BaseCheckResult = ValidCheckResult | InvalidCheckResult + +export interface ValidCheckResult { + valid: true + [k: string | number]: any +} + +export interface InvalidCheckResult { + valid: false + message: string + [k: string | number]: any +} diff --git a/src/onebot11/actions/utils.ts b/src/onebot11/actions/utils.ts new file mode 100644 index 0000000..20e7e7f --- /dev/null +++ b/src/onebot11/actions/utils.ts @@ -0,0 +1,61 @@ +import { OB11Return } from '../types'; + +// export function createSuccessActionResponse({ +// status = 0, +// retcode = 0, +// data = {} as T, +// ...others +// }: Partial> = {} = {}): OB11Return { +// return { +// status, +// retcode, +// data, +// message: '', +// ...others +// } +// } + +// export function createFailedActionResponse({ +// status = -1, +// retcode = -1, +// message = '', +// ...others +// }: Partial> = {}): OB11Return { +// return { +// status, +// retcode, +// data: {}, +// message, +// ...others, +// } +// } + +// export function createActionParamsErrorResponse(type, others = {}) { +// return createFailedActionResponse({ +// message: `${type} 接收到了不正确的参数`, +// ...others, +// }) +// } + +// export function createErrorResponseWithParams(params = {}) { +// return createFailedActionResponse({ +// ...params, +// }) +// } + +export class OB11Response { + static res(data: T, status: number = 0, message: string = ""): OB11Return { + return { + status: status, + retcode: status, + data: data, + message: message + } + } + static ok(data: T) { + return OB11Response.res(data) + } + static error(err: string) { + return OB11Response.res(null, -1, err) + } +} diff --git a/src/onebot11/server.ts b/src/onebot11/server.ts index ec178e5..d63ae03 100644 --- a/src/onebot11/server.ts +++ b/src/onebot11/server.ts @@ -5,16 +5,9 @@ import { Request } from 'express'; import { Response } from 'express'; const JSONbig = require('json-bigint')({ storeAsString: true }); -import { AtType, ChatType, Group, SelfInfo } from "../ntqqapi/types"; -import { friends, getGroup, getGroupMember, getStrangerByUin, groups, msgHistory, selfInfo } from "../common/data"; -import { OB11ApiName, OB11Message, OB11Return, OB11MessageData, OB11Group, OB11GroupMember, OB11PostSendMsg, OB11MessageDataType, OB11User } from './types'; -import { OB11Constructor } from "./constructor"; -import { NTQQApi } from "../ntqqapi/ntcall"; -import { Peer } from "../ntqqapi/ntcall"; -import { SendMessageElement } from "../ntqqapi/types"; -import { SendMsgElementConstructor } from "../ntqqapi/constructor"; -import { uri2local } from "./utils"; -import { v4 as uuid4 } from 'uuid'; +import { selfInfo } from "../common/data"; +import { OB11Message, OB11Return, OB11MessageData } from './types'; +import { actionHandles } from "./actions"; // @SiberianHusky 2021-08-15 @@ -137,12 +130,12 @@ export function postMsg(msg: OB11Message) { let routers: Record Promise>> = {}; -function registerRouter(action: OB11ApiName, handle: (payload: PayloadType) => Promise>) { +function registerRouter(action: string, handle: (payload: any) => Promise) { let url = action.toString() if (!action.startsWith("/")) { url = "/" + action } - async function _handle(res: Response, payload: PayloadType) { + async function _handle(res: Response, payload: any) { log("receive post data", url, payload) try { const result = await handle(payload) @@ -163,169 +156,6 @@ function registerRouter(action: OB11ApiName, handle routers[url] = handle } -registerRouter<{ message_id: string }, OB11Message>("get_msg", async (payload) => { - log("history msg ids", Object.keys(msgHistory)); - const msg = msgHistory[payload.message_id.toString()] - if (msg) { - const msgData = await OB11Constructor.message(msg); - return OB11Response.ok(msgData) - } else { - return OB11Response.error("消息不存在") - } -}) - -registerRouter<{}, OB11User>("get_login_info", async (payload) => { - return OB11Response.ok(OB11Constructor.selfInfo(selfInfo)); -}) - -registerRouter<{}, OB11User[]>("get_friend_list", async (payload) => { - return OB11Response.ok(OB11Constructor.friends(friends)); -}) - -registerRouter<{}, OB11Group[]>("get_group_list", async (payload) => { - return OB11Response.ok(OB11Constructor.groups(groups)); -}) - - -registerRouter<{ group_id: number }, OB11Group[]>("get_group_info", async (payload) => { - const group = await getGroup(payload.group_id.toString()) - if (group) { - return OB11Response.ok(OB11Constructor.groups(groups)); - } - else { - return OB11Response.error(`群${payload.group_id}不存在`) - } -}) - -registerRouter<{ group_id: number }, OB11GroupMember[]>("get_group_member_list", async (payload) => { - - const group = await getGroup(payload.group_id.toString()); - if (group) { - if (!group.members?.length){ - group.members = await NTQQApi.getGroupMembers(payload.group_id.toString()) - } - return OB11Response.ok(OB11Constructor.groupMembers(group)); - } - else { - return OB11Response.error(`群${payload.group_id}不存在`) - } -}) - -registerRouter<{ group_id: number, user_id: number }, OB11GroupMember>("get_group_member_info", async (payload) => { - const member = await getGroupMember(payload.group_id.toString(), payload.user_id.toString()) - if (member) { - return OB11Response.ok(OB11Constructor.groupMember(payload.group_id.toString(), member)) - } - else { - return OB11Response.error(`群成员${payload.user_id}不存在`) - } -}) - -const handleSendMsg = async (payload) => { - const peer: Peer = { - chatType: ChatType.friend, - peerUid: "" - } - let group: Group | undefined = undefined; - if (payload?.group_id) { - group = await getGroup(payload.group_id.toString()) - if (!group) { - return OB11Response.error(`群${payload.group_id}不存在`) - } - peer.chatType = ChatType.group - // peer.name = group.name - peer.peerUid = group.groupCode - } - else if (payload?.user_id) { - const friend = friends.find(f => f.uin == payload.user_id.toString()) - if (friend) { - // peer.name = friend.nickName - peer.peerUid = friend.uid - } - else { - peer.chatType = ChatType.temp - const tempUser = getStrangerByUin(payload.user_id.toString()) - if (!tempUser) { - return OB11Response.error(`找不到私聊对象${payload.user_id}`) - } - // peer.name = tempUser.nickName - peer.peerUid = tempUser.uid - } - } - if (typeof payload.message === "string") { - payload.message = [{ - type: OB11MessageDataType.text, - data: { - text: payload.message - } - }] as OB11MessageData[] - } - else if (!Array.isArray(payload.message)) { - payload.message = [payload.message] - } - const sendElements: SendMessageElement[] = [] - for (let sendMsg of payload.message) { - switch (sendMsg.type) { - case OB11MessageDataType.text: { - const text = sendMsg.data?.text; - if (text) { - sendElements.push(SendMsgElementConstructor.text(sendMsg.data!.text)) - } - } break; - case OB11MessageDataType.at: { - let atQQ = sendMsg.data?.qq; - if (atQQ) { - atQQ = atQQ.toString() - if (atQQ === "all") { - sendElements.push(SendMsgElementConstructor.at(atQQ, atQQ, AtType.atAll, "全体成员")) - } - else { - const atMember = group?.members.find(m => m.uin == atQQ) - if (atMember) { - sendElements.push(SendMsgElementConstructor.at(atQQ, atMember.uid, AtType.atUser, atMember.cardName || atMember.nick)) - } - } - } - } break; - case OB11MessageDataType.reply: { - let replyMsgId = sendMsg.data.id; - if (replyMsgId) { - replyMsgId = replyMsgId.toString() - const replyMsg = msgHistory[replyMsgId] - if (replyMsg) { - sendElements.push(SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsgId, replyMsg.senderUin, replyMsg.senderUin)) - } - } - } break; - case OB11MessageDataType.image: { - const file = sendMsg.data?.file - if (file) { - const picPath = await (await uri2local(uuid4(), file)).path - if (picPath) { - sendElements.push(await SendMsgElementConstructor.pic(picPath)) - } - } - } break; - case OB11MessageDataType.voice: { - const file = sendMsg.data?.file - if (file) { - const voicePath = await (await uri2local(uuid4(), file)).path - if (voicePath) { - sendElements.push(await SendMsgElementConstructor.ptt(voicePath)) - } - } - } - } - } - log("send msg:", peer, sendElements) - try { - const returnMsg = await NTQQApi.sendMsg(peer, sendElements) - return OB11Response.ok({ message_id: returnMsg.msgId }) - } catch (e) { - return OB11Response.error(e.toString()) - } +for (const [action, handler] of Object.entries(actionHandles)) { + registerRouter(action, (payload) => handler.handle(payload)) } - -registerRouter("send_msg", handleSendMsg) -registerRouter("send_private_msg", handleSendMsg) -registerRouter("send_group_msg", handleSendMsg) \ No newline at end of file