Skip to content

Commit fad5ee9

Browse files
casonliwsqDevTeam
authored andcommitted
openapi 支持消息引用 api授权链接 api 权限列表 (merge request !27)
- 发消息接口增加消息引用支持 - 增加 api 接口创建 api 权限授权链接 - 增加 api 接口获取 api 权限列表
1 parent 0b59b45 commit fad5ee9

File tree

8 files changed

+207
-8
lines changed

8 files changed

+207
-8
lines changed

dto/api_permissions.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dto
2+
3+
// APIPermissions API 权限列表对象
4+
type APIPermissions struct {
5+
APIList []*APIPermission `json:"apis,omitempty"` // API 权限列表
6+
}
7+
8+
// APIPermission API 权限对象
9+
type APIPermission struct {
10+
Path string `json:"path,omitempty"` // API 接口名,例如 /guilds/{guild_id}/members/{user_id}
11+
Method string `json:"method,omitempty"` // 请求方法,例如 GET
12+
Desc string `json:"desc,omitempty"` // API 接口名称,例如 获取频道信
13+
AuthStatus int `json:"auth_status,omitempty"` // 授权状态,auth_stats 为 1 时已授权
14+
}
15+
16+
// APIPermissionDemandIdentify API 权限需求标识对象
17+
type APIPermissionDemandIdentify struct {
18+
Path string `json:"path,omitempty"` // API 接口名,例如 /guilds/{guild_id}/members/{user_id}
19+
Method string `json:"method,omitempty"` // 请求方法,例如 GET
20+
}
21+
22+
// APIPermissionDemand 接口权限需求对象
23+
type APIPermissionDemand struct {
24+
GuildID string `json:"guild_id,omitempty"` // 频道 ID
25+
ChannelID string `json:"channel_id,omitempty"` // 子频道 ID
26+
APIIdentify *APIPermissionDemandIdentify `json:"api_identify,omitempty"` // 权限接口唯一标识
27+
Title string `json:"title,omitempty"` // 接口权限链接中的接口权限描述信息
28+
Desc string `json:"desc,omitempty"` // 接口权限链接中的机器人可使用功能的描述信息
29+
}
30+
31+
// APIPermissionDemandToCreate 创建频道 API 接口权限授权链接结构体定义
32+
type APIPermissionDemandToCreate struct {
33+
ChannelID string `json:"channel_id"` // 子频道 ID
34+
APIIdentify *APIPermissionDemandIdentify `json:"api_identify,omitempty"` // 接口权限链接中的接口权限描述信息
35+
Desc string `json:"desc"` // 接口权限链接中的机器人可使用功能的描述信息
36+
}

dto/message_create.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@ package dto
22

33
// MessageToCreate 发送消息结构体定义
44
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,为空是主动消息,公域机器人会异步审核,不为空是被动消息,公域机器人会校验语料
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+
}
13+
14+
// MessageReference 引用消息
15+
type MessageReference struct {
16+
MessageID string `json:"message_id"` // 消息 id
17+
IgnoreGetMessageError bool `json:"ignore_get_message_error"` // 是否忽律获取消息失败错误
18+
}
19+
20+
// Markdown markdown 消息
21+
type Markdown struct {
22+
TemplateID int `json:"template_id"`
23+
Params []*MarkdownParams `json:"params"`
24+
}
25+
26+
// MarkdownParams markdown 模版参数 键值对
27+
type MarkdownParams struct {
28+
Key string `json:"key"`
29+
Values []string `json:"values"`
1030
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package apitest
2+
3+
import (
4+
"testing"
5+
6+
"github.com/tencent-connect/botgo/dto"
7+
)
8+
9+
func TestGetAPIPermissions(t *testing.T) {
10+
apiIdentify := &dto.APIPermissionDemandIdentify{}
11+
t.Run(
12+
"get api permissions", func(t *testing.T) {
13+
result, err := api.GetAPIPermissions(ctx, testGuildID)
14+
if err != nil {
15+
t.Error(err)
16+
}
17+
for _, v := range result.APIList {
18+
t.Logf("api permissions:%+v", v)
19+
}
20+
if len(result.APIList) > 0 {
21+
apiIdentify.Path = result.APIList[0].Path
22+
apiIdentify.Method = result.APIList[0].Method
23+
}
24+
25+
},
26+
)
27+
t.Run(
28+
"create API permission demand", func(t *testing.T) {
29+
demand, err := api.RequireAPIPermissions(ctx, testGuildID, &dto.APIPermissionDemandToCreate{
30+
ChannelID: testChannelID,
31+
APIIdentify: apiIdentify,
32+
Desc: "授权链接",
33+
})
34+
if err != nil {
35+
t.Error(err)
36+
}
37+
t.Logf("demand:%+v", demand)
38+
},
39+
)
40+
}

examples/apitest/main_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ var (
2626
testChannelID = "1513270" // replace your channel id
2727
testMessageID = `08e092eeb983afef9e0110f9bb5d1a1231343431313532313836373838333234303420801e
2828
28003091c4bb02380c400c48d8a7928d06` // replace your channel id
29-
testRolesID = `10054557` // replace your roles id
30-
testMemberID = `1201318637970874066` // replace your member id
31-
ctx context.Context
29+
testRolesID = `10054557` // replace your roles id
30+
testMemberID = `1201318637970874066` // replace your member id
31+
testMarkdownTemplateID = 1231231231231231 // replace your markdown template id
32+
ctx context.Context
3233
)
3334

3435
func TestMain(m *testing.M) {

examples/apitest/message_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,59 @@ func TestRetractMessage(t *testing.T) {
9595
},
9696
)
9797
}
98+
99+
func TestMessageReference(t *testing.T) {
100+
t.Run(
101+
"引用消息", func(t *testing.T) {
102+
message, err := api.PostMessage(ctx, testChannelID, &dto.MessageToCreate{
103+
Content: "文本引用消息",
104+
MessageReference: &dto.MessageReference{
105+
MessageID: testMessageID,
106+
IgnoreGetMessageError: false,
107+
},
108+
})
109+
if err != nil {
110+
t.Error(err)
111+
}
112+
t.Logf("message : %v", message)
113+
},
114+
)
115+
}
116+
117+
func TestMarkdownMessage(t *testing.T) {
118+
t.Run(
119+
"markdown 消息", func(t *testing.T) {
120+
message, err := api.PostMessage(ctx, testChannelID, &dto.MessageToCreate{
121+
Markdown: &dto.Markdown{
122+
TemplateID: testMarkdownTemplateID,
123+
Params: []*dto.MarkdownParams{
124+
{
125+
Key: "title",
126+
Values: []string{"标题"},
127+
},
128+
{
129+
Key: "slice",
130+
Values: []string{"1", "频道名称<#1146349>", "3"},
131+
},
132+
{
133+
Key: "image",
134+
Values: []string{"https://pub.idqqimg.com/pc/misc/files/20191015/32ed5b691a1138ac452a59e42f3f83b5.png"},
135+
},
136+
{
137+
Key: "link",
138+
Values: []string{"[🔗我的收藏夹](qq.com)"},
139+
},
140+
{
141+
Key: "desc",
142+
Values: []string{"简介"},
143+
},
144+
},
145+
},
146+
})
147+
if err != nil {
148+
t.Error(err)
149+
}
150+
t.Logf("message : %v", message)
151+
},
152+
)
153+
}

