update reminders API
Jamie Hannaford authored 5 years ago
James committed 5 years ago
1 | 1 | |
2 | 2 | |
3 | 3 | [[projects]] |
4 | digest = "1:a2c1d0e43bd3baaa071d1b9ed72c27d78169b2b269f71c105ac4ba34b1be4a39" | |
4 | 5 | name = "github.com/davecgh/go-spew" |
5 | 6 | packages = ["spew"] |
7 | pruneopts = "UT" | |
6 | 8 | revision = "346938d642f2ec3594ed81d874461961cd0faa76" |
7 | 9 | version = "v1.1.0" |
8 | 10 | |
9 | 11 | [[projects]] |
12 | digest = "1:43dd08a10854b2056e615d1b1d22ac94559d822e1f8b6fcc92c1a1057e85188e" | |
10 | 13 | name = "github.com/gorilla/websocket" |
11 | 14 | packages = ["."] |
15 | pruneopts = "UT" | |
12 | 16 | revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b" |
13 | 17 | version = "v1.2.0" |
14 | 18 | |
15 | 19 | [[projects]] |
20 | digest = "1:40e195917a951a8bf867cd05de2a46aaf1806c50cf92eebf4c16f78cd196f747" | |
16 | 21 | name = "github.com/pkg/errors" |
17 | 22 | packages = ["."] |
23 | pruneopts = "UT" | |
18 | 24 | revision = "645ef00459ed84a119197bfb8d8205042c6df63d" |
19 | 25 | version = "v0.8.0" |
20 | 26 | |
21 | 27 | [[projects]] |
28 | digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" | |
22 | 29 | name = "github.com/pmezard/go-difflib" |
23 | 30 | packages = ["difflib"] |
31 | pruneopts = "UT" | |
24 | 32 | revision = "792786c7400a136282c1664665ae0a8db921c6c2" |
25 | 33 | version = "v1.0.0" |
26 | 34 | |
27 | 35 | [[projects]] |
36 | digest = "1:18752d0b95816a1b777505a97f71c7467a8445b8ffb55631a7bf779f6ba4fa83" | |
28 | 37 | name = "github.com/stretchr/testify" |
29 | 38 | packages = ["assert"] |
39 | pruneopts = "UT" | |
30 | 40 | revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686" |
31 | 41 | version = "v1.2.2" |
32 | 42 | |
33 | 43 | [solve-meta] |
34 | 44 | analyzer-name = "dep" |
35 | 45 | analyzer-version = 1 |
36 | inputs-digest = "596fa546322c2a1e9708a10c9f39aca2e04792b477fab86fb2899fbaab776070" | |
46 | input-imports = [ | |
47 | "github.com/gorilla/websocket", | |
48 | "github.com/pkg/errors", | |
49 | "github.com/stretchr/testify/assert", | |
50 | ] | |
37 | 51 | solver-name = "gps-cdcl" |
38 | 52 | solver-version = 1 |
5 | 5 | "net/url" |
6 | 6 | ) |
7 | 7 | |
8 | // AddReminder adds a reminder for a channel or a user. | |
9 | // | |
10 | // Only one of ChannelID or UserID should be provided. Both can be omitted, in | |
11 | // which case the reminder is set for the authenticated user. | |
12 | // | |
13 | // See https://api.slack.com/methods/reminders.add (NOTE: the ability to set | |
14 | // reminders on a channel is currently undocumented but has been tested to | |
15 | // work) | |
16 | func (api *Client) AddReminder(channelID, userID, text, time string) error { | |
17 | return api.AddReminderContext(context.Background(), channelID, userID, text, time) | |
18 | } | |
19 | ||
20 | // AddReminderContext adds a reminder for a channel or a user with a custom context. | |
21 | // | |
22 | // See AddReminder for full details. | |
23 | func (api *Client) AddReminderContext(ctx context.Context, channelID, userID, text, time string) error { | |
24 | values := url.Values{ | |
25 | "token": {api.token}, | |
26 | "text": {text}, | |
27 | "time": {time}, | |
28 | "channel": {channelID}, | |
29 | } | |
30 | if channelID != "" { | |
31 | values.Set("channel", channelID) | |
32 | } else if userID != "" { | |
33 | values.Set("user", userID) | |
34 | } | |
35 | ||
8 | func (api *Client) addReminder(ctx context.Context, values url.Values) error { | |
36 | 9 | response := &SlackResponse{} |
37 | if err := post(ctx, api.httpclient, "reminders.add", values, response, api.debug); err != nil { | |
10 | if err := postSlackMethod(ctx, api.httpclient, "reminders.add", values, response, api); err != nil { | |
38 | 11 | return err |
39 | 12 | } |
40 | 13 | if !response.Ok { |
42 | 15 | } |
43 | 16 | return nil |
44 | 17 | } |
18 | ||
19 | // AddChannelReminder adds a reminder for a channel with a custom context. | |
20 | // | |
21 | // See https://api.slack.com/methods/reminders.add (NOTE: the ability to set | |
22 | // reminders on a channel is currently undocumented but has been tested to | |
23 | // work) | |
24 | func (api *Client) AddChannelReminder(channelID, text, time string) error { | |
25 | values := url.Values{ | |
26 | "token": {api.token}, | |
27 | "text": {text}, | |
28 | "time": {time}, | |
29 | "channel": {channelID}, | |
30 | } | |
31 | return api.addReminder(context.Background(), values) | |
32 | } | |
33 | ||
34 | // AddUserReminder adds a reminder for a user with a custom context. | |
35 | // | |
36 | // See https://api.slack.com/methods/reminders.add (NOTE: the ability to set | |
37 | // reminders on a channel is currently undocumented but has been tested to | |
38 | // work) | |
39 | func (api *Client) AddUserReminder(userID, text, time string) error { | |
40 | values := url.Values{ | |
41 | "token": {api.token}, | |
42 | "text": {text}, | |
43 | "time": {time}, | |
44 | "user": {userID}, | |
45 | } | |
46 | return api.addReminder(context.Background(), values) | |
47 | } |
37 | 37 | |
38 | 38 | func TestSlack_AddReminder(t *testing.T) { |
39 | 39 | once.Do(startServer) |
40 | SLACK_API = "http://" + serverAddr + "/" | |
40 | APIURL = "http://" + serverAddr + "/" | |
41 | 41 | api := New("testing-token") |
42 | 42 | tests := []struct { |
43 | 43 | chanID string |
75 | 75 | "", |
76 | 76 | "someUserID", |
77 | 77 | "hello world", |
78 | "tomorrow at 10am", | |
78 | "tomorrow at 9am", | |
79 | 79 | map[string]string{ |
80 | 80 | "text": "hello world", |
81 | "time": "tomorrow at 10am", | |
81 | "time": "tomorrow at 9am", | |
82 | 82 | "user": "someUserID", |
83 | 83 | }, |
84 | 84 | false, |
85 | }, | |
86 | { | |
87 | "", | |
88 | "someUserID", | |
89 | "trigger-error", | |
90 | "tomorrow at 9am", | |
91 | map[string]string{ | |
92 | "text": "trigger-error", | |
93 | "time": "tomorrow at 9am", | |
94 | "user": "someUserID", | |
95 | }, | |
96 | true, | |
85 | 97 | }, |
86 | 98 | } |
87 | 99 | var rh *remindersHandler |
88 | 100 | http.HandleFunc("/reminders.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) |
89 | 101 | for i, test := range tests { |
90 | 102 | rh = newRemindersHandler() |
91 | err := api.AddReminder(test.chanID, test.userID, test.text, test.time) | |
103 | var err error | |
104 | if test.chanID != "" { | |
105 | err = api.AddChannelReminder(test.chanID, test.text, test.time) | |
106 | } else { | |
107 | err = api.AddUserReminder(test.userID, test.text, test.time) | |
108 | } | |
92 | 109 | if test.expectErr == false && err != nil { |
93 | 110 | t.Fatalf("%d: Unexpected error: %s", i, err) |
94 | 111 | } else if test.expectErr == true && err == nil { |