Skip to content

Commit

Permalink
1.5.0-R4:新增md模板与按钮组件的发送
Browse files Browse the repository at this point in the history
  • Loading branch information
Kloping committed Dec 22, 2023
1 parent 1c4f4e2 commit cbb532b
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ import io.github.kloping.qqbot.impl.ListenerHost;

更多使用方式参考查看 [test](./src/test/java)

SDK尚在完善中...
SDK尚在完善中...
15 changes: 15 additions & 0 deletions docs/message.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ source: [Emoji 列表](https://bot.q.qq.com/wiki/develop/api/openapi/emoji/model

</details>

***

### 关于markdown与按钮

```java
event.send(new Markdown("custom_template_id")
//申请的模板 参数填充
.addParam("key", "value")
.setKeyboard("id"));
```

- [md模板申请 与 按钮组件申请](https://q.qq.com/qqbot/#/developer/advanced-features)
- [按钮配置参考](https://bot.q.qq.com/wiki/develop/api-v2/server-inter/message/trans/msg-btn.html#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%8D%8F%E8%AE%AE)
- [md配置参考](https://bot.q.qq.com/wiki/develop/api-v2/server-inter/message/type/markdown.html#%E6%94%AF%E6%8C%81%E6%A0%BC%E5%BC%8F)
> 当前仅支持群聊发送md 按钮组件不能单独发送 须与md消息组合发送
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.kloping</groupId>
<artifactId>bot-qqpd-java</artifactId>
<version>1.5.0-Beta7</version>
<version>1.5.0-R4</version>

<packaging>jar</packaging>
<name>bot-qqpd-java</name>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/github/kloping/qqbot/entities/ex/Keyboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.kloping.qqbot.entities.ex;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
* 无模板 暂未适配自定义按钮
* 按钮不可单独发送
* 需与md一并发送
*
* @author github.kloping
*/
@Data
@AllArgsConstructor
public class Keyboard {
private String id;
}
98 changes: 98 additions & 0 deletions src/main/java/io/github/kloping/qqbot/entities/ex/Markdown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.github.kloping.qqbot.entities.ex;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.sun.istack.internal.Nullable;
import io.github.kloping.qqbot.api.SendAble;
import io.github.kloping.qqbot.api.SenderAndCidMidGetter;
import io.github.kloping.qqbot.entities.ex.enums.EnvType;
import io.github.kloping.qqbot.entities.qqpd.message.RawPreMessage;
import io.github.kloping.qqbot.http.data.Result;
import io.github.kloping.qqbot.http.data.V2MsgData;
import io.github.kloping.qqbot.http.data.V2Result;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.LinkedList;
import java.util.List;

import static io.github.kloping.qqbot.entities.qqpd.Channel.SEND_MESSAGE_HEADERS;

/**
* @author github.kloping
*/
@Getter
public class Markdown implements SendAble {
private String custom_template_id;
private List<Param> params = new LinkedList<>();

/**
* 原生md可用
*/
@Nullable
private String content;

@JSONField(serialize = false, deserialize = false)
private Keyboard keyboard;

/**
* 需要再<a href="https://q.qq.com/qqbot/#/developer/advanced-features">QQ机器人管理平台</a> 申请并审核通过后使用
*
* @param custom_template_id
*/
public Markdown(String custom_template_id) {
this.custom_template_id = custom_template_id;
}

public Markdown addParam(String key, String value) {
params.add(new Param(key, new String[]{value}));
return this;
}

public Markdown setContent(String content) {
this.content = content;
return this;
}

public Markdown setKeyboard(Keyboard keyboard) {
this.keyboard = keyboard;
return this;
}

public Markdown setKeyboard(String id) {
return setKeyboard(new Keyboard(id));
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Param {

public String key;
private String[] values;
}

@Override
public Result<V2Result> send(SenderAndCidMidGetter er) {
return send(er, 1);
}

public Result<V2Result> send(SenderAndCidMidGetter er, Integer msgSeq) {
if (er.getEnvType() == EnvType.GROUP) {
V2MsgData v2MsgData = new V2MsgData();
v2MsgData.setMarkdown(this);
v2MsgData.setMsg_type(2);
v2MsgData.setMsg_id(er.getMid());
if (keyboard != null) v2MsgData.setKeyboard(getKeyboard());
return new Result(er.getBot().groupBaseV2.send(er.getCid(), JSON.toJSONString(v2MsgData), SEND_MESSAGE_HEADERS));
} else if (er.getEnvType() == EnvType.GUILD) {
RawPreMessage preMessage = new RawPreMessage();
preMessage.setMarkdown(this);
preMessage.setMsgId(er.getMid());
return new Result(er.getBot().messageBase.send(er.getCid(), preMessage, SEND_MESSAGE_HEADERS));
}
return er.send(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,37 @@

import io.github.kloping.qqbot.entities.qqpd.data.Emoji;
import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
import lombok.Getter;

/**
* @author github.kloping
*/
public class MessagePreBuilder {
private MessagePre pre = new MessagePre();

@Getter
private boolean empty = true;
public MessagePreBuilder append(String text) {
pre.setContent(pre.getContent() + text);
empty = false;
return this;
}

public MessagePreBuilder append(Image image) {
pre.setImage(image);
empty = false;
return this;
}

public MessagePreBuilder append(At at) {
pre.setContent(pre.getContent() + at);
return this;
return append(at.toString());
}

public MessagePreBuilder append(AtAll at) {
pre.setContent(pre.getContent() + "@everyone");
return this;
return append("@everyone");
}

public MessagePreBuilder append(Emoji emoji) {
pre.setContent(pre.getContent() + emoji.toString0());
return this;
return append(emoji.toString0());
}

public MessagePreBuilder reply(RawMessage message) {
Expand All @@ -45,5 +46,6 @@ public MessagePre build() {

public void clear() {
pre.setContent("");
empty = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public Result send(SenderAndCidMidGetter er) {
if (er.getEnvType() == EnvType.GUILD) {
MessagePreBuilder builder = new MessagePreBuilder();
boolean flag0 = false;
Result result = null;
for (SendAble sendAble : data) {
if (sendAble instanceof PlainText) {
builder.append(sendAble.toString());
Expand All @@ -91,48 +92,53 @@ public Result send(SenderAndCidMidGetter er) {
builder.append((AtAll) sendAble);
} else if (sendAble instanceof Emoji) {
builder.append((Emoji) sendAble);
} else if (sendAble instanceof Markdown) {
result = ((Markdown) sendAble).send(er);
}
}
return er.send(builder.build());
if (!builder.isEmpty()) {
result = er.send(builder.build());
}
return result;
} else {
SenderV2 v2 = (SenderV2) er;
boolean flag0 = false;

V2MsgData data = new V2MsgData();
if (Judge.isNotEmpty(er.getMid())) data.setMsg_id(er.getMid());

Result<V2Result> result = null;
int sent = 0;
for (SendAble e0 : this.data) {
if (e0 instanceof Image) {
Image image = (Image) e0;
if (RawMessage.imagePrepare(image, er.getBot())) continue;
if (!flag0) {
if (image.getFile_type() != 1) {
image.send(er);
sent++;
continue;
}
flag0 = true;
V2Result 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);
result.logFileInfo(er.getBot().logger, image);
data.setMsg_type(7);
data.setMedia(new V2MsgData.Media(result.getFile_info()));
} else {
data.setMsg_seq(v2.getMsgSeq());
v2.getV2().send(er.getCid(), data.toString(), SEND_MESSAGE_HEADERS);
data = new V2MsgData();
if (Judge.isNotEmpty(er.getMid())) data.setMsg_id(er.getMid());
V2Result 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);
result.logFileInfo(er.getBot().logger, image);
data.setMsg_type(7);
data.setMedia(new V2MsgData.Media(result.getFile_info()));
}
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));
result.getData().logFileInfo(er.getBot().logger, image);
data.setMsg_type(7);
data.setMedia(new V2MsgData.Media(result.getData().getFile_info()));
} else if (e0 instanceof Markdown) {
result = ((Markdown) e0).send(er, v2.getMsgSeq());
sent++;
} else {
data.setContent(data.getContent() + e0.toString());
}
}
data.setMsg_seq(v2.getMsgSeq());
return new Result<V2Result>(v2.getV2().send(er.getCid(), data.toString(), SEND_MESSAGE_HEADERS));
if (sent < this.data.size()) {
data.setMsg_seq(v2.getMsgSeq());
return result = new Result<V2Result>(v2.getV2().send(er.getCid(), data.toString(), SEND_MESSAGE_HEADERS));
} else return result;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.kloping.qqbot.entities.qqpd.message;

import com.alibaba.fastjson.annotation.JSONField;
import io.github.kloping.qqbot.entities.ex.Markdown;
import lombok.Data;
import lombok.experimental.Accessors;

Expand All @@ -23,7 +24,7 @@ public class RawPreMessage {
@JSONField(name = "msg_id")
private String msgId;
private String eventId;
private Object markdown;
private Markdown markdown;

public RawPreMessage(String content) {
this.content = content;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/kloping/qqbot/http/MessageBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@HttpClient(Starter.NET_MAIN)
@Headers("io.github.kloping.qqbot.Start0.getHeaders")
public interface MessageBase {
/**
/**
* send a message
*
* @param cid
Expand All @@ -28,7 +28,7 @@ public interface MessageBase {
*/
@PostPath("/channels/{channel_id}/messages")
@Callback("io.github.kloping.qqbot.http.data.ActionResult.doc")
ActionResult send(@PathValue("channel_id") String cid, @RequestBody(type = RequestBody.type.json) RawPreMessage body, @Headers Map<String, String> headers);
ActionResult send(@PathValue("channel_id") String cid, @RequestBody(type = RequestBody.type.json) Object body, @Headers Map<String, String> headers);

/**
* send a message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.kloping.qqbot.http.data;

import com.alibaba.fastjson.JSON;
import io.github.kloping.qqbot.entities.ex.Keyboard;
import io.github.kloping.qqbot.entities.ex.Markdown;
import lombok.Data;
import lombok.Getter;
import lombok.experimental.Accessors;
Expand All @@ -19,7 +21,8 @@ public class V2MsgData {
private Integer msg_type = 0;
private String image = null;
private Media media;

private Markdown markdown;
private Keyboard keyboard;
private String msg_id;
private Integer msg_seq;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public String getClassName() {

@Override
public String toString() {
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.0-Beta7", bot.getConfig().getAppid());
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.0-R4", bot.getConfig().getAppid());
}
}

0 comments on commit cbb532b

Please sign in to comment.