Skip to content

Commit 6fa7bf3

Browse files
author
casonli
committed
sdk: 增加自定义消息按钮组件 (merge request !52)
Squash merge branch 'feature_20220420_casonli_keyborad_story_873653479' into 'master' 提交自定义消息按钮组件
1 parent 3fc0a91 commit 6fa7bf3

File tree

3 files changed

+148
-15
lines changed

3 files changed

+148
-15
lines changed

dto/keyboard/keyboard.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package keyboard
2+
3+
type ActionType uint32
4+
type PermissionType uint32
5+
6+
const (
7+
// ActionTypeURL http 或 小程序 客户端识别 schema, data字段为链接
8+
ActionTypeURL ActionType = 0
9+
// ActionTypeCallback 回调互动回调地址, data 传给互动回调地址
10+
ActionTypeCallback ActionType = 1
11+
// ActionTypeAtBot at机器人, 根据 at_bot_show_channel_list 决定在当前频道或用户选择频道,自动在输入框 @bot data
12+
ActionTypeAtBot ActionType = 2
13+
// PermissionTypeSpecifyUserIDs 仅指定这条消息的人可操作
14+
PermissionTypeSpecifyUserIDs PermissionType = 0
15+
// PermissionTypManager 仅频道管理者可操作
16+
PermissionTypManager PermissionType = 1
17+
// PermissionTypAll 所有人可操作
18+
PermissionTypAll PermissionType = 2
19+
// PermissionTypSpecifyRoleIDs 指定身份组可操作
20+
PermissionTypSpecifyRoleIDs PermissionType = 3
21+
)
22+
23+
// MessageKeyboard 消息按钮组件
24+
type MessageKeyboard struct {
25+
ID string `json:"id,omitempty"` // 消息按钮组件模板 ID
26+
Content *CustomKeyboard `json:"content,omitempty"` // 消息按钮组件自定义内容
27+
}
28+
29+
// CustomKeyboard 自定义 Keyboard
30+
type CustomKeyboard struct {
31+
Rows []*Row `json:"rows,omitempty"` // 行数组
32+
}
33+
34+
// Row 每行结构
35+
type Row struct {
36+
Buttons []*Button `json:"buttons,omitempty"` // 每行按钮
37+
}
38+
39+
// Button 单个按纽
40+
type Button struct {
41+
ID string `json:"id,omitempty"` // 按钮 ID
42+
RenderData *RenderData `json:"render_data,omitempty"` // 渲染展示字段
43+
Action *Action `json:"action,omitempty"` // 该按纽操作相关字段
44+
}
45+
46+
// RenderData 按纽渲染展示
47+
type RenderData struct {
48+
Label string `json:"label,omitempty"` // 按纽上的文字
49+
VisitedLabel string `json:"visited_label,omitempty"` // 点击后按纽上文字
50+
Style int `json:"style,omitempty"` // 按钮样式,0:灰色线框,1:蓝色线框
51+
}
52+
53+
// Action 按纽点击操作
54+
type Action struct {
55+
Type ActionType `json:"type,omitempty"` // 操作类型
56+
Permission *Permission `son:"permission,omitempty"` // 可操作
57+
ClickLimit uint32 `json:"click_limit,omitempty"` // 可点击的次数, 默认不限
58+
Data string `json:"data,omitempty"` // 操作相关数据
59+
AtBotShowChannelList bool `json:"at_bot_show_channel_list,omitempty"` // false:当前 true:弹出展示子频道选择器
60+
}
61+
62+
// Permission 按纽操作权限
63+
type Permission struct {
64+
// Type 操作权限类型
65+
Type PermissionType `json:"type,omitempty"`
66+
// SpecifyRoleIDs 身份组
67+
SpecifyRoleIDs []string `json:"specify_role_ids,omitempty"`
68+
// SpecifyUserIDs 指定 UserID
69+
SpecifyUserIDs []string `json:"specify_user_ids,omitempty"`
70+
}

dto/message_create.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package dto
22

