diff --git a/reminders.go b/reminders.go index 9b905387e..de1170a63 100644 --- a/reminders.go +++ b/reminders.go @@ -21,6 +21,11 @@ type reminderResp struct { Reminder Reminder `json:"reminder"` } +type remindersResp struct { + SlackResponse + Reminders []Reminder `json:"reminders"` +} + func (api *Client) doReminder(ctx context.Context, path string, values url.Values) (*Reminder, error) { response := &reminderResp{} if err := api.postMethod(ctx, path, values, response); err != nil { @@ -29,6 +34,31 @@ func (api *Client) doReminder(ctx context.Context, path string, values url.Value return &response.Reminder, response.Err() } +func (api *Client) doReminders(ctx context.Context, path string, values url.Values) ([]*Reminder, error) { + response := &remindersResp{} + if err := api.postMethod(ctx, path, values, response); err != nil { + return nil, err + } + + // create an array of pointers to reminders + var reminders = make([]*Reminder, 0, len(response.Reminders)) + for _, reminder := range response.Reminders { + reminders = append(reminders, &reminder) + } + + return reminders, response.Err() +} + +// ListReminders lists all the reminders created by or for the authenticated user +// +// See https://api.slack.com/methods/reminders.list +func (api *Client) ListReminders() ([]*Reminder, error) { + values := url.Values{ + "token": {api.token}, + } + return api.doReminders(context.Background(), "reminders.list", values) +} + // AddChannelReminder adds a reminder for a channel. // // See https://api.slack.com/methods/reminders.add (NOTE: the ability to set diff --git a/reminders_test.go b/reminders_test.go index 1ced533a4..bed758948 100644 --- a/reminders_test.go +++ b/reminders_test.go @@ -156,3 +156,18 @@ func TestSlack_DeleteReminder(t *testing.T) { } } } + +func TestSlack_ListReminders(t *testing.T) { + // as the ListReminders takes only token parameter + // we just test if the call doesn't return an error + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + var rh *remindersHandler + http.HandleFunc("/reminders.list", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) + rh = newRemindersHandler() + + _, err := api.ListReminders() + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } +}