Skip to content

Commit 4e02646

Browse files
committed
feat: v2群聊发送图片支持非直链上传
1 parent bccf4a2 commit 4e02646

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.kloping</groupId>
88
<artifactId>bot-qqpd-java</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1-Beta1</version>
1010

1111
<packaging>jar</packaging>
1212
<name>bot-qqpd-java</name>

src/main/java/io/github/kloping/qqbot/entities/ex/Image.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
import org.jsoup.helper.HttpConnection;
1717

1818
import java.io.ByteArrayInputStream;
19+
import java.util.Base64;
1920
import java.util.UUID;
2021

2122
import static io.github.kloping.qqbot.entities.qqpd.Channel.SEND_FORM_DATA_HEADERS;
2223
import static io.github.kloping.qqbot.entities.qqpd.Channel.SEND_MESSAGE_HEADERS;
2324

2425
/**
2526
*
26-
* 发送到q群时必须使用url构造参数
27-
* <br>
2827
* 发送到频道时url必须备案(少数情况不需要
2928
* @author github.kloping
3029
*/
@@ -87,14 +86,20 @@ public Result send(SenderAndCidMidGetter er) {
8786
return er.send(packet);
8887
} else {
8988
SenderV2 v2 = (SenderV2) er;
90-
if (RawMessage.imagePrepare(this, er.getBot())) return null;
91-
V2Result result = v2.getV2().sendFile(er.getCid(), String.format("{\"file_type\": %s,\"url\": \"%s\",\"srv_send_msg\": false}", getFile_type(), getUrl()), Channel.SEND_MESSAGE_HEADERS);
89+
RawMessage.imagePrepare(this, er.getBot());
90+
V2Result result = null;
91+
if (Judge.isNotEmpty(getUrl())) {
92+
result = v2.getV2().sendFile(er.getCid(), String.format("{\"file_type\": %s,\"url\": \"%s\",\"srv_send_msg\": false}", getFile_type(), getUrl()), Channel.SEND_MESSAGE_HEADERS);
93+
} else {
94+
result = v2.getV2().sendFile(er.getCid(), String.format("{\"file_type\": %s,\"file_data\": \"%s\",\"srv_send_msg\": false}", getFile_type(), Base64.getEncoder().encodeToString(bytes)), Channel.SEND_MESSAGE_HEADERS);
95+
}
9296
result.logFileInfo(er.getBot().logger, this);
9397
V2MsgData data = new V2MsgData();
9498
data.setMsg_type(7);
9599
if (Judge.isNotEmpty(er.getMid())) data.setMsg_id(er.getMid());
96100
data.setMedia(new V2MsgData.Media(result.getFile_info()));
97101
data.setMsg_seq(v2.getMsgSeq());
102+
98103
return new Result<V2Result>(v2.getV2().send(er.getCid(), data.toString(), SEND_MESSAGE_HEADERS));
99104
}
100105
}

