-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #456
- Loading branch information
Showing
7 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Package pushover implements a Pushover notifier, allowing messages to be sent to multiple recipients and supports | ||
// both users and groups. | ||
package pushover | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gregdel/pushover" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
//go:generate mockery --name=pushoverClient --output=. --case=underscore --inpackage | ||
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 errors.Wrapf(err, "failed to send message to Pushover recipient '%s'", p.recipients[i]) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package pushover | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gregdel/pushover" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPushover_New(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service := New("") | ||
assert.NotNil(service) | ||
} | ||
|
||
func TestPushover_AddReceivers(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service := New("") | ||
assert.NotNil(service) | ||
|
||
service.AddReceivers("") | ||
assert.Len(service.recipients, 1) | ||
|
||
service.AddReceivers("", "") | ||
assert.Len(service.recipients, 3) | ||
} | ||
|
||
func TestPushover_Send(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service := New("") | ||
assert.NotNil(service) | ||
|
||
// No receivers added | ||
ctx := context.Background() | ||
err := service.Send(ctx, "subject", "message") | ||
assert.Nil(err) | ||
|
||
// Test error response | ||
mockClient := newMockPushoverClient(t) | ||
mockClient. | ||
On("SendMessage", &pushover.Message{Title: "subject", Message: "message"}, pushover.NewRecipient("1234")). | ||
Return(&pushover.Response{}, errors.New("some error")) | ||
|
||
service.client = mockClient | ||
service.AddReceivers("1234") | ||
err = service.Send(ctx, "subject", "message") | ||
assert.NotNil(err) | ||
mockClient.AssertExpectations(t) | ||
|
||
// Test success response | ||
mockClient = newMockPushoverClient(t) | ||
mockClient. | ||
On("SendMessage", &pushover.Message{Title: "subject", Message: "message"}, pushover.NewRecipient("1234")). | ||
Return(&pushover.Response{}, nil) | ||
|
||
mockClient. | ||
On("SendMessage", &pushover.Message{Title: "subject", Message: "message"}, pushover.NewRecipient("5678")). | ||
Return(&pushover.Response{}, nil) | ||
|
||
service.client = mockClient | ||
service.AddReceivers("5678") | ||
err = service.Send(ctx, "subject", "message") | ||
assert.Nil(err) | ||
mockClient.AssertExpectations(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Pushover Usage | ||
|
||
Ensure that you have already navigated to your GOPATH and installed the following packages: | ||
|
||
* `go get -u github.com/nikoksr/notify` | ||
|
||
## Steps for Pushover App | ||
|
||
These are general and very high level instructions | ||
|
||
1. Create a new Pushover App by visiting [here](https://pushover.net/apps/build) | ||
2. Copy your *App token* for usage below | ||
3. Copy the *User ID* or *Group ID* for where you'd like to send messages | ||
4. Now you should be good to use the code below | ||
|
||
## Sample Code | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/nikoksr/notify" | ||
"github.com/nikoksr/notify/service/pushover" | ||
) | ||
|
||
func main() { | ||
|
||
notifier := notify.New() | ||
|
||
// Provide your Pushover App token | ||
pushoverService := pushover.New("APP_TOKEN") | ||
|
||
// Pass user and/or group IDs for where to send the messages | ||
pushoverService.AddReceivers("USER_ID", "GROUP_ID") | ||
|
||
// Tell our notifier to use the Pushover service. You can repeat the above process | ||
// for as many services as you like and just tell the notifier to use them. | ||
notifier.UseServices(pushoverService) | ||
|
||
// Send a message | ||
_ = notifier.Send( | ||
context.Background(), | ||
"Hello!", | ||
"I am a bot written in Go!", | ||
) | ||
} | ||
``` |