|
1 | 1 | package io.github.kloping.qqbot.entities.ex; |
2 | 2 |
|
3 | | -import io.github.kloping.judge.Judge; |
4 | | -import io.github.kloping.qqbot.api.SendAble; |
5 | | -import io.github.kloping.qqbot.api.SenderAndCidMidGetter; |
6 | | -import io.github.kloping.qqbot.api.SenderV2; |
7 | | -import io.github.kloping.qqbot.entities.ex.enums.EnvType; |
8 | | -import io.github.kloping.qqbot.entities.qqpd.Channel; |
9 | | -import io.github.kloping.qqbot.entities.qqpd.message.RawMessage; |
10 | | -import io.github.kloping.qqbot.http.data.Result; |
11 | | -import io.github.kloping.qqbot.http.data.V2MsgData; |
12 | | -import io.github.kloping.qqbot.http.data.V2Result; |
13 | | -import io.github.kloping.qqbot.impl.MessagePacket; |
14 | | -import lombok.Data; |
15 | 3 | import lombok.experimental.Accessors; |
16 | | -import org.jsoup.helper.HttpConnection; |
17 | | - |
18 | | -import java.io.ByteArrayInputStream; |
19 | | -import java.util.Base64; |
20 | | -import java.util.UUID; |
21 | | - |
22 | | -import static io.github.kloping.qqbot.entities.qqpd.Channel.SEND_FORM_DATA_HEADERS; |
23 | | -import static io.github.kloping.qqbot.entities.qqpd.Channel.SEND_MESSAGE_HEADERS; |
24 | 4 |
|
25 | 5 | /** |
| 6 | + * 图片消息 支持jpg/png |
| 7 | + * <br>发送到频道时url必须备案(少数情况不需要 |
| 8 | + * <br>可使用 bytes发送 |
26 | 9 | * |
27 | | - * 发送到频道时url必须备案(少数情况不需要 |
28 | 10 | * @author github.kloping |
29 | 11 | */ |
30 | | -@Data |
31 | 12 | @Accessors(chain = true) |
32 | | -public class Image implements SendAble { |
33 | | - private String url; |
34 | | - private byte[] bytes; |
35 | | - private String type = "image/jpeg"; |
36 | | - private String name = UUID.randomUUID() + ".jpg"; |
37 | | - |
38 | | - /** |
39 | | - * 仅用于qq群发送 |
40 | | - * <td> 媒体类型:1 图片,2 视频,3 语音,4 文件(暂不开放)<br>资源格式要求<br>图片:png/jpg,视频:mp4,语音:silk</td> |
41 | | - */ |
42 | | - private Integer file_type = 1; |
| 13 | +public class Image extends FileMsg { |
43 | 14 |
|
44 | 15 | public Image(byte[] bytes) { |
45 | | - this.bytes = bytes; |
| 16 | + super(bytes, FileType.IMAGE); |
46 | 17 | } |
47 | 18 |
|
48 | 19 | public Image(String url) { |
49 | | - this.url = url; |
50 | | - } |
51 | | - |
52 | | - public Image(String url, Integer file_type) { |
53 | | - this.url = url; |
54 | | - this.file_type = file_type; |
55 | | - } |
56 | | - |
57 | | - public Image(byte[] bytes, String type) { |
58 | | - this.bytes = bytes; |
59 | | - this.type = type; |
| 20 | + super(url, FileType.IMAGE); |
60 | 21 | } |
61 | 22 |
|
62 | | - public Image(byte[] bytes, String type, String name) { |
63 | | - this.bytes = bytes; |
64 | | - this.type = type; |
| 23 | + public Image(byte[] bytes, String name) { |
| 24 | + super(bytes, FileType.IMAGE); |
65 | 25 | this.name = name; |
66 | 26 | } |
67 | 27 |
|
68 | | - @Override |
69 | | - public Result send(SenderAndCidMidGetter er) { |
70 | | - if (er.getEnvType() == EnvType.GUILD) { |
71 | | - if (getBytes() != null) { |
72 | | - BaseKeyVals keyVals = new BaseKeyVals(); |
73 | | - if (er.getMid() != null) { |
74 | | - HttpConnection.KeyVal v0 = HttpConnection.KeyVal.create("msg_id", er.getMid()); |
75 | | - v0.contentType("text/plain"); |
76 | | - keyVals.add(v0); |
77 | | - } |
78 | | - HttpConnection.KeyVal v1 = HttpConnection.KeyVal.create("file_image", name); |
79 | | - v1.inputStream(new ByteArrayInputStream(bytes)); |
80 | | - v1.contentType(type); |
81 | | - keyVals.add(v1); |
82 | | - return new Result<>(er.getBot().messageBase.send(er.getCid(), SEND_FORM_DATA_HEADERS, keyVals)); |
83 | | - } |
84 | | - MessagePacket packet = new MessagePacket(); |
85 | | - if (Judge.isNotEmpty(getUrl())) packet.setImage(getUrl()); |
86 | | - return er.send(packet); |
87 | | - } else { |
88 | | - SenderV2 v2 = (SenderV2) er; |
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 | | - } |
96 | | - result.logFileInfo(er.getBot().logger, this); |
97 | | - V2MsgData data = new V2MsgData(); |
98 | | - data.setMsg_type(7); |
99 | | - if (Judge.isNotEmpty(er.getMid())) data.setMsg_id(er.getMid()); |
100 | | - data.setMedia(new V2MsgData.Media(result.getFile_info())); |
101 | | - data.setMsg_seq(v2.getMsgSeq()); |
102 | | - return new Result<V2Result>(v2.getV2().send(er.getCid(), data.toString(), SEND_MESSAGE_HEADERS)); |
103 | | - } |
| 28 | + public Image(String url, String name) { |
| 29 | + super(url, FileType.IMAGE); |
| 30 | + this.name = name; |
104 | 31 | } |
105 | 32 | } |
0 commit comments