matterbridge/vendor/github.com/slack-go/slack/reminders.go

105 lines
2.9 KiB
Go
Raw Normal View History

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"`
}
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
}
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 {
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))
for _, reminder := range response.Reminders {
2021-03-20 14:40:23 -07:00
reminders = append(reminders, reminder)
2020-10-19 14:40:00 -07:00
}
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
// reminders on a channel is currently undocumented but has been tested to
// work)
func (api *Client) AddChannelReminder(channelID, text, time string) (*Reminder, error) {
values := url.Values{
"token": {api.token},
"text": {text},
"time": {time},
"channel": {channelID},
}
return api.doReminder(context.Background(), "reminders.add", values)
}
// AddUserReminder adds a reminder for a user.
//
// See https://api.slack.com/methods/reminders.add (NOTE: the ability to set
// reminders on a channel is currently undocumented but has been tested to
// work)
func (api *Client) AddUserReminder(userID, text, time string) (*Reminder, error) {
values := url.Values{
"token": {api.token},
"text": {text},
"time": {time},
"user": {userID},
}
return api.doReminder(context.Background(), "reminders.add", values)
}
// DeleteReminder deletes an existing reminder.
//
// See https://api.slack.com/methods/reminders.delete
func (api *Client) DeleteReminder(id string) error {
values := url.Values{
"token": {api.token},
"reminder": {id},
}
response := &SlackResponse{}
2019-09-07 13:46:58 -07:00
if err := api.postMethod(context.Background(), "reminders.delete", values, response); err != nil {
return err
}
return response.Err()
}