-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
186 lines (159 loc) · 5.47 KB
/
session.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
184
185
186
package youdu
import (
"context"
"net/http"
)
type SessionType string
var SessionMultiType SessionType = "multi"
type CreateSessionRequest struct {
Title string `json:"title"`
Creator string `json:"creator"`
Type SessionType `json:"type,omitempty"`
Member []string `json:"member"`
}
type UpdateSessionRequest struct {
SessionID string `json:"sessionId"`
OpUser string `json:"opUser"`
Title string `json:"title,omitempty"`
AddMember []string `json:"addMember"`
DelMember []string `json:"delMember"`
}
type SessionResponse struct {
SessionID string `json:"sessionId"`
Title string `json:"title"`
Owner string `json:"owner"`
Version int `json:"version"`
Type SessionType `json:"type"`
Member []string `json:"member"`
}
func (c *Client) CreateSession(
ctx context.Context, request CreateSessionRequest,
) (response SessionResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/cgi/session/create",
withRequestBody(request), withRequestAccessToken(), withRequestEncrypt())
if err != nil {
return
}
err = c.sendRequest(req, &response, withResponseDecrypt())
return
}
func (c *Client) GetSession(ctx context.Context, sessionID string) (response SessionResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, "/cgi/session/get",
withRequestAccessToken(),
withRequestEncrypt(),
withRequestParamsKV("sessionId", sessionID),
)
if err != nil {
return
}
err = c.sendRequest(req, &response, withResponseDecrypt())
return
}
func (c *Client) UpdateSession(
ctx context.Context, request UpdateSessionRequest,
) (response SessionResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/cgi/session/update",
withRequestBody(request), withRequestAccessToken(), withRequestEncrypt())
if err != nil {
return
}
err = c.sendRequest(req, &response, withResponseDecrypt())
return
}
type InterfaceSessionMessageRequest interface{}
var (
_ InterfaceSessionMessageRequest = SessionMessageResponse{}
_ InterfaceSessionMessageRequest = TextSessionMessageRequest{}
_ InterfaceSessionMessageRequest = ImageSessionMessageRequest{}
_ InterfaceSessionMessageRequest = FileSessionMessageRequest{}
_ InterfaceSessionMessageRequest = VoiceSessionMessageRequest{}
_ InterfaceSessionMessageRequest = VideoSessionMessageRequest{}
)
type SessionMessageResponse struct {
SessionID string `json:"sessionId,omitempty"`
Receiver string `json:"receiver,omitempty"`
Sender string `json:"sender"`
MsgType MsgType `json:"msgType"`
Text MessageText `json:"text,omitempty"`
Image MessageMedia `json:"image,omitempty"`
File MessageFile `json:"file,omitempty"`
Voice MessageMedia `json:"voice,omitempty"`
Video MessageMedia `json:"video,omitempty"`
}
type TextSessionMessageRequest struct {
SessionID string `json:"sessionId,omitempty"`
Receiver string `json:"receiver,omitempty"`
Sender string `json:"sender"`
MsgType MsgType `json:"msgType"`
Text MessageText `json:"text"`
}
type ImageSessionMessageRequest struct {
SessionID string `json:"sessionId,omitempty"`
Receiver string `json:"receiver,omitempty"`
Sender string `json:"sender"`
MsgType MsgType `json:"msgType"`
Image MessageMedia `json:"image"`
}
type FileSessionMessageRequest struct {
SessionID string `json:"sessionId,omitempty"`
Receiver string `json:"receiver,omitempty"`
Sender string `json:"sender"`
MsgType MsgType `json:"msgType"`
File MessageFile `json:"file"`
}
type VoiceSessionMessageRequest struct {
SessionID string `json:"sessionId,omitempty"`
Receiver string `json:"receiver,omitempty"`
Sender string `json:"sender"`
MsgType MsgType `json:"msgType"`
Voice MessageMedia `json:"voice"`
}
type VideoSessionMessageRequest struct {
SessionID string `json:"sessionId,omitempty"`
Receiver string `json:"receiver,omitempty"`
Sender string `json:"sender"`
MsgType MsgType `json:"msgType"`
Video MessageMedia `json:"video"`
}
func (c *Client) SendSessionMessage(
ctx context.Context,
request InterfaceSessionMessageRequest,
) (response Response, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/cgi/session/send",
withRequestBody(request), withRequestAccessToken(), withRequestEncrypt())
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}
func (c *Client) SendTextSessionMessage(
ctx context.Context, request TextSessionMessageRequest,
) (response Response, err error) {
request.MsgType = MsgTypeText
return c.SendSessionMessage(ctx, request)
}
func (c *Client) SendImageSessionMessage(
ctx context.Context, request ImageSessionMessageRequest,
) (response Response, err error) {
request.MsgType = MsgTypeImage
return c.SendSessionMessage(ctx, request)
}
func (c *Client) SendFileSessionMessage(
ctx context.Context, request FileSessionMessageRequest,
) (response Response, err error) {
request.MsgType = MsgTypeFile
return c.SendSessionMessage(ctx, request)
}
func (c *Client) SendVoiceSessionMessage(
ctx context.Context, request VoiceSessionMessageRequest,
) (response Response, err error) {
request.MsgType = MsgTypeVoice
return c.SendSessionMessage(ctx, request)
}
func (c *Client) SendVideoSessionMessage(
ctx context.Context, request VideoSessionMessageRequest,
) (response Response, err error) {
request.MsgType = MsgTypeVideo
return c.SendSessionMessage(ctx, request)
}