From eccf5885690ab4b1d67d6e83eeb4f2be84da2b99 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Tue, 19 Mar 2024 12:33:08 +0800 Subject: [PATCH] feat: api /get_group_msg_history --- src/ntqqapi/api/msg.ts | 11 ++++++ src/ntqqapi/ntcall.ts | 2 +- .../action/go-cqhttp/GetGroupMsgHistory.ts | 34 +++++++++++++++++++ src/onebot11/action/index.ts | 2 ++ src/onebot11/action/types.ts | 1 + 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts diff --git a/src/ntqqapi/api/msg.ts b/src/ntqqapi/api/msg.ts index bfca2be..ec79de3 100644 --- a/src/ntqqapi/api/msg.ts +++ b/src/ntqqapi/api/msg.ts @@ -23,6 +23,17 @@ export class NTQQMsgApi { args: [{peer:{peerUid: groupCode, chatType: ChatType.group}, cnt: 20}, null] }) } + static async getMsgHistory(peer: Peer, msgId: string, count: number) { + return await callNTQQApi({ + methodName: NTQQApiMethod.HISTORY_MSG, + args: [{ + peer, + msgId, + cnt: count, + queryOrder: true, + }, null] + }) + } static async fetchRecentContact(){ await callNTQQApi({ methodName: NTQQApiMethod.RECENT_CONTACT, diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 20c35c6..15b86a2 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -17,7 +17,7 @@ export enum NTQQApiClass { export enum NTQQApiMethod { RECENT_CONTACT = "nodeIKernelRecentContactService/fetchAndSubscribeABatchOfRecentContact", ADD_ACTIVE_CHAT = "nodeIKernelMsgService/getAioFirstViewLatestMsgsAndAddActiveChat", // 激活群助手内的聊天窗口,这样才能收到消息 - ADD_ACTIVE_CHAT_2 = "nodeIKernelMsgService/getMsgsIncludeSelfAndAddActiveChat", + HISTORY_MSG = "nodeIKernelMsgService/getMsgsIncludeSelfAndAddActiveChat", LIKE_FRIEND = "nodeIKernelProfileLikeService/setBuddyProfileLike", SELF_INFO = "fetchAuthData", FRIENDS = "nodeIKernelBuddyService/getBuddyList", diff --git a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts new file mode 100644 index 0000000..05ae6b5 --- /dev/null +++ b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts @@ -0,0 +1,34 @@ +import BaseAction from "../BaseAction"; +import {OB11Message, OB11User} from "../../types"; +import {groups} from "../../../common/data"; +import {ActionName} from "../types"; +import {ChatType} from "../../../ntqqapi/types"; +import {dbUtil} from "../../../common/db"; +import {NTQQMsgApi} from "../../../ntqqapi/api/msg"; +import {OB11Constructor} from "../../constructor"; +import {log} from "../../../common/utils"; + + +interface Payload { + group_id: number + message_seq: number +} + +export default class GoCQHTTPGetGroupMsgHistory extends BaseAction { + actionName = ActionName.GoCQHTTP_GetGroupMsgHistory + + protected async _handle(payload: Payload): Promise { + const group = groups.find(group => group.groupCode === payload.group_id.toString()) + if (!group) { + throw `群${payload.group_id}不存在` + } + const startMsgId = (await dbUtil.getMsgByShortId(payload.message_seq))?.msgId || "0" + // log("startMsgId", startMsgId) + let msgList = (await NTQQMsgApi.getMsgHistory({chatType: ChatType.group, peerUid: group.groupCode}, startMsgId, 20)).msgList + await Promise.all(msgList.map(async msg => { + msg.msgShortId = await dbUtil.addMsg(msg) + })) + const ob11MsgList = await Promise.all(msgList.map(msg=>OB11Constructor.message(msg))) + return ob11MsgList + } +} \ No newline at end of file diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index def74d6..8c65113 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -37,6 +37,7 @@ import {GetConfigAction, SetConfigAction} from "./llonebot/Config"; import GetGroupAddRequest from "./llonebot/GetGroupAddRequest"; import SetQQAvatar from './llonebot/SetQQAvatar' import GoCQHTTPDownloadFile from "./go-cqhttp/DownloadFile"; +import GoCQHTTPGetGroupMsgHistory from "./go-cqhttp/GetGroupMsgHistory"; export const actionHandlers = [ new Debug(), @@ -77,6 +78,7 @@ export const actionHandlers = [ new GetGuildList(), new GoCQHTTPMarkMsgAsRead(), new GoCQHTTPUploadGroupFile(), + new GoCQHTTPGetGroupMsgHistory(), ] diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index a00bf13..6ffedc0 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -55,4 +55,5 @@ export enum ActionName { GoCQHTTP_MarkMsgAsRead = "mark_msg_as_read", GoCQHTTP_UploadGroupFile = "upload_group_file", GoCQHTTP_DownloadFile = "download_file", + GoCQHTTP_GetGroupMsgHistory = "get_group_msg_history", } \ No newline at end of file