3+
import "github.com/tencent-connect/botgo/dto/keyboard"
4+
35
// MessageToCreate 发送消息结构体定义
46
type MessageToCreate struct {
5-
Content string `json:"content,omitempty"`
6-
Embed *Embed `json:"embed,omitempty"`
7-
Ark *Ark `json:"ark,omitempty"`
8-
Image string `json:"image,omitempty"`
9-
MsgID string `json:"msg_id,omitempty"` // 要回复的消息id,为空是主动消息,公域机器人会异步审核,不为空是被动消息,公域机器人会校验语料
10-
MessageReference *MessageReference `json:"message_reference,omitempty"`
11-
Markdown *Markdown `json:"markdown,omitempty"`
12-
Keyboard *Keyboard `json:"keyboard,omitempty"` // 内嵌键盘
13-
EventID string `json:"event_id,omitempty"` // 要回复的事件id, 逻辑同MsgID
7+
Content string `json:"content,omitempty"`
8+
Embed *Embed `json:"embed,omitempty"`
9+
Ark *Ark `json:"ark,omitempty"`
10+
Image string `json:"image,omitempty"`
11+
MsgID string `json:"msg_id,omitempty"` // 要回复的消息id,为空是主动消息,公域机器人会异步审核,不为空是被动消息,公域机器人会校验语料
12+
MessageReference *MessageReference `json:"message_reference,omitempty"`
13+
Markdown *Markdown `json:"markdown,omitempty"`
14+
Keyboard *keyboard.MessageKeyboard `json:"keyboard,omitempty"` // 消息按钮组件
15+
EventID string `json:"event_id,omitempty"` // 要回复的事件id, 逻辑同MsgID
1416
}
1517

1618
// MessageReference 引用消息
@@ -32,11 +34,6 @@ type MarkdownParams struct {
3234
Values []string `json:"values"`
3335
}
3436

35-
// Keyboard 内嵌键盘
36-
type Keyboard struct {
37-
ID string `json:"id"`
38-
}
39-
4037
// SettingGuideToCreate 发送引导消息的结构体
4138
type SettingGuideToCreate struct {
4239
Content string `json:"content,omitempty"` // 频道内发引导消息可以带@

examples/apitest/message_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/tencent-connect/botgo/dto/keyboard"
8+
79
"github.com/tencent-connect/botgo/dto"
810
"github.com/tencent-connect/botgo/openapi"
911
)
@@ -161,6 +163,70 @@ func TestMarkdownMessage(t *testing.T) {
161163
)
162164
}
163165

166+
func TestKeyboardMessage(t *testing.T) {
167+
t.Run(
168+
"消息按钮组件消息", func(t *testing.T) {
169+
message, err := api.PostMessage(
170+
ctx, testChannelID, &dto.MessageToCreate{
171+
Markdown: &dto.Markdown{
172+
Content: "# 123 \n 今天是个好天气",
173+
},
174+
Keyboard: &keyboard.MessageKeyboard{
175+
Content: &keyboard.CustomKeyboard{
176+
Rows: []*keyboard.Row{
177+
{
178+
Buttons: []*keyboard.Button{
179+
{
180+
ID: "1",
181+
RenderData: &keyboard.RenderData{
182+
Label: "指定身份组可点击",
183+
VisitedLabel: "点击后按钮上文字",
184+
Style: 0,
185+
},
186+
Action: &keyboard.Action{
187+
Type: keyboard.ActionTypeAtBot,
188+
Permission: &keyboard.Permission{
189+
Type: keyboard.PermissionTypAll,
190+
SpecifyRoleIDs: []string{"1"},
191+
},
192+
ClickLimit: 10,
193+
Data: "/搜索",
194+
AtBotShowChannelList: true,
195+
},
196+
},
197+
{
198+
ID: "2",
199+
RenderData: &keyboard.RenderData{
200+
Label: "指定身份组可点击",
201+
VisitedLabel: "点击后按钮上文字",
202+
Style: 0,
203+
},
204+
Action: &keyboard.Action{
205+
Type: keyboard.ActionTypeAtBot,
206+
Permission: &keyboard.Permission{
207+
Type: keyboard.PermissionTypeSpecifyUserIDs,
208+
SpecifyUserIDs: []string{"9859283702500083161"},
209+
},
210+
ClickLimit: 10,
211+
Data: "/搜索",
212+
AtBotShowChannelList: true,
213+
},
214+
},
215+
},
216+
},
217+
},
218+
},
219+
},
220+
},
221+
)
222+
if err != nil {
223+
t.Error(err)
224+
}
225+
t.Logf("message : %v", message)
226+
},
227+
)
228+
}
229+
164230
func TestContentMessage(t *testing.T) {
165231
t.Run(
166232
"content 消息", func(t *testing.T) {
@@ -182,7 +248,7 @@ func TestPatchMessage(t *testing.T) {
182248
"修改消息", func(t *testing.T) {
183249
message, err := api.PatchMessage(
184250
ctx, testChannelID, testMessageID, &dto.MessageToCreate{
185-
Keyboard: &dto.Keyboard{
251+
Keyboard: &keyboard.MessageKeyboard{
186252
ID: "62",
187253
},
188254
Markdown: &dto.Markdown{

0 commit comments

Comments
 (0)