openapi/iface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type OpenAPI interface {
2323
ChannelPermissionsAPI
2424
AnnouncesAPI
2525
ScheduleAPI
26+
APIPermissionsAPI
2627
}
2728

2829
// Base 基础能力接口
@@ -168,3 +169,12 @@ type ScheduleAPI interface {
168169
// DeleteSchedule 删除日程
169170
DeleteSchedule(ctx context.Context, channelID, scheduleID string) error
170171
}
172+
173+
// APIPermissionsAPI api 权限相关接口
174+
type APIPermissionsAPI interface {
175+
// GetAPIPermissions 获取频道可用权限列表
176+
GetAPIPermissions(ctx context.Context, guildID string) (*dto.APIPermissions, error)
177+
// RequireAPIPermissions 创建频道 API 接口权限授权链接
178+
RequireAPIPermissions(ctx context.Context,
179+
guildID string, demand *dto.APIPermissionDemandToCreate) (*dto.APIPermissionDemand, error)
180+
}

openapi/v1/api_permissions.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
6+
"github.com/tencent-connect/botgo/dto"
7+
)
8+
9+
// GetAPIPermissions 获取频道可用权限列表
10+
func (o *openAPI) GetAPIPermissions(ctx context.Context, guildID string) (*dto.APIPermissions, error) {
11+
resp, err := o.request(ctx).
12+
SetResult(dto.APIPermissions{}).
13+
SetPathParam("guild_id", guildID).
14+
Get(o.getURL(apiPermissionURI))
15+
if err != nil {
16+
return nil, err
17+
}
18+
return resp.Result().(*dto.APIPermissions), nil
19+
}
20+
21+
// RequireAPIPermissions 创建频道 API 接口权限授权链接
22+
func (o *openAPI) RequireAPIPermissions(ctx context.Context,
23+
guildID string, demand *dto.APIPermissionDemandToCreate) (*dto.APIPermissionDemand, error) {
24+
resp, err := o.request(ctx).
25+
SetResult(dto.APIPermissionDemand{}).
26+
SetPathParam("guild_id", guildID).
27+
SetBody(demand).
28+
Post(o.getURL(apiPermissionDemandURI))
29+
if err != nil {
30+
return nil, err
31+
}
32+
return resp.Result().(*dto.APIPermissionDemand), nil
33+
}

openapi/v1/resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const (
5353

5454
schedulesURI uri = "/channels/{channel_id}/schedules"
5555
scheduleURI uri = "/channels/{channel_id}/schedules/{schedule_id}"
56+
57+
apiPermissionURI uri = "/guilds/{guild_id}/api_permission"
58+
apiPermissionDemandURI uri = "/guilds/{guild_id}/api_permission/demand"
5659
)
5760

5861
// getURL 获取接口地址,会处理沙箱环境判断

0 commit comments

Comments
 (0)