Skip to content

Commit 1c677e1

Browse files
committed
1.5.0,支持单独发送自定义按钮;新增INTERACTION_CREATE按钮回调事件;feat文档
1 parent a4b16b0 commit 1c677e1

19 files changed

+590
-35
lines changed

docs/imgs/img.png

89.6 KB
Loading

docs/imgs/img_1.png

145 KB
Loading

docs/imgs/img_2.png

6.66 KB
Loading

docs/message.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ event.send(new Markdown("custom_template_id")
8686
- [md模板申请 与 按钮组件申请](https://q.qq.com/qqbot/#/developer/advanced-features)
8787
- [按钮配置参考](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)
8888
- [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)
89-
> 当前仅支持群聊发送md 按钮组件不能单独发送 须与md消息组合发送
89+
> 当前仅支持群聊发送md 按钮组件不能单独发送 须与md消息组合发送
90+
91+
### 按钮发送
92+
> 按钮发送说明 24/3/20 v1.5.0
93+
![img_1.png](./imgs/img_1.png)
94+
> 效果如
95+
![img_2.png](./imgs/img_2.png)

docs/readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,5 @@ starter.APPLICATION.INSTANCE.getContextManager().getContextEntity(Start0.class).
9797
starter.getBot().groupBaseV2.send("8468B15808B8200A56E6DD92EBA51AAC", data.toString(), SEND_MESSAGE_HEADERS);
9898
}
9999
});
100+
![img.png](./imgs/img.png)
101+

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-RC</version>
9+
<version>1.5.0</version>
1010

1111
<packaging>jar</packaging>
1212
<name>bot-qqpd-java</name>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.kloping.qqbot.api.event;
2+
3+
import io.github.kloping.qqbot.api.Sender;
4+
import io.github.kloping.qqbot.entities.qqpd.InterAction;
5+
6+
/**
7+
* @author github.kloping
8+
*/
9+
public interface InterActionEvent extends Event, Sender {
10+
/**
11+
* 0 频道场景,1 群聊场景,2 单聊场景
12+
*
13+
* @return
14+
*/
15+
Integer getChatType();
16+
17+
/**
18+
* 获得数据
19+
*
20+
* @return
21+
*/
22+
InterAction getInterAction();
23+
24+
/**
25+
* 响应
26+
* 0 成功 1 操作失败 2 操作频繁 3 重复操作 4 没有权限 5 仅管理员操作 <br>
27+
* 测试未知异常 暂不可用
28+
*
29+
* @param code
30+
*/
31+
void response(int code);
32+
33+
@Override
34+
default String getClassName() {
35+
return "InterActionEvent";
36+
}
37+
}

src/main/java/io/github/kloping/qqbot/entities/Bot.java

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class Bot {
2121
@AutoStand
2222
public Logger logger;
2323

24+
@AutoStand
25+
public InterActionBase interActionBase;
26+
2427
@AutoStand
2528
public GuildBase guildBase;
2629

Original file line numberDiff line numberDiff line change
@@ -1,17 +1,276 @@
11
package io.github.kloping.qqbot.entities.ex;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONArray;
5+
import com.alibaba.fastjson.JSONObject;
6+
import io.github.kloping.qqbot.api.SendAble;
7+
import io.github.kloping.qqbot.api.SenderAndCidMidGetter;
8+
import io.github.kloping.qqbot.entities.ex.enums.EnvType;
9+
import io.github.kloping.qqbot.entities.qqpd.message.RawPreMessage;
10+
import io.github.kloping.qqbot.http.data.Result;
11+
import io.github.kloping.qqbot.http.data.V2MsgData;
12+
import lombok.Getter;
13+
14+
import java.util.LinkedList;
15+
import java.util.List;
16+
17+
import static io.github.kloping.qqbot.entities.qqpd.Channel.SEND_MESSAGE_HEADERS;
518

619
/**
7-
* 无模板 暂未适配自定义按钮
8-
* 按钮不可单独发送
9-
* 需与md一并发送
20+
* 按钮可单独发送 <br>
21+
* 但与md一并发送时必须使用模板
22+
* <br>
23+
* <table><thead><tr><th><strong>属性</strong></th> <th><strong>类型</strong></th> <th><strong>必填</strong></th> <th><strong>说明</strong></th></tr></thead> <tbody><tr><td>id</td> <td>string</td> <td>否</td> <td>按钮ID:在一个keyboard消息内设置唯一</td></tr> <tr><td>render_data.label</td> <td>string</td> <td>是</td> <td>按钮上的文字</td></tr> <tr><td>render_data.visited_label</td> <td>string</td> <td>是</td> <td>点击后按钮的上文字</td></tr> <tr><td>render_data.style</td> <td>int</td> <td>是</td> <td>按钮样式:0 灰色线框,1 蓝色线框</td></tr> <tr><td>action.type</td> <td>int</td> <td>是</td> <td>设置 0 跳转按钮:http 或 小程序 客户端识别 scheme,设置 1 回调按钮:回调后台接口, data 传给后台,设置 2 指令按钮:自动在输入框插入 @bot data</td></tr> <tr><td>action.permisson.type</td> <td>int</td> <td>是</td> <td>0 指定用户可操作,1 仅管理者可操作,2 所有人可操作,3 指定身份组可操作(仅频道可用)</td></tr> <tr><td>action.permisson.specify_user_ids</td> <td>array</td> <td>否</td> <td>有权限的用户 id 的列表</td></tr> <tr><td>action.permisson.specify_role_ids</td> <td>array</td> <td>否</td> <td>有权限的身份组 id 的列表(仅频道可用)</td></tr> <tr><td>action.data</td> <td>string</td> <td>是</td> <td>操作相关的数据</td></tr> <tr><td>action.reply</td> <td>bool</td> <td>否</td> <td>指令按钮可用,指令是否带引用回复本消息,默认 false。支持版本 8983</td></tr> <tr><td>action.enter</td> <td>bool</td> <td>否</td> <td>指令按钮可用,点击按钮后直接自动发送 data,默认 false。支持版本 8983</td></tr> <tr><td>action.anchor</td> <td>int</td> <td>否</td> <td>本字段仅在指令按钮下有效,设置后后会忽略 action.enter 配置。<br>设置为 1 时 ,点击按钮自动唤起启手Q选图器,其他值暂无效果。<br>(仅支持手机端版本 8983+ 的单聊场景,桌面端不支持)</td></tr> <tr><td>action.click_limit</td> <td>int</td> <td>否</td> <td>【已弃用】可操作点击的次数,默认不限</td></tr> <tr><td>action.at_bot_show_channel_list</td> <td>bool</td> <td>否</td> <td>【已弃用】指令按钮可用,弹出子频道选择器,默认 false</td></tr> <tr><td>action.unsupport_tips</td> <td>string</td> <td>是</td> <td>客户端不支持本action的时候,弹出的toast文案</td></tr></tbody></table>
24+
*
1025
*
1126
* @author github.kloping
1227
*/
13-
@Data
14-
@AllArgsConstructor
15-
public class Keyboard {
16-
private String id;
28+
public class Keyboard implements SendAble {
29+
@Getter
30+
private String id = null;
31+
32+
/**
33+
* 模板创建方式必须与markdown搭配
34+
* {@link Markdown#setKeyboard}
35+
*
36+
* @param id
37+
*/
38+
public Keyboard(String id) {
39+
this.id = id;
40+
}
41+
42+
private Keyboard() {
43+
}
44+
45+
@Getter
46+
private JSONObject content;
47+
48+
@Override
49+
public Result send(SenderAndCidMidGetter er) {
50+
if (id != null) {
51+
er.getBot().logger.error("keyboard id is not null.");
52+
return null;
53+
}
54+
if (er.getEnvType() == EnvType.GROUP) {
55+
V2MsgData v2MsgData = new V2MsgData();
56+
v2MsgData.setMsg_type(2);
57+
v2MsgData.setKeyboard(this);
58+
v2MsgData.setMsg_id(er.getMid());
59+
String d0 = JSON.toJSONString(v2MsgData);
60+
return new Result(er.getBot().groupBaseV2.send(er.getCid(), d0, SEND_MESSAGE_HEADERS));
61+
} else if (er.getEnvType() == EnvType.GUILD) {
62+
RawPreMessage preMessage = new RawPreMessage();
63+
preMessage.setMsgId(er.getMid());
64+
return new Result(er.getBot().messageBase.send(er.getCid(), preMessage, SEND_MESSAGE_HEADERS));
65+
}
66+
return er.send(this);
67+
}
68+
69+
public static class KeyboardBuilder {
70+
public static KeyboardBuilder create() {
71+
return new KeyboardBuilder();
72+
}
73+
74+
private Integer id = 1;
75+
76+
private Integer requestIdIndex() {
77+
return id++;
78+
}
79+
80+
public RowBuilder addRow() {
81+
return new RowBuilder(this);
82+
}
83+
84+
private List list = new LinkedList();
85+
86+
public Keyboard build() {
87+
Keyboard keyboard = new Keyboard();
88+
JSONObject rows = new JSONObject();
89+
rows.put("rows", JSONArray.parseArray(JSON.toJSONString(list)));
90+
keyboard.content = rows;
91+
return keyboard;
92+
}
93+
}
94+
95+
public static class RowBuilder {
96+
private KeyboardBuilder builder;
97+
98+
public RowBuilder(KeyboardBuilder builder) {
99+
this.builder = builder;
100+
}
101+
102+
private Buttons buttons = new Buttons();
103+
104+
public ButtonBuilder addButton() {
105+
return new ButtonBuilder(this);
106+
}
107+
108+
public RowBuilder addButton(Button button) {
109+
buttons.buttons.add(button);
110+
return this;
111+
}
112+
113+
public KeyboardBuilder build() {
114+
builder.list.add(buttons);
115+
return builder;
116+
}
117+
}
118+
119+
public static class ButtonBuilder {
120+
private RowBuilder builder;
121+
private Button button;
122+
123+
public ButtonBuilder(RowBuilder builder) {
124+
this.builder = builder;
125+
button = new Button();
126+
button.id = builder.builder.requestIdIndex().toString();
127+
}
128+
129+
/**
130+
* label – 按钮上的文字
131+
*
132+
* @param text
133+
* @return
134+
*/
135+
public ButtonBuilder setLabel(String text) {
136+
button.render_data.label = text;
137+
return this;
138+
}
139+
140+
/**
141+
* visited_label – 按钮上的文字
142+
*
143+
* @param text
144+
* @return
145+
*/
146+
public ButtonBuilder setVisitedLabel(String text) {
147+
button.render_data.visited_label = text;
148+
return this;
149+
}
150+
151+
/**
152+
* render_data.style int 是 按钮样式:0 灰色线框,1 蓝色线框
153+
*
154+
* @param style
155+
* @return
156+
*/
157+
public ButtonBuilder setStyle(Integer style) {
158+
button.render_data.style = style;
159+
return this;
160+
}
161+
162+
/**
163+
* @param data 操作相关的数据
164+
* @return
165+
*/
166+
public ButtonBuilder setActionData(String data) {
167+
button.action.data = data;
168+
return this;
169+
}
170+
171+
/**
172+
* @param type 设置 0 跳转按钮:http 或 小程序 客户端识别 scheme,设置 1 回调按钮:回调后台接口, data 传给后台,设置 2 指令按钮:自动在输入框插入 @bot data
173+
* @return
174+
*/
175+
public ButtonBuilder setActionType(Integer type) {
176+
button.action.type = type;
177+
return this;
178+
}
179+
180+
/**
181+
* @param enter 指令按钮可用,点击按钮后直接自动发送 data,默认 false。支持版本 8983
182+
* @return
183+
*/
184+
public ButtonBuilder setActionEnter(boolean enter) {
185+
button.action.enter = enter;
186+
return this;
187+
}
188+
189+
/**
190+
* @param reply 指令按钮可用,指令是否带引用回复本消息,默认 false。支持版本 8983
191+
* @return
192+
*/
193+
public ButtonBuilder setActionReply(boolean reply) {
194+
button.action.reply = reply;
195+
return this;
196+
}
197+
198+
/**
199+
* @param tips 客户端不支持本action的时候,弹出的toast文案
200+
* @return
201+
*/
202+
public ButtonBuilder setUnSupportTips(String tips) {
203+
button.action.unsupport_tips = tips;
204+
return this;
205+
}
206+
207+
/**
208+
* @param type 0 指定用户可操作,1 仅管理者可操作,2 所有人可操作,3 指定身份组可操作(仅频道可用)
209+
* @return
210+
*/
211+
public ButtonBuilder setPermissionType(Integer type) {
212+
button.action.permission.type = type;
213+
return this;
214+
}
215+
216+
public ButtonBuilder setPermission(Permission permission) {
217+
button.action.permission = permission;
218+
return this;
219+
}
220+
221+
public RowBuilder build() {
222+
builder.buttons.buttons.add(button);
223+
return builder;
224+
}
225+
}
226+
227+
@Getter
228+
public static class Buttons {
229+
private List<Button> buttons = new LinkedList<>();
230+
}
231+
232+
@Getter
233+
public static class Button {
234+
private RenderData render_data = new RenderData();
235+
private Action action = new Action();
236+
private String id;
237+
}
238+
239+
@Getter
240+
public static class RenderData {
241+
private String label;
242+
private String visited_label;
243+
private Integer style = 1;
244+
}
245+
246+
@Getter
247+
public static class Action {
248+
private String data = "按钮";
249+
private Integer type = 2;
250+
private Permission permission = new Permission();
251+
private String unsupport_tips = "客户端版本过低";
252+
private Boolean reply = false;
253+
private Boolean enter = false;
254+
}
255+
256+
/**
257+
* action.permisson.type int 是 0 指定用户可操作,1 仅管理者可操作,2 所有人可操作,3 指定身份组可操作(仅频道可用) <br>
258+
* action.permisson.specify_user_ids array 否 有权限的用户 id 的列表 <br>
259+
* action.permisson.specify_role_ids array 否 有权限的身份组 id 的列表(仅频道可用)
260+
*/
261+
@Getter
262+
public static class Permission {
263+
private String[] specify_role_ids = new String[]{};
264+
private String[] specify_user_ids = new String[]{};
265+
private Integer type = 2;
266+
267+
public Permission() {
268+
}
269+
270+
public Permission(String[] specify_role_ids, String[] specify_user_ids, Integer type) {
271+
this.specify_role_ids = specify_role_ids;
272+
this.specify_user_ids = specify_user_ids;
273+
this.type = type;
274+
}
275+
}
17276
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
@Getter
2626
public class Markdown implements SendAble {
2727
private String custom_template_id;
28-
private List<Param> params = new LinkedList<>();
28+
private List<Param> params = null;
2929

3030
/**
3131
* 原生md可用
3232
*/
3333
private String content;
3434

35-
@JSONField(serialize = false, deserialize = false)
3635
private Keyboard keyboard;
3736

3837
/**
@@ -45,6 +44,7 @@ public Markdown(String custom_template_id) {
4544
}
4645

4746
public Markdown addParam(String key, String value) {
47+
if (params == null) params = new LinkedList<>();
4848
params.add(new Param(key, new String[]{value}));
4949
return this;
5050
}
@@ -67,7 +67,6 @@ public Markdown setKeyboard(String id) {
6767
@AllArgsConstructor
6868
@NoArgsConstructor
6969
public static class Param {
70-
7170
public String key;
7271
private String[] values;
7372
}

0 commit comments

Comments
 (0)