From 8237b783656ee30865b5577294005c114a395d3e Mon Sep 17 00:00:00 2001 From: Haris Chaniotakis Date: Wed, 10 Feb 2021 17:20:40 +0200 Subject: [PATCH 1/5] feat(service): Add SendGrid service --- go.mod | 3 +- go.sum | 4 +++ service/sendgrid/sendgrid.go | 70 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 service/sendgrid/sendgrid.go diff --git a/go.mod b/go.mod index 4ba4c560..9a1bac87 100644 --- a/go.mod +++ b/go.mod @@ -9,10 +9,11 @@ require ( github.com/dghubble/go-twitter v0.0.0-20201011215211-4b180d0cc78d github.com/dghubble/oauth1 v0.7.0 github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible - github.com/google/go-querystring v1.0.0 // indirect github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible github.com/pkg/errors v0.9.1 github.com/plivo/plivo-go v5.5.1+incompatible + github.com/sendgrid/rest v2.6.2+incompatible // indirect + github.com/sendgrid/sendgrid-go v3.7.2+incompatible github.com/sirupsen/logrus v1.7.0 // indirect github.com/slack-go/slack v0.8.0 github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 85e934cd..5620b389 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,10 @@ github.com/plivo/plivo-go v5.5.1+incompatible h1:LtZaUNHjSrNzBCHAe/IdDBnLGlyZB+W github.com/plivo/plivo-go v5.5.1+incompatible/go.mod h1:OhnI9crdl6O+D94Lp1lvuwJoA3KUH39J6IM+j3HwCBE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sendgrid/rest v2.6.2+incompatible h1:zGMNhccsPkIc8SvU9x+qdDz2qhFoGUPGGC4mMvTondA= +github.com/sendgrid/rest v2.6.2+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE= +github.com/sendgrid/sendgrid-go v3.7.2+incompatible h1:ePQr9ns8so+28whk+gLKRYiyI5IiCESkDIqy7cjiwLg= +github.com/sendgrid/sendgrid-go v3.7.2+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8= github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/slack-go/slack v0.8.0 h1:ANyLY5KHLV+MxLJDQum2IuHTLwbCbDtaWY405X1EU9U= diff --git a/service/sendgrid/sendgrid.go b/service/sendgrid/sendgrid.go new file mode 100644 index 00000000..3e043cb9 --- /dev/null +++ b/service/sendgrid/sendgrid.go @@ -0,0 +1,70 @@ +package sendgrid + +import ( + "net/http" + + "github.com/pkg/errors" + + "github.com/sendgrid/sendgrid-go" + "github.com/sendgrid/sendgrid-go/helpers/mail" +) + +// SendGrid struct holds necessary data to communicate with the Slack API. +type SendGrid struct { + client *sendgrid.Client + senderAddress string + senderName string + receiverAddresses []string +} + +// New returns a new instance of a SendGrid notification service. +// You will need a SendGrid API key. +// See https://sendgrid.com/docs/for-developers/sending-email/api-getting-started/ +func New(apiKey, senderAddress, senderName string) *SendGrid { + client := sendgrid.NewSendClient(apiKey) + + return &SendGrid{ + client: client, + senderAddress: senderAddress, + senderName: senderName, + receiverAddresses: []string{}, + } +} + +// AddReceivers takes email addresses and adds them to the internal address list. The Send method will send +// a given message to all those addresses. +func (s *SendGrid) AddReceivers(addresses ...string) { + s.receiverAddresses = append(s.receiverAddresses, addresses...) +} + +// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports +// html as markup language. +func (s SendGrid) Send(subject, message string) error { + from := mail.NewEmail(s.senderName, s.senderAddress) + c := mail.NewContent("text/html", message) + + // Create a new personalization instance to be able to add multiple receiver addresses. + p := mail.NewPersonalization() + p.Subject = subject + + for _, receiverAddress := range s.receiverAddresses { + receiverEmail := mail.NewEmail(receiverAddress, receiverAddress) + p.AddTos(receiverEmail) + } + + m := mail.NewV3Mail() + m.AddPersonalizations(p) + m.AddContent(c) + m.SetFrom(from) + + resp, err := s.client.Send(m) + if err != nil { + return errors.Wrap(err, "failed to send mail using SendGrid service") + } + + if resp.StatusCode != http.StatusAccepted { + return errors.New("failed to send mail using SendGrid service") + } + + return nil +} From 77f04b507863b5d0aef0a600389a7a1c8046b657 Mon Sep 17 00:00:00 2001 From: Haris Chaniotakis Date: Wed, 10 Feb 2021 17:22:58 +0200 Subject: [PATCH 2/5] docs: Update README with new supported SendGrid service --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8d305e2b..cfc0c3ef 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ _ = notifier.Send( - *Microsoft Teams* - *Plivo* - *Pushbullet* +- *SendGrid* - *Slack* - *Telegram* - *Twitter* From 3c21a6eed8aeb0916c54638db6e08f785b3c48f9 Mon Sep 17 00:00:00 2001 From: Haris Chaniotakis Date: Wed, 10 Feb 2021 17:30:43 +0200 Subject: [PATCH 3/5] fix: fix comment typo on SendGrid struct --- service/sendgrid/sendgrid.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/sendgrid/sendgrid.go b/service/sendgrid/sendgrid.go index 3e043cb9..690e1a38 100644 --- a/service/sendgrid/sendgrid.go +++ b/service/sendgrid/sendgrid.go @@ -9,7 +9,7 @@ import ( "github.com/sendgrid/sendgrid-go/helpers/mail" ) -// SendGrid struct holds necessary data to communicate with the Slack API. +// SendGrid struct holds necessary data to communicate with the SendGrid API. type SendGrid struct { client *sendgrid.Client senderAddress string From 82bdb092f0e4bb1c7c58cacafd33e8e6b8f0d9ed Mon Sep 17 00:00:00 2001 From: Haris Chaniotakis Date: Fri, 12 Feb 2021 10:29:23 +0200 Subject: [PATCH 4/5] refactor: remove unnecessary variable assignments --- service/sendgrid/sendgrid.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/service/sendgrid/sendgrid.go b/service/sendgrid/sendgrid.go index 690e1a38..ecc4be59 100644 --- a/service/sendgrid/sendgrid.go +++ b/service/sendgrid/sendgrid.go @@ -21,10 +21,8 @@ type SendGrid struct { // You will need a SendGrid API key. // See https://sendgrid.com/docs/for-developers/sending-email/api-getting-started/ func New(apiKey, senderAddress, senderName string) *SendGrid { - client := sendgrid.NewSendClient(apiKey) - return &SendGrid{ - client: client, + client: sendgrid.NewSendClient(apiKey), senderAddress: senderAddress, senderName: senderName, receiverAddresses: []string{}, @@ -48,8 +46,7 @@ func (s SendGrid) Send(subject, message string) error { p.Subject = subject for _, receiverAddress := range s.receiverAddresses { - receiverEmail := mail.NewEmail(receiverAddress, receiverAddress) - p.AddTos(receiverEmail) + p.AddTos(mail.NewEmail(receiverAddress, receiverAddress)) } m := mail.NewV3Mail() From 2f93b480421b0ce471696f2f80846f2accba93d9 Mon Sep 17 00:00:00 2001 From: Haris Chaniotakis Date: Fri, 12 Feb 2021 19:08:34 +0200 Subject: [PATCH 5/5] refactor: proper variable naming --- service/sendgrid/sendgrid.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/service/sendgrid/sendgrid.go b/service/sendgrid/sendgrid.go index ecc4be59..77198044 100644 --- a/service/sendgrid/sendgrid.go +++ b/service/sendgrid/sendgrid.go @@ -39,28 +39,28 @@ func (s *SendGrid) AddReceivers(addresses ...string) { // html as markup language. func (s SendGrid) Send(subject, message string) error { from := mail.NewEmail(s.senderName, s.senderAddress) - c := mail.NewContent("text/html", message) + content := mail.NewContent("text/html", message) // Create a new personalization instance to be able to add multiple receiver addresses. - p := mail.NewPersonalization() - p.Subject = subject + personalization := mail.NewPersonalization() + personalization.Subject = subject for _, receiverAddress := range s.receiverAddresses { - p.AddTos(mail.NewEmail(receiverAddress, receiverAddress)) + personalization.AddTos(mail.NewEmail(receiverAddress, receiverAddress)) } - m := mail.NewV3Mail() - m.AddPersonalizations(p) - m.AddContent(c) - m.SetFrom(from) + mailMessage := mail.NewV3Mail() + mailMessage.AddPersonalizations(personalization) + mailMessage.AddContent(content) + mailMessage.SetFrom(from) - resp, err := s.client.Send(m) + resp, err := s.client.Send(mailMessage) if err != nil { return errors.Wrap(err, "failed to send mail using SendGrid service") } if resp.StatusCode != http.StatusAccepted { - return errors.New("failed to send mail using SendGrid service") + return errors.New("the SendGrid endpoint did not accept the message") } return nil