Codebase list golang-github-nlopes-slack / 939518d
update reminders API Jamie Hannaford authored 5 years ago James committed 5 years ago
3 changed file(s) with 68 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
11
22
33 [[projects]]
4 digest = "1:a2c1d0e43bd3baaa071d1b9ed72c27d78169b2b269f71c105ac4ba34b1be4a39"
45 name = "github.com/davecgh/go-spew"
56 packages = ["spew"]
7 pruneopts = "UT"
68 revision = "346938d642f2ec3594ed81d874461961cd0faa76"
79 version = "v1.1.0"
810
911 [[projects]]
12 digest = "1:43dd08a10854b2056e615d1b1d22ac94559d822e1f8b6fcc92c1a1057e85188e"
1013 name = "github.com/gorilla/websocket"
1114 packages = ["."]
15 pruneopts = "UT"
1216 revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b"
1317 version = "v1.2.0"
1418
1519 [[projects]]
20 digest = "1:40e195917a951a8bf867cd05de2a46aaf1806c50cf92eebf4c16f78cd196f747"
1621 name = "github.com/pkg/errors"
1722 packages = ["."]
23 pruneopts = "UT"
1824 revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
1925 version = "v0.8.0"
2026
2127 [[projects]]
28 digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
2229 name = "github.com/pmezard/go-difflib"
2330 packages = ["difflib"]
31 pruneopts = "UT"
2432 revision = "792786c7400a136282c1664665ae0a8db921c6c2"
2533 version = "v1.0.0"
2634
2735 [[projects]]
36 digest = "1:18752d0b95816a1b777505a97f71c7467a8445b8ffb55631a7bf779f6ba4fa83"
2837 name = "github.com/stretchr/testify"
2938 packages = ["assert"]
39 pruneopts = "UT"
3040 revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686"
3141 version = "v1.2.2"
3242
3343 [solve-meta]
3444 analyzer-name = "dep"
3545 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 ]
3751 solver-name = "gps-cdcl"
3852 solver-version = 1
55 "net/url"
66 )
77
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 {
369 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 {
3811 return err
3912 }
4013 if !response.Ok {
4215 }
4316 return nil
4417 }
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 }
3737
3838 func TestSlack_AddReminder(t *testing.T) {
3939 once.Do(startServer)
40 SLACK_API = "http://" + serverAddr + "/"
40 APIURL = "http://" + serverAddr + "/"
4141 api := New("testing-token")
4242 tests := []struct {
4343 chanID string
7575 "",
7676 "someUserID",
7777 "hello world",
78 "tomorrow at 10am",
78 "tomorrow at 9am",
7979 map[string]string{
8080 "text": "hello world",
81 "time": "tomorrow at 10am",
81 "time": "tomorrow at 9am",
8282 "user": "someUserID",
8383 },
8484 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,
8597 },
8698 }
8799 var rh *remindersHandler
88100 http.HandleFunc("/reminders.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
89101 for i, test := range tests {
90102 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 }
92109 if test.expectErr == false && err != nil {
93110 t.Fatalf("%d: Unexpected error: %s", i, err)
94111 } else if test.expectErr == true && err == nil {