-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathpushover.go
More file actions
63 lines (53 loc) · 1.67 KB
/
pushover.go
File metadata and controls
63 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package pushover
import (
"context"
"fmt"
"github.com/gregdel/pushover"
)
type pushoverClient interface {
SendMessage(*pushover.Message, *pushover.Recipient) (*pushover.Response, error)
}
// Compile-time check to ensure that pushover.Pushover implements the pushoverClient interface.
var _ pushoverClient = new(pushover.Pushover)
// Pushover struct holds necessary data to communicate with the Pushover API.
type Pushover struct {
client pushoverClient
recipients []pushover.Recipient
}
// New returns a new instance of a Pushover notification service.
// For more information about Pushover app token:
//
// -> https://support.pushover.net/i175-how-do-i-get-an-api-or-application-token
func New(appToken string) *Pushover {
client := pushover.New(appToken)
s := &Pushover{
client: client,
recipients: []pushover.Recipient{},
}
return s
}
// AddReceivers takes Pushover user/group IDs and adds them to the internal recipient list. The Send method will send
// a given message to all of those recipients.
func (p *Pushover) AddReceivers(recipientIDs ...string) {
for _, recipient := range recipientIDs {
p.recipients = append(p.recipients, *pushover.NewRecipient(recipient))
}
}
// Send takes a message subject and a message body and sends them to all previously set recipients.
func (p Pushover) Send(ctx context.Context, subject, message string) error {
for i := range p.recipients {
select {
case <-ctx.Done():
return ctx.Err()
default:
_, err := p.client.SendMessage(
pushover.NewMessageWithTitle(message, subject),
&p.recipients[i],
)
if err != nil {
return fmt.Errorf("send message to recipient %d: %w", i+1, err)
}
}
}
return nil
}