From 76e81df84c1f3e520c839e535a440d3dfb689244 Mon Sep 17 00:00:00 2001 From: MisakaTAT Date: Thu, 23 Jun 2022 11:02:51 +0800 Subject: [PATCH] :sparkles: Supported upload private file --- .../mikuac/shiro/common/utils/ShiroUtils.java | 33 ++++++----- src/main/java/com/mikuac/shiro/core/Bot.java | 58 +++++++++++++++++-- .../mikuac/shiro/enums/ActionPathEnum.java | 10 +++- 3 files changed, 80 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/mikuac/shiro/common/utils/ShiroUtils.java b/src/main/java/com/mikuac/shiro/common/utils/ShiroUtils.java index 68a381d..94d33ac 100644 --- a/src/main/java/com/mikuac/shiro/common/utils/ShiroUtils.java +++ b/src/main/java/com/mikuac/shiro/common/utils/ShiroUtils.java @@ -200,25 +200,26 @@ public static List stringToMsgChain(String msg) { /** * 创建自定义消息合并转发 * - * @param uin 发送者QQ号 - * @param name 发送者显示名字 - * @param msgList 消息列表,每个元素视为一个消息节点 - * 参考文档 + * @param uin 发送者QQ号 + * @param name 发送者显示名字 + * @param contents 消息列表,每个元素视为一个消息节点 + * 参考文档 * @return 转发消息 */ - public static List> generateForwardMsg(long uin, String name, List msgList) { - List> nodeList = new ArrayList<>(); - msgList.forEach(msg -> { - Map node = new HashMap<>(5); - node.put("type", "node"); - Map data = new HashMap<>(5); - data.put("name", name); - data.put("uin", uin); - data.put("content", msg); - node.put("data", data); - nodeList.add(node); + public static List> generateForwardMsg(long uin, String name, List contents) { + val nodes = new ArrayList>(); + contents.forEach(msg -> { + val node = new HashMap(16) {{ + put("type", "node"); + put("data", new HashMap(16) {{ + put("name", name); + put("uin", uin); + put("content", msg); + }}); + }}; + nodes.add(node); }); - return nodeList; + return nodes; } } \ No newline at end of file diff --git a/src/main/java/com/mikuac/shiro/core/Bot.java b/src/main/java/com/mikuac/shiro/core/Bot.java index ea2fc53..5fe235d 100644 --- a/src/main/java/com/mikuac/shiro/core/Bot.java +++ b/src/main/java/com/mikuac/shiro/core/Bot.java @@ -860,14 +860,15 @@ public ActionData downloadFile(String url) { * 参考文档 * @return {@link ActionRaw} */ - public ActionRaw sendGroupForwardMsg(long groupId, List> msg) { + public ActionData sendGroupForwardMsg(long groupId, List> msg) { val action = ActionPathEnum.SEND_GROUP_FORWARD_MSG; val params = new JSONObject() {{ put("group_id", groupId); put("messages", msg); }}; val result = actionHandler.action(session, action, params); - return result != null ? result.to(ActionRaw.class) : null; + return result != null ? result.to(new TypeReference>() { + }.getType()) : null; } /** @@ -994,14 +995,44 @@ public ActionRaw setBotProfile(String nickname, String company, String email, St * 参考文档 * @return {@link ActionRaw} */ - public ActionRaw sendPrivateForwardMsg(long userId, List> msg) { + public ActionData sendPrivateForwardMsg(long userId, List> msg) { val action = ActionPathEnum.SEND_PRIVATE_FORWARD_MSG; val params = new JSONObject() {{ put("user_id", userId); put("messages", msg); }}; val result = actionHandler.action(session, action, params); - return result != null ? result.to(ActionRaw.class) : null; + return result != null ? result.to(new TypeReference>() { + }.getType()) : null; + } + + /** + * 发送合并转发 + * + * @param event 事件 + * @param msg 自定义转发消息 (可使用 ShiroUtils.generateForwardMsg() 方法创建) + * 参考文档 + * @return {@link ActionRaw} + */ + public ActionData sendForwardMsg(WholeMessageEvent event, List> msg) { + val action = ActionPathEnum.SEND_FORWARD_MSG; + val params = new JSONObject() {{ + put("messages", msg); + }}; + switch (event.getMessageType()) { + case "private": { + params.put("user_id", event.getUserId()); + break; + } + case "group": { + params.put("group_id", event.getGroupId()); + break; + } + default: + } + val result = actionHandler.action(session, action, params); + return result != null ? result.to(new TypeReference>() { + }.getType()) : null; } /** @@ -1052,4 +1083,23 @@ public ActionData ocrImage(String image) { }.getType()) : null; } + /** + * 私聊发送文件 + * + * @param userId 目标用户 + * @param file 本地文件路径 + * @param name 文件名 + * @return {@link ActionRaw} + */ + public ActionRaw uploadPrivateFile(long userId, String file, String name) { + val action = ActionPathEnum.UPLOAD_PRIVATE_FILE; + val params = new JSONObject() {{ + put("user_id", userId); + put("file", file); + put("name", name); + }}; + val result = actionHandler.action(session, action, params); + return result != null ? result.to(ActionRaw.class) : null; + } + } diff --git a/src/main/java/com/mikuac/shiro/enums/ActionPathEnum.java b/src/main/java/com/mikuac/shiro/enums/ActionPathEnum.java index 2759064..118fc98 100644 --- a/src/main/java/com/mikuac/shiro/enums/ActionPathEnum.java +++ b/src/main/java/com/mikuac/shiro/enums/ActionPathEnum.java @@ -219,7 +219,15 @@ public enum ActionPathEnum implements ActionPath { /** * 图片 OCR */ - OCR_IMAGE("ocr_image"); + OCR_IMAGE("ocr_image"), + /** + * 合并转发 + */ + SEND_FORWARD_MSG("send_forward_msg"), + /** + * 上传私聊文件 + */ + UPLOAD_PRIVATE_FILE("upload_private_file"); /** * 请求路径