-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonebot.go
183 lines (170 loc) · 4.6 KB
/
onebot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"github.com/sirupsen/logrus"
"io"
"net/http"
"strconv"
)
var OneBotClient *http.Client
type OneBotTokenTransport struct {
Token string
Base http.RoundTripper
}
func (t *OneBotTokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+t.Token)
return t.Base.RoundTrip(req)
}
type FriendInfoData struct {
UserId int64 `json:"user_id"`
Nickname string `json:"nickname"`
Remark string `json:"remark"`
}
type GroupInfoData struct {
GroupId int64 `json:"group_id"`
GroupName string `json:"group_name"`
GroupMemo string `json:"group_memo"`
GroupCreateTime uint32 `json:"group_create_time"`
GroupLevel uint32 `json:"group_level"`
MemberCount int32 `json:"member_count"`
MaxMemberCount int32 `json:"max_member_count"`
}
type FriendList struct {
Retcode int64 `json:"retcode"`
Status string `json:"status"`
Data []FriendInfoData `json:"data"`
}
type GroupList struct {
Retcode int64 `json:"retcode"`
Status string `json:"status"`
Data []GroupInfoData `json:"data"`
}
type GroupInfo struct {
Retcode int64 `json:"retcode"`
Status string `json:"status"`
Data GroupInfoData `json:"data"`
}
type LoginInfo struct {
Retcode int64 `json:"retcode"`
Status string `json:"status"`
Data struct {
UserId int64 `json:"user_id"`
Nickname string `json:"nickname"`
} `json:"data"`
}
type GroupMemberInfo struct {
Retcode int64 `json:"retcode"`
Status string `json:"status"`
Data struct {
UserId int64 `json:"user_id"`
Nickname string `json:"nickname"`
Card string `json:"card"`
} `json:"data"`
}
func GetGroupList() ([]GroupInfoData, error) {
requestUrl := GlobalConfig.OneBot11.ServerUrl + "get_group_list"
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return nil, err
}
resp, err := OneBotClient.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
logrus.Info("Get group list success: " + string(body))
var respData GroupList
err = json.Unmarshal(body, &respData)
return respData.Data, err
}
func GetFriendList() ([]FriendInfoData, error) {
requestUrl := GlobalConfig.OneBot11.ServerUrl + "get_friend_list"
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return nil, err
}
resp, err := OneBotClient.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
logrus.Info("Get friend list success: " + string(body))
var respData FriendList
err = json.Unmarshal(body, &respData)
return respData.Data, err
}
func GetLoginInfo() (LoginInfo, error) {
requestUrl := GlobalConfig.OneBot11.ServerUrl + "get_login_info"
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return LoginInfo{}, err
}
resp, err := OneBotClient.Do(req)
defer resp.Body.Close()
if err != nil {
return LoginInfo{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return LoginInfo{}, err
}
logrus.Info("Get login info success: " + string(body))
var respData LoginInfo
err = json.Unmarshal(body, &respData)
return respData, err
}
func GetGroupMemberInfo(userId string, groupId string) (GroupMemberInfo, error) {
requestUrl := GlobalConfig.OneBot11.ServerUrl + "get_group_member_info"
req, err := http.NewRequest("GET", requestUrl, nil)
q := req.URL.Query()
q.Add("group_id", groupId)
q.Add("user_id", userId)
req.URL.RawQuery = q.Encode()
if err != nil {
return GroupMemberInfo{}, err
}
resp, err := OneBotClient.Do(req)
defer resp.Body.Close()
if err != nil {
return GroupMemberInfo{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return GroupMemberInfo{}, err
}
logrus.Info("Get group member info success: " + string(body))
var respData GroupMemberInfo
err = json.Unmarshal(body, &respData)
return respData, err
}
func GetGroupInfo(groupId int64) (GroupInfo, error) {
requestUrl := GlobalConfig.OneBot11.ServerUrl + "get_group_info"
req, err := http.NewRequest("GET", requestUrl, nil)
q := req.URL.Query()
q.Add("group_id", strconv.FormatInt(groupId, 10))
req.URL.RawQuery = q.Encode()
if err != nil {
return GroupInfo{}, err
}
resp, err := OneBotClient.Do(req)
defer resp.Body.Close()
if err != nil {
return GroupInfo{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return GroupInfo{}, err
}
logrus.Info("Get group info success: " + string(body))
var respData GroupInfo
err = json.Unmarshal(body, &respData)
return respData, err
}