forked from xen0n/go-workwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
297 lines (277 loc) · 7.27 KB
/
example_test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package workwx_test
import (
"net/http"
"strconv"
"time"
"github.com/xen0n/go-workwx"
)
const (
corpID = "your_corpid"
corpSecret = "your_corpsecret"
)
func ExampleWorkwx() {
agentID := int64(1234567)
client := workwx.New(corpID)
// there're advanced options
_ = workwx.New(
corpID,
workwx.WithQYAPIHost("http://localhost:8888"),
workwx.WithHTTPClient(&http.Client{}),
)
// work with individual apps
app := client.WithApp(corpSecret, agentID)
app.SpawnAccessTokenRefresher()
// see other examples for more details
}
func ExampleWorkwxApp_SendTextMessage() {
agentID := int64(1234567)
client := workwx.New(corpID)
app := client.WithApp(corpSecret, agentID)
// preferably do this at app initialization
app.SpawnAccessTokenRefresher()
// send to user(s)
to1 := workwx.Recipient{
UserIDs: []string{"testuser"},
}
_ = app.SendTextMessage(&to1, "send to user(s)", false)
// "safe" message
to2 := workwx.Recipient{
UserIDs: []string{"testuser"},
}
_ = app.SendTextMessage(&to2, "safe message", true)
// send to party(parties)
to3 := workwx.Recipient{
PartyIDs: []string{"testdept"},
}
_ = app.SendTextMessage(&to3, "send to party(parties)", false)
// send to tag(s)
to4 := workwx.Recipient{
TagIDs: []string{"testtag"},
}
_ = app.SendTextMessage(&to4, "send to tag(s)", false)
// send to chatid
to5 := workwx.Recipient{
ChatID: "testchat",
}
_ = app.SendTextMessage(&to5, "send to chatid", false)
}
func ExampleWorkwxApp_ApplyOAEvent() {
agentID := int64(1234567)
client := workwx.New(corpID)
app := client.WithApp(corpSecret, agentID)
app.SpawnAccessTokenRefresher()
appInfo := workwx.OAApplyEvent{
CreatorUserID: "your_userid",
TemplateID: "your_templateid",
UseTemplateApprover: 1,
ApplyData: workwx.OAContents{
Contents: []workwx.OAContent{
{
Control: workwx.OAControlText,
ID: "Text-1608628829793",
Value: workwx.OAContentValue{
Text: "文本",
},
},
{
Control: workwx.OAControlTextarea,
ID: "Textarea-1608628832640",
Value: workwx.OAContentValue{
Text: "多行文本\n可换行",
},
},
{
Control: workwx.OAControlNumber,
ID: "Number-1608632495498",
Value: workwx.OAContentValue{
Number: "123.45",
},
},
{
Control: workwx.OAControlMoney,
ID: "Money-1608632497034",
Value: workwx.OAContentValue{
Money: "678.90",
},
},
{
Control: workwx.OAControlFormula,
ID: "Formula-1608632498148",
Value: workwx.OAContentValue{
Formula: workwx.OAContentFormula{Value: "5.0"},
},
},
{
Control: workwx.OAControlDate,
ID: "Date-1608632499227",
Value: workwx.OAContentValue{
Date: workwx.OAContentDate{Type: "day", Timestamp: strconv.FormatInt(time.Now().Unix(), 10)},
},
},
{
Control: workwx.OAControlDate,
ID: "Date-1608632500394",
Value: workwx.OAContentValue{
Date: workwx.OAContentDate{Type: "hour", Timestamp: strconv.FormatInt(time.Now().Unix(), 10)},
},
},
{
Control: workwx.OAControlDateRange,
ID: "DateRange-1608632502131",
Value: workwx.OAContentValue{
DateRange: workwx.OAContentDateRange{
NewBegin: int(time.Now().Unix()),
NewEnd: int(time.Now().Add(time.Hour * 24).Unix()),
NewDuration: 60 * 60 * 24,
},
},
},
{
Control: workwx.OAControlSelector,
ID: "Selector-1608632503203",
Value: workwx.OAContentValue{
Selector: workwx.OAContentSelector{
Type: "single",
Options: []workwx.OAContentSelectorOption{
{Key: "option-1608632503204"},
},
},
},
},
{
Control: workwx.OAControlSelector,
ID: "Selector-1608632504330",
Value: workwx.OAContentValue{
Selector: workwx.OAContentSelector{
Type: "multi",
Options: []workwx.OAContentSelectorOption{
{Key: "option-1608632504330"},
{Key: "option-1608632504331"},
},
},
},
},
{
Control: workwx.OAControlContact,
ID: "Contact-1608632505579",
Value: workwx.OAContentValue{
Members: []workwx.OAContentMember{{
UserID: "your_userid",
Name: "your_name",
}},
},
},
{
Control: workwx.OAControlContact,
ID: "Contact-1608632506635",
Value: workwx.OAContentValue{
Departments: []workwx.OAContentDepartment{{
OpenAPIID: "39",
Name: "xx部门1",
}, {
OpenAPIID: "40",
Name: "xx部门2",
}},
},
},
{
Control: workwx.OAControlLocation,
ID: "Location-1608632507748",
Value: workwx.OAContentValue{
Location: workwx.OAContentLocation{
Latitude: "30.547239",
Longitude: "104.063291",
Title: "腾讯科技(成都)有限公司(腾讯成都大厦)",
Address: "四川省成都市武侯区天府三街198号腾讯成都大厦A座",
Time: int(time.Now().Unix()),
},
},
},
{
Control: workwx.OAControlRelatedApproval,
ID: "RelatedApproval-1608632509930",
Value: workwx.OAContentValue{
RelatedApproval: []workwx.OAContentRelatedApproval{
{SpNo: "202012220021"},
},
},
},
{
Control: workwx.OAControlTable,
ID: "Table-1608632511066",
Value: workwx.OAContentValue{
Table: []workwx.OAContentTableList{
{
List: []workwx.OAContent{
{
Control: workwx.OAControlText,
ID: "Text-1608632519610",
Value: workwx.OAContentValue{
Text: "第一行第一列",
},
}, {
Control: workwx.OAControlText,
ID: "Text-1608632521106",
Value: workwx.OAContentValue{
Text: "第一行第二列",
},
},
},
},
{
List: []workwx.OAContent{
{
Control: workwx.OAControlText,
ID: "Text-1608632519610",
Value: workwx.OAContentValue{
Text: "第二行第一列",
},
}, {
Control: workwx.OAControlText,
ID: "Text-1608632521106",
Value: workwx.OAContentValue{
Text: "第二行第二列",
},
},
},
},
},
},
},
{
Control: workwx.OAControlVacation,
ID: "Vacation-1608715577151",
Value: workwx.OAContentValue{
Vacation: workwx.OAContentVacation{
Selector: workwx.OAContentSelector{
Type: "single",
Options: []workwx.OAContentSelectorOption{
{
Key: "3",
},
},
},
Attendance: workwx.OAContentVacationAttendance{
DateRange: workwx.OAContentVacationAttendanceDateRange{
Type: "hour",
OAContentDateRange: workwx.OAContentDateRange{
NewBegin: int(time.Now().Unix()),
NewEnd: int(time.Now().Add(time.Hour * 72).Unix()),
NewDuration: 60 * 60 * 72,
},
},
Type: 1,
},
},
},
},
},
},
SummaryList: []workwx.OASummaryList{{SummaryInfo: []workwx.OAText{{
Text: "摘要第1行",
}}}, {SummaryInfo: []workwx.OAText{{
Text: "摘要第2行",
}}}},
}
_, _ = app.ApplyOAEvent(appInfo)
}