-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): Add Reddit service (#471)
- Loading branch information
Showing
8 changed files
with
281 additions
and
0 deletions.
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
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,16 @@ | ||
# Reddit Usage | ||
|
||
Ensure that you have already navigated to your GOPATH and installed the following packages: | ||
|
||
* `go get -u github.com/nikoksr/notify` | ||
|
||
## Steps for Reddit notifications | ||
|
||
These are general and very high level instructions | ||
|
||
1. Log into Reddit create a new "script" type by visiting [here](https://www.reddit.com/prefs/apps/) | ||
2. The "redirect uri" parameter doesn't matter in this case and can just be set to `http://localhost:8080` | ||
2. Copy the *client id* and *client secret* for usage below | ||
4. Now you should be good to use the code detailed in [doc.go](doc.go) | ||
|
||
**NOTE**: You may have difficulties using your user's password if you have 2FA enabled. You can disable it by going [here](https://www.reddit.com/prefs/update/) but be aware of the security implications and ensure you have a strong password set. |
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,37 @@ | ||
/* | ||
Package reddit implements a Reddit notifier, allowing messages to be sent to multiple recipients | ||
Usage: | ||
package main | ||
import ( | ||
"context" | ||
"github.com/nikoksr/notify" | ||
"github.com/nikoksr/notify/service/reddit" | ||
) | ||
func main() { | ||
notifier := notify.New() | ||
// Provide your Reddit app credentials and username/password | ||
redditService := reddit.New("clientID", "clientSecret", "username", "password") | ||
// Pass the usernames for where to send the messages | ||
pushoverService.AddReceivers("User1", "User2") | ||
// Tell our notifier to use the Reddit service. You can repeat the above process | ||
// for as many services as you like and just tell the notifier to use them. | ||
notifier.UseServices(redditService) | ||
// Send a message | ||
_ = notifier.Send( | ||
context.Background(), | ||
"Hello!", | ||
"I am a bot written in Go!", | ||
) | ||
} | ||
*/ | ||
package reddit |
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,87 @@ | ||
// Package reddit implements a Reddit notifier, allowing messages to be sent to multiple recipients | ||
package reddit | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/vartanbeno/go-reddit/v2/reddit" | ||
) | ||
|
||
//go:generate mockery --name=redditMessageClient --output=. --case=underscore --inpackage | ||
type redditMessageClient interface { | ||
Send(context.Context, *reddit.SendMessageRequest) (*reddit.Response, error) | ||
} | ||
|
||
// Compile-time check to ensure that reddit.MessageService implements the redditMessageClient interface. | ||
var _ redditMessageClient = new(reddit.MessageService) | ||
|
||
// Reddit struct holds necessary data to communicate with the Reddit API. | ||
type Reddit struct { | ||
client redditMessageClient | ||
recipients []string | ||
} | ||
|
||
// New returns a new instance of a Reddit notification service. | ||
// For more information on obtaining client credentials: | ||
// | ||
// -> https://github.com/reddit-archive/reddit/wiki/OAuth2 | ||
func New(clientID, clientSecret, username, password string) (*Reddit, error) { | ||
// Disable HTTP2 in http client | ||
// Details: https://www.reddit.com/r/redditdev/comments/t8e8hc/getting_nothing_but_429_responses_when_using_go/i18yga2/ | ||
h := http.Client{ | ||
Transport: &http.Transport{ | ||
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{}, | ||
}, | ||
} | ||
rClient, err := reddit.NewClient( | ||
reddit.Credentials{ | ||
ID: clientID, | ||
Secret: clientSecret, | ||
Username: username, | ||
Password: password, | ||
}, | ||
reddit.WithHTTPClient(&h), | ||
reddit.WithUserAgent("github.com/nikoksr/notify"), | ||
) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to instantiate base Reddit client") | ||
} | ||
|
||
r := &Reddit{ | ||
client: rClient.Message, | ||
recipients: []string{}, | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
// AddReceivers takes Reddit usernames and adds them to the internal recipient list. The Send method will send | ||
// a given message to all of those users. | ||
func (r *Reddit) AddReceivers(recipients ...string) { | ||
r.recipients = append(r.recipients, recipients...) | ||
} | ||
|
||
// Send takes a message subject and a message body and sends them to all previously set recipients. | ||
func (r *Reddit) Send(ctx context.Context, subject, message string) error { | ||
for i := range r.recipients { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
m := reddit.SendMessageRequest{ | ||
To: r.recipients[i], | ||
Subject: subject, | ||
Text: message, | ||
} | ||
|
||
_, err := r.client.Send(ctx, &m) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to send message to Reddit recipient '%s'", r.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 reddit | ||
|
||
import ( | ||
context "context" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/vartanbeno/go-reddit/v2/reddit" | ||
) | ||
|
||
func TestReddit_New(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service, err := New("id", "secret", "user", "password") | ||
assert.NotNil(service) | ||
assert.NoError(err) | ||
} | ||
|
||
func TestReddit_AddReceivers(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service, err := New("id", "secret", "user", "password") | ||
assert.NotNil(service) | ||
assert.NoError(err) | ||
|
||
service.AddReceivers("") | ||
assert.Len(service.recipients, 1) | ||
|
||
service.AddReceivers("", "") | ||
assert.Len(service.recipients, 3) | ||
} | ||
|
||
func TestReddit_Send(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := require.New(t) | ||
|
||
service, err := New("id", "secret", "user", "password") | ||
assert.NotNil(service) | ||
assert.NoError(err) | ||
|
||
// No receivers added | ||
ctx := context.Background() | ||
err = service.Send(ctx, "subject", "message") | ||
assert.Nil(err) | ||
|
||
// Test error response | ||
mockClient := newMockRedditMessageClient(t) | ||
mockClient. | ||
On("Send", ctx, mock.AnythingOfType("*reddit.SendMessageRequest")). | ||
Return(&reddit.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 = newMockRedditMessageClient(t) | ||
mockClient. | ||
On("Send", ctx, mock.AnythingOfType("*reddit.SendMessageRequest")). | ||
Return(&reddit.Response{}, nil) | ||
|
||
service.client = mockClient | ||
service.AddReceivers("5678") | ||
err = service.Send(ctx, "subject", "message") | ||
assert.Nil(err) | ||
mockClient.AssertExpectations(t) | ||
} |