2019-01-31 08:06:36 -08:00
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Reminder struct {
|
2020-12-31 05:48:12 -08:00
|
|
|
ID string `json:"id"`
|
|
|
|
Creator string `json:"creator"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Recurring bool `json:"recurring"`
|
|
|
|
Time int `json:"time"`
|
|
|
|
CompleteTS int `json:"complete_ts"`
|
2019-01-31 08:06:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type reminderResp struct {
|
|
|
|
SlackResponse
|
|
|
|
Reminder Reminder `json:"reminder"`
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:40:00 -07:00
|
|
|
type remindersResp struct {
|
|
|
|
SlackResponse
|
2021-03-20 14:40:23 -07:00
|
|
|
Reminders []*Reminder `json:"reminders"`
|
2020-10-19 14:40:00 -07:00
|
|
|
}
|
|
|
|
|
2019-01-31 08:06:36 -08:00
|
|
|
func (api *Client) doReminder(ctx context.Context, path string, values url.Values) (*Reminder, error) {
|
|
|
|
response := &reminderResp{}
|
2019-09-07 13:46:58 -07:00
|
|
|
if err := api.postMethod(ctx, path, values, response); err != nil {
|
2019-01-31 08:06:36 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &response.Reminder, response.Err()
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:40:00 -07:00
|
|
|
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))
|
2024-08-27 10:04:05 -07:00
|
|
|
reminders = append(reminders, response.Reminders...)
|
2020-10-19 14:40:00 -07:00
|
|
|
return reminders, response.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListReminders lists all the reminders created by or for the authenticated user
|
2024-08-27 10:04:05 -07:00
|
|
|
// For more details, see ListRemindersContext documentation.
|
2020-10-19 14:40:00 -07:00
|
|
|
func (api *Client) ListReminders() ([]*Reminder, error) {
|
2022-04-25 14:50:10 -07:00
|
|
|
return api.ListRemindersContext(context.Background())
|
|
|
|
}
|
|
|
|
|
2024-08-27 10:04:05 -07:00
|
|
|
// ListRemindersContext lists all the reminders created by or for the authenticated user with a custom context.
|
|
|
|
// Slack API docs: https://api.slack.com/methods/reminders.list
|
2022-04-25 14:50:10 -07:00
|
|
|
func (api *Client) ListRemindersContext(ctx context.Context) ([]*Reminder, error) {
|
2020-10-19 14:40:00 -07:00
|
|
|
values := url.Values{
|
|
|
|
"token": {api.token},
|
|
|
|
}
|
2022-04-25 14:50:10 -07:00
|
|
|
return api.doReminders(ctx, "reminders.list", values)
|
2020-10-19 14:40:00 -07:00
|
|
|
}
|
|
|
|
|
2019-01-31 08:06:36 -08:00
|
|
|
// AddChannelReminder adds a reminder for a channel.
|
2024-08-27 10:04:05 -07:00
|
|
|
// For more details, see AddChannelReminderContext documentation.
|
2019-01-31 08:06:36 -08:00
|
|
|
func (api *Client) AddChannelReminder(channelID, text, time string) (*Reminder, error) {
|
2022-04-25 14:50:10 -07:00
|
|
|
return api.AddChannelReminderContext(context.Background(), channelID, text, time)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddChannelReminderContext adds a reminder for a channel with a custom context
|
2024-08-27 10:04:05 -07:00
|
|
|
// NOTE: the ability to set reminders on a channel is currently undocumented but has been tested to work.
|
|
|
|
// Slack API docs: https://api.slack.com/methods/reminders.add
|
2022-04-25 14:50:10 -07:00
|
|
|
func (api *Client) AddChannelReminderContext(ctx context.Context, channelID, text, time string) (*Reminder, error) {
|
2019-01-31 08:06:36 -08:00
|
|
|
values := url.Values{
|
|
|
|
"token": {api.token},
|
|
|
|
"text": {text},
|
|
|
|
"time": {time},
|
|
|
|
"channel": {channelID},
|
|
|
|
}
|
2022-04-25 14:50:10 -07:00
|
|
|
return api.doReminder(ctx, "reminders.add", values)
|
2019-01-31 08:06:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddUserReminder adds a reminder for a user.
|
2024-08-27 10:04:05 -07:00
|
|
|
// For more details, see AddUserReminderContext documentation.
|
2019-01-31 08:06:36 -08:00
|
|
|
func (api *Client) AddUserReminder(userID, text, time string) (*Reminder, error) {
|
2022-04-25 14:50:10 -07:00
|
|
|
return api.AddUserReminderContext(context.Background(), userID, text, time)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddUserReminderContext adds a reminder for a user with a custom context
|
2024-08-27 10:04:05 -07:00
|
|
|
// Slack API docs: https://api.slack.com/methods/reminders.add
|
2022-04-25 14:50:10 -07:00
|
|
|
func (api *Client) AddUserReminderContext(ctx context.Context, userID, text, time string) (*Reminder, error) {
|
2019-01-31 08:06:36 -08:00
|
|
|
values := url.Values{
|
|
|
|
"token": {api.token},
|
|
|
|
"text": {text},
|
|
|
|
"time": {time},
|
|
|
|
"user": {userID},
|
|
|
|
}
|
2022-04-25 14:50:10 -07:00
|
|
|
return api.doReminder(ctx, "reminders.add", values)
|
2019-01-31 08:06:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteReminder deletes an existing reminder.
|
2024-08-27 10:04:05 -07:00
|
|
|
// For more details, see DeleteReminderContext documentation.
|
2019-01-31 08:06:36 -08:00
|
|
|
func (api *Client) DeleteReminder(id string) error {
|
2022-04-25 14:50:10 -07:00
|
|
|
return api.DeleteReminderContext(context.Background(), id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteReminderContext deletes an existing reminder with a custom context
|
2024-08-27 10:04:05 -07:00
|
|
|
// Slack API docs: https://api.slack.com/methods/reminders.delete
|
2022-04-25 14:50:10 -07:00
|
|
|
func (api *Client) DeleteReminderContext(ctx context.Context, id string) error {
|
2019-01-31 08:06:36 -08:00
|
|
|
values := url.Values{
|
|
|
|
"token": {api.token},
|
|
|
|
"reminder": {id},
|
|
|
|
}
|
|
|
|
response := &SlackResponse{}
|
2022-04-25 14:50:10 -07:00
|
|
|
if err := api.postMethod(ctx, "reminders.delete", values, response); err != nil {
|
2019-01-31 08:06:36 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return response.Err()
|
|
|
|
}
|