|
1 | 1 | package io.github.kloping.qqbot.entities.ex;
|
2 | 2 |
|
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; |
5 | 18 |
|
6 | 19 | /**
|
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 | + * |
10 | 25 | *
|
11 | 26 | * @author github.kloping
|
12 | 27 | */
|
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 | + } |
17 | 276 | }
|
0 commit comments