src/main/java/io/github/kloping/qqbot/entities/ex/msg/MessageChain.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public Result send(SenderAndCidMidGetter er) {
110110
for (SendAble e0 : this.data) {
111111
if (e0 instanceof Image) {
112112
Image image = (Image) e0;
113-
if (RawMessage.imagePrepare(image, er.getBot())) continue;
113+
RawMessage.imagePrepare(image, er.getBot());
114114
if (!flag0) {
115115
if (image.getFile_type() != 1) {
116116
image.send(er);
@@ -124,7 +124,13 @@ public Result send(SenderAndCidMidGetter er) {
124124
data = new V2MsgData();
125125
if (Judge.isNotEmpty(er.getMid())) data.setMsg_id(er.getMid());
126126
}
127-
result = new Result<>(v2.getV2().sendFile(er.getCid(), String.format("{\"file_type\": %s,\"url\": \"%s\",\"srv_send_msg\": false}", image.getFile_type(), image.getUrl()), Channel.SEND_MESSAGE_HEADERS));
127+
if (Judge.isNotEmpty(image.getUrl())) {
128+
result = new Result<>(v2.getV2().sendFile(er.getCid(), String.format("{\"file_type\": %s,\"url\": \"%s\",\"srv_send_msg\": false}",
129+
image.getFile_type(), image.getUrl()), Channel.SEND_MESSAGE_HEADERS));
130+
} else {
131+
result = new Result<>(v2.getV2().sendFile(er.getCid(), String.format("{\"file_type\": %s,\"file_data\": \"%s\",\"srv_send_msg\": false}",
132+
image.getFile_type(), Base64.getEncoder().encodeToString(image.getBytes())), Channel.SEND_MESSAGE_HEADERS));
133+
}
128134
result.getData().logFileInfo(er.getBot().logger, image);
129135
data.setMsg_type(7);
130136
data.setMedia(new V2MsgData.Media(result.getData().getFile_info()));

src/main/java/io/github/kloping/qqbot/entities/qqpd/message/RawMessage.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.kloping.qqbot.entities.qqpd.message;
22

33
import com.alibaba.fastjson.annotation.JSONField;
4+
import io.github.kloping.MySpringTool.PartUtils;
45
import io.github.kloping.judge.Judge;
56
import io.github.kloping.qqbot.api.*;
67
import io.github.kloping.qqbot.api.message.Pinsble;
@@ -74,21 +75,19 @@ public Result send(String text) {
7475
return send(text, this);
7576
}
7677

77-
public static boolean imagePrepare(Image msg, Bot bot) {
78+
public static void imagePrepare(Image msg, Bot bot) {
7879
try {
7980
if (Judge.isEmpty(msg.getUrl())) {
8081
if (msg.getBytes() != null) if (bot.getConfig().getInterceptor0() != null) {
8182
String url = bot.getConfig().getInterceptor0().upload(msg.getBytes());
8283
if (Judge.isNotEmpty(url)) {
8384
msg.setUrl(url);
84-
} else return true;
85+
}
8586
}
8687
}
8788
} catch (Exception e) {
88-
e.printStackTrace();
89-
bot.logger.error(e.getMessage());
89+
bot.logger.error(PartUtils.getExceptionLine(e));
9090
}
91-
return false;
9291
}
9392

9493
@Override

src/main/java/io/github/kloping/qqbot/entities/qqpd/v2/Group.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.alibaba.fastjson.JSON;
44
import com.alibaba.fastjson.JSONObject;
5+
import io.github.kloping.judge.Judge;
56
import io.github.kloping.qqbot.api.SendAble;
67
import io.github.kloping.qqbot.api.SenderV2;
78
import io.github.kloping.qqbot.entities.ex.Image;
@@ -15,6 +16,8 @@
1516
import lombok.Getter;
1617
import lombok.experimental.Accessors;
1718

19+
import java.util.Base64;
20+
1821
import static io.github.kloping.qqbot.entities.qqpd.Channel.SEND_MESSAGE_HEADERS;
1922

2023
/**
@@ -42,8 +45,13 @@ public Result<V2Result> send(String text, RawMessage message) {
4245
}
4346

4447
private V2Result sendImage(Image msg) {
45-
if (RawMessage.imagePrepare(msg, bot)) return null;
46-
V2Result result = bot.groupBaseV2.sendFile(getOpenid(), String.format("{\"file_type\": %s,\"url\": \"%s\",\"srv_send_msg\": false}", msg.getFile_type(), msg.getUrl()), Channel.SEND_MESSAGE_HEADERS);
48+
RawMessage.imagePrepare(msg, bot);
49+
V2Result result = null;
50+
if (Judge.isNotEmpty(msg.getUrl())) {
51+
result = bot.groupBaseV2.sendFile(getOpenid(), String.format("{\"file_type\": %s,\"url\": \"%s\",\"srv_send_msg\": false}", msg.getFile_type(), msg.getUrl()), Channel.SEND_MESSAGE_HEADERS);
52+
} else {
53+
result = bot.groupBaseV2.sendFile(getCid(), String.format("{\"file_type\": %s,\"file_data\": \"%s\",\"srv_send_msg\": false}", msg.getFile_type(), Base64.getEncoder().encodeToString(msg.getBytes())), Channel.SEND_MESSAGE_HEADERS);
54+
}
4755
result.logFileInfo(bot.logger, msg);
4856
V2MsgData data = new V2MsgData();
4957
data.setMedia(new V2MsgData.Media(result.getFile_info()));

src/main/java/io/github/kloping/qqbot/http/GroupBaseV2.java

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public interface GroupBaseV2 extends BaseV2{
2828

2929
/**
3030
* 发送群聊媒体
31+
* <table><thead><tr><th><strong>属性</strong></th> <th><strong>类型</strong></th> <th><strong>必填</strong></th> <th><strong>说明</strong></th></tr></thead> <tbody><tr><td>file_type</td> <td>int</td> <td>是</td> <td>媒体类型:1 图片,2 视频,3 语音,4 文件(暂不开放)<br>资源格式要求<br>图片:png/jpg,视频:mp4,语音:silk</td></tr> <tr><td>url</td> <td>string</td> <td>是</td> <td>需要发送媒体资源的url</td></tr> <tr><td>srv_send_msg</td> <td>bool</td> <td>是</td> <td>设置 true 会直接发送消息到目标端,且会占用<code>主动消息频次</code></td></tr> <tr><td>file_data</td> <td></td> <td>否</td> <td>【暂未支持】</td></tr></tbody></table>
3132
* @param gid
3233
* @param body
3334
* @param headers

src/main/java/io/github/kloping/qqbot/impl/BaseConnectedEvent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public String getClassName() {
4141

4242
@Override
4343
public String toString() {
44-
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.0.", bot.getConfig().getAppid());
44+
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.1-Beta1", bot.getConfig().getAppid());
4545
}
4646
}

0 commit comments

Comments
 (0)