forked from griffin-stewie/go-chatwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
535 lines (472 loc) · 14.4 KB
/
api.go
File metadata and controls
535 lines (472 loc) · 14.4 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package gochatwork
import (
"encoding/json"
"fmt"
"io"
"time"
)
// BaseURL ChatWork API endpooint URL
const BaseURL = `https://api.chatwork.com/v2`
// Me model
type Me struct {
AccountID int `json:"account_id"`
RoomID int `json:"room_id"`
Name string `json:"name"`
ChatworkID string `json:"chatwork_id"`
OrganizationID int `json:"organization_id"`
OrganizationName string `json:"organization_name"`
Department string `json:"department"`
Title string `json:"title"`
URL string `json:"url"`
Introduction string `json:"introduction"`
Mail string `json:"mail"`
TelOrganization string `json:"tel_organization"`
TelExtension string `json:"tel_extension"`
TelMobile string `json:"tel_mobile"`
Skype string `json:"skype"`
Facebook string `json:"facebook"`
Twitter string `json:"twitter"`
AvatarImageURL string `json:"avatar_image_url"`
LoginMail string `json:"login_mail"`
}
// Me GET "/me"
func (c *Client) Me() (me Me, err error) {
ret, err := c.Get("/me", map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &me)
return
}
// Status model
type Status struct {
UnreadRoomNum int `json:"unread_room_num"`
MentionRoomNum int `json:"mention_room_num"`
MytaskRoomNum int `json:"mytask_room_num"`
UnreadNum int `json:"unread_num"`
MentionNum int `json:"mention_num"`
MyTaskNum int `json:"mytask_num"`
}
// MyStatus GET "/my/status"
func (c *Client) MyStatus() (status Status, err error) {
ret, err := c.Get("/my/status", nil)
if err != nil {
return
}
err = json.Unmarshal(ret, &status)
return
}
// MyTask model
type MyTask struct {
Task
Room struct {
Roomid int `json:"room_id"`
Name string `json:"name"`
IconPath string `json:"icon_path"`
}
}
// MyTasks GET "/my/tasks"
// params keys
// - assigned_by_account_id
// - status: [open, done]
func (c *Client) MyTasks(params map[string]string) (tasks []MyTask, err error) {
ret, err := c.Get("/my/tasks", params)
if err != nil {
return
}
err = json.Unmarshal(ret, &tasks)
return
}
// Contact model
type Contact struct {
AccountID int `json:"account_id"`
RoomID int `json:"room_id"`
Name string `json:"name"`
ChatworkID string `json:"chatwork_id"`
OrganizationID int `json:"organization_id"`
OrganizationName string `json:"organization_name"`
Department string `json:"department"`
AvatarImageURL string `json:"avatar_image_url"`
}
// Contacts GET "/contacts"
func (c *Client) Contacts() (contacts []Contact, err error) {
ret, err := c.Get("/contacts", map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &contacts)
return
}
// Room model
type Room struct {
RoomID int `json:"room_id"`
Name string `json:"name"`
Type string `json:"type"`
Role string `json:"role"`
Sticky bool `json:"sticky"`
UnreadNum int `json:"unread_num"`
MentionNum int `json:"mention_num"`
MytaskNum int `json:"mytask_num"`
MessageNum int `json:"message_num"`
FileNum int `json:"file_num"`
TaskNum int `json:"task_num"`
IconPath string `json:"icon_path"`
LastUpdateTime int64 `json:"last_update_time"`
Description string `json:"description"`
}
// Rooms GET "/rooms"
func (c *Client) Rooms() (rooms []Room, err error) {
ret, err := c.Get("/rooms", map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &rooms)
return
}
// Room GET "/rooms/{room_id}"
func (c *Client) Room(roomID string) (room Room, err error) {
ret, err := c.Get("/rooms/"+roomID, map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &room)
return
}
// CreateRoom POST "/rooms"
// params keys
// * name
// * members_admin_ids
// - description
// - icon_preset
// - members_member_ids
// - members_readonly_ids
func (c *Client) CreateRoom(params map[string]string) ([]byte, error) {
return c.Post("/rooms", params)
}
// UpdateRoom PUT "/rooms/{room_id}"
// params keys
// - description
// - icon_preset
// - name
func (c *Client) UpdateRoom(roomID string, params map[string]string) ([]byte, error) {
return c.Put("/rooms/"+roomID, params)
}
// DeleteRoom DELETE "/rooms/{room_id}"
// params key
// * action_type: [leave, delete]
func (c *Client) DeleteRoom(roomID string, params map[string]string) ([]byte, error) {
return c.Delete("/rooms/"+roomID, params)
}
// Member model
type Member struct {
AccountID int `json:"account_id"`
Role string `json:"role"`
Name string `json:"name"`
ChatworkID string `json:"chatwork_id"`
OrganizationID int `json:"organization_id"`
OrganizationName string `json:"organization_name"`
Department string `json:"department"`
AvatarImageURL string `json:"avatar_image_url"`
}
// RoomMembers GET "/rooms/{room_id}/members"
func (c *Client) RoomMembers(roomID string) (members []Member, err error) {
ret, err := c.Get("/rooms/"+roomID+"/members", map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &members)
return
}
// UpdateRoomMembers PUT "/rooms/{room_id}/members"
// params keys
// * members_admin_ids
// - members_member_ids
// - members_readonly_ids
func (c *Client) UpdateRoomMembers(roomID string, params map[string]string) ([]byte, error) {
return c.Put("/rooms/"+roomID+"/members", params)
}
// Account model
type Account struct {
AccountID int `json:"account_id"`
Name string `json:"name"`
AvatarImageURL string `json:"avatar_image_url"`
}
// Message model
type Message struct {
MessageID string `json:"message_id"`
Account Account `json:"account"`
Body string `json:"body"`
SendTime int64 `json:"send_time"`
UpdateTime int64 `json:"update_time"`
}
// SendDate time.Time representation of SendTime
func (m Message) SendDate() time.Time {
return time.Unix(m.SendTime, 0)
}
// UpdateDate time.Time representation of UpdateTime
func (m Message) UpdateDate() time.Time {
return time.Unix(m.UpdateTime, 0)
}
// Messages slice of Message
type Messages []Message
// RoomMessages GET "/rooms/{room_id}/messages"
func (c *Client) RoomMessages(roomID string, params map[string]string) (msgs Messages, err error) {
ret, err := c.Get("/rooms/"+roomID+"/messages", params)
if err != nil {
return
}
err = json.Unmarshal(ret, &msgs)
return
}
// PostRoomMessage POST "/rooms/{room_id}/messages"
func (c *Client) PostRoomMessage(roomID string, body string) ([]byte, error) {
return c.Post("/rooms/"+roomID+"/messages", map[string]string{"body": body})
}
// RoomMessage GET "/rooms/{room_id}/messages/{message_id}"
func (c *Client) RoomMessage(roomID, messageID string) (message Message, err error) {
ret, err := c.Get("/rooms/"+roomID+"/messages/"+messageID, map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &message)
return
}
// DeleteRoomMessage DELETE "/rooms/{room_id}/messages/{message_id}"
func (c *Client) DeleteRoomMessage(roomID, messageID string) ([]byte, error) {
return c.Delete("/rooms/"+roomID+"/messages/"+messageID, map[string]string{})
}
// UpdateRoomMessage PUT "/rooms/{room_id}/messages/{message_id}"
func (c *Client) UpdateRoomMessage(roomID, messageID, body string) ([]byte, error) {
return c.Put("/rooms/"+roomID+"/messages/"+messageID, map[string]string{"body": body})
}
// RoomMessageMarkAsRead PUT "/rooms/{room_id}/messages/read"
func (c *Client) RoomMessageMarkAsRead(roomID, messageID string) ([]byte, error) {
return c.Put("/rooms/"+roomID+"/messages/read/", map[string]string{"message_id": messageID})
}
// RoomMessageMarkAsUnread PUT "/rooms/{room_id}/messages/unread"
func (c *Client) RoomMessageMarkAsUnread(roomID, messageID string) ([]byte, error) {
return c.Put("/rooms/"+roomID+"/messages/unread/", map[string]string{"message_id": messageID})
}
// Task model
type Task struct {
TaskID int `json:"task_id"`
Account Account `json:"account"`
AssignedByAccount Account `json:"assigned_by_account"`
MessageID string `json:"message_id"`
Body string `json:"body"`
LimitTime int64 `json:"limit_time"`
Status string `json:"status"`
}
// LimitDate time.Time representation of LimitTime
func (t Task) LimitDate() time.Time {
return time.Unix(t.LimitTime, 0)
}
// RoomTasks GET "/rooms/{room_id}/tasks"
// params keys
// - account_id
// - assigned_by_account_id
// - status: [open, done]
func (c *Client) RoomTasks(roomID string, params map[string]string) (tasks []Task, err error) {
ret, err := c.Get("/rooms/"+roomID+"/tasks", params)
if err != nil {
return
}
err = json.Unmarshal(ret, &tasks)
return
}
// PostRoomTask POST "/rooms/{room_id}/tasks"
// params keys
// * body
// * to_ids
// - limit
func (c *Client) PostRoomTask(roomID string, params map[string]string) ([]byte, error) {
return c.Post("/rooms/"+roomID+"/tasks", params)
}
// RoomTask GET "/rooms/{room_id}/tasks/tasks_id"
func (c *Client) RoomTask(roomID, taskID string) (task Task, err error) {
ret, err := c.Get("/rooms/"+roomID+"/tasks/"+taskID, map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &task)
return
}
// File model
type File struct {
FileID int `json:"file_id"`
Account Account `json:"account"`
MessageID string `json:"message_id"`
Filename string `json:"filename"`
Filesize int `json:"filesize"`
UploadTime int64 `json:"upload_time"`
DownloadURL string `json:"download_url"`
}
// UploadDate time.Time representation of UploadTime
func (f File) UploadDate() time.Time {
return time.Unix(f.UploadTime, 0)
}
// RoomFiles GET "/rooms/{room_id}/files/"
// params key
// - account_id
func (c *Client) RoomFiles(roomID string, params map[string]string) (files []File, err error) {
ret, err := c.Get("/rooms/"+roomID+"/files", params)
if err != nil {
return
}
err = json.Unmarshal(ret, &files)
return
}
// RoomFile GET "/rooms/{room_id}/files/{file_id}"
// params key
// - create_download_url: ["0", "1"]
func (c *Client) RoomFile(roomID, fileID string, params map[string]string) (file File, err error) {
ret, err := c.Get("/rooms/"+roomID+"/files/"+fileID, params)
if err != nil {
return
}
err = json.Unmarshal(ret, &file)
return
}
// PostRoomFile POST "/rooms/{room_id}/files"
func (c *Client) PostRoomFile(roomID, message, fileName string, file io.Reader) ([]byte, error) {
return c.postFile("/rooms/"+roomID+"/files", message, fileName, file)
}
// RateLimit model
type RateLimit struct {
Limit int
Remaining int
ResetTime int64
}
// ResetDate time.Time representation of ResetTime
func (r RateLimit) ResetDate() time.Time {
return time.Unix(r.ResetTime, 0)
}
// RateLimit returns rate limit
func (c *Client) RateLimit() *RateLimit {
if c.latestRateLimit == nil {
// When API is not called even once, call API and get RateLimit in response header
c.Me()
}
return c.latestRateLimit
}
// InvitationLink model
type InvitationLink struct {
IsPublic bool `json:"public"`
URL string `json:"url"`
NeedAcceptance bool `json:"need_acceptance"`
Description string `json:"description"`
}
// GetInvitationLink GET "/rooms/{room_id}/link"
func (c *Client) GetInvitationLink(roomID string) (invitationLink InvitationLink, err error) {
ret, err := c.Get("/rooms/"+roomID+"/link", map[string]string{})
if err != nil {
return
}
err = json.Unmarshal(ret, &invitationLink)
return
}
// PostInvitationLink POST "/rooms/{room_id}/link"
// params keys
// - code
// - description
// - need_acceptance
func (c *Client) PostInvitationLink(roomID, code, description string, needAcceptance bool) (invitationLink InvitationLink, err error) {
params := make(map[string]string)
boolStr := "0"
if needAcceptance {
boolStr = "1"
}
params["need_acceptance"] = boolStr
if len(code) != 0 {
params["code"] = code
}
if len(description) != 0 {
params["description"] = description
}
ret, err := c.Post("/rooms/"+roomID+"/link", params)
if err != nil {
return
}
err = json.Unmarshal(ret, &invitationLink)
return
}
// UpdateInvitationLink PUT "/rooms/{room_id}/link"
// params keys
// - code
// - description
// - need_acceptance
func (c *Client) UpdateInvitationLink(roomID, code, description string, needAcceptance bool) (invitationLink InvitationLink, err error) {
params := make(map[string]string)
boolStr := "0"
if needAcceptance {
boolStr = "1"
}
params["need_acceptance"] = boolStr
if len(code) != 0 {
params["code"] = code
}
if len(description) != 0 {
params["description"] = description
}
ret, err := c.Put("/rooms/"+roomID+"/link", params)
if err != nil {
return
}
err = json.Unmarshal(ret, &invitationLink)
return
}
// DeleteInvitationLink DELETE "/rooms/{room_id}/link"
// params keys
// - code
// - description
// - need_acceptance
func (c *Client) DeleteInvitationLink(roomID string) (invitationLink InvitationLink, err error) {
params := make(map[string]string)
ret, err := c.Delete("/rooms/"+roomID+"/link", params)
if err != nil {
return
}
err = json.Unmarshal(ret, &invitationLink)
return
}
// IncomingRequest model
type IncomingRequest struct {
RequestID int `json:"request_id"`
AccountID int `json:"account_id"`
Message string `json:"message"`
Name string `json:"name"`
ChatworkID int `json:"chatwork_id"`
OrganizationID string `json:"organization_id"`
OrganizationName string `json:"organization_name"`
Department string `json:"department"`
AvatarImageUrl string `json:"avatar_image_url"`
}
// GetIncomingRequests GET "/incoming_requests"
func (c *Client) GetIncomingRequests() (incomingRequests []IncomingRequest, err error) {
ret, err := c.Get("/incoming_requests", map[string]string{})
if err != nil {
return
}
if len(ret) == 0 {
return
}
err = json.Unmarshal(ret, &incomingRequests)
return
}
// ApproveIncomingRequests PUT "/incoming_requests/{request_id}"
func (c *Client) ApproveIncomingRequests(requestID int) (incomingRequest IncomingRequest, err error) {
ret, err := c.Put(fmt.Sprintf("/incoming_requests/%d", requestID), map[string]string{})
if err != nil {
return
}
if len(ret) == 0 {
return
}
err = json.Unmarshal(ret, &incomingRequest)
return
}
// DeleteIncomingRequests DELETE "/incoming_requests/{request_id}"
func (c *Client) DeleteIncomingRequests(requestID int) error {
_, err := c.Delete(fmt.Sprintf("/incoming_requests/%d", requestID), map[string]string{})
return err
}