Skip to content

Commit

Permalink
✨ Supported upload private file
Browse files Browse the repository at this point in the history
  • Loading branch information
MisakaTAT committed Jun 23, 2022
1 parent 566b32e commit 76e81df
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
33 changes: 17 additions & 16 deletions src/main/java/com/mikuac/shiro/common/utils/ShiroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,26 @@ public static List<MsgChainBean> stringToMsgChain(String msg) {
/**
* 创建自定义消息合并转发
*
* @param uin 发送者QQ号
* @param name 发送者显示名字
* @param msgList 消息列表,每个元素视为一个消息节点
* <a href="https://docs.go-cqhttp.org/cqcode/#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91">参考文档</a>
* @param uin 发送者QQ号
* @param name 发送者显示名字
* @param contents 消息列表,每个元素视为一个消息节点
* <a href="https://docs.go-cqhttp.org/cqcode/#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91">参考文档</a>
* @return 转发消息
*/
public static List<Map<String, Object>> generateForwardMsg(long uin, String name, List<String> msgList) {
List<Map<String, Object>> nodeList = new ArrayList<>();
msgList.forEach(msg -> {
Map<String, Object> node = new HashMap<>(5);
node.put("type", "node");
Map<String, Object> 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<Map<String, Object>> generateForwardMsg(long uin, String name, List<String> contents) {
val nodes = new ArrayList<Map<String, Object>>();
contents.forEach(msg -> {
val node = new HashMap<String, Object>(16) {{
put("type", "node");
put("data", new HashMap<String, Object>(16) {{
put("name", name);
put("uin", uin);
put("content", msg);
}});
}};
nodes.add(node);
});
return nodeList;
return nodes;
}

}
58 changes: 54 additions & 4 deletions src/main/java/com/mikuac/shiro/core/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,15 @@ public ActionData<DownloadFileResp> downloadFile(String url) {
* <a href="https://docs.go-cqhttp.org/cqcode/#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91">参考文档</a>
* @return {@link ActionRaw}
*/
public ActionRaw sendGroupForwardMsg(long groupId, List<Map<String, Object>> msg) {
public ActionData<MsgId> sendGroupForwardMsg(long groupId, List<Map<String, Object>> 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<ActionData<MsgId>>() {
}.getType()) : null;
}

/**
Expand Down Expand Up @@ -994,14 +995,44 @@ public ActionRaw setBotProfile(String nickname, String company, String email, St
* <a href="https://docs.go-cqhttp.org/cqcode/#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91">参考文档</a>
* @return {@link ActionRaw}
*/
public ActionRaw sendPrivateForwardMsg(long userId, List<Map<String, Object>> msg) {
public ActionData<MsgId> sendPrivateForwardMsg(long userId, List<Map<String, Object>> 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<ActionData<MsgId>>() {
}.getType()) : null;
}

/**
* 发送合并转发
*
* @param event 事件
* @param msg 自定义转发消息 (可使用 ShiroUtils.generateForwardMsg() 方法创建)
* <a href="https://docs.go-cqhttp.org/cqcode/#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91">参考文档</a>
* @return {@link ActionRaw}
*/
public ActionData<MsgId> sendForwardMsg(WholeMessageEvent event, List<Map<String, Object>> 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<ActionData<MsgId>>() {
}.getType()) : null;
}

/**
Expand Down Expand Up @@ -1052,4 +1083,23 @@ public ActionData<OcrResp> 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;
}

}
10 changes: 9 additions & 1 deletion src/main/java/com/mikuac/shiro/enums/ActionPathEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

/**
* 请求路径
Expand Down

0 comments on commit 76e81df

Please sign in to comment.