-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial wrappers to email and sms notifications via sendgrid and twilio
- Loading branch information
1 parent
9b1bb20
commit 55c4a21
Showing
5 changed files
with
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package notifications | ||
|
||
import "context" | ||
|
||
type Notification struct { | ||
To string | ||
Subject string | ||
Body string | ||
} | ||
|
||
type NotificationService interface { | ||
Init(conf any) error | ||
SendNotification(context.Context, *Notification) error | ||
} |
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,44 @@ | ||
package sendgrid | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sendgrid/sendgrid-go" | ||
"github.com/sendgrid/sendgrid-go/helpers/mail" | ||
"github.com/vocdoni/saas-backend/notifications" | ||
) | ||
|
||
type SendGridConfig struct { | ||
FromName string | ||
FromAddress string | ||
APIKey string | ||
} | ||
|
||
type SendGridEmail struct { | ||
config *SendGridConfig | ||
client *sendgrid.Client | ||
} | ||
|
||
func (sg *SendGridEmail) Init(rawConfig any) error { | ||
// parse configuration | ||
config, ok := rawConfig.(*SendGridConfig) | ||
if !ok { | ||
return fmt.Errorf("invalid SendGrid configuration") | ||
} | ||
// set configuration in struct | ||
sg.config = config | ||
// init SendGrid client | ||
sg.client = sendgrid.NewSendClient(sg.config.APIKey) | ||
return nil | ||
} | ||
|
||
func (sg *SendGridEmail) SendNotification(ctx context.Context, notification *notifications.Notification) error { | ||
// create from email | ||
from := mail.NewEmail(sg.config.FromName, sg.config.FromAddress) | ||
// create email with notification data | ||
message := mail.NewSingleEmail(from, notification.Subject, mail.NewEmail("", notification.To), notification.Body, notification.Body) | ||
Check failure on line 40 in notifications/sendgrid/email.go GitHub Actions / lint
|
||
// send the email | ||
_, err := sg.client.SendWithContext(ctx, message) | ||
return err | ||
} |
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,58 @@ | ||
package twilio | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
t "github.com/twilio/twilio-go" | ||
api "github.com/twilio/twilio-go/rest/api/v2010" | ||
"github.com/vocdoni/saas-backend/notifications" | ||
) | ||
|
||
const ( | ||
AccountSidEnv = "TWILIO_ACCOUNT_SID" | ||
AuthTokenEnv = "TWILIO_AUTH_TOKEN" | ||
) | ||
|
||
type TwilioConfig struct { | ||
AccountSid string | ||
AuthToken string | ||
FromNumber string | ||
} | ||
|
||
type TwilioSMS struct { | ||
config *TwilioConfig | ||
client *t.RestClient | ||
} | ||
|
||
func (tsms *TwilioSMS) Init(rawConfig any) error { | ||
// parse configuration | ||
config, ok := rawConfig.(*TwilioConfig) | ||
if !ok { | ||
return fmt.Errorf("invalid Twilio configuration") | ||
} | ||
// set configuration in struct | ||
tsms.config = config | ||
// set account SID and auth token as environment variables | ||
if err := os.Setenv(AccountSidEnv, tsms.config.AccountSid); err != nil { | ||
return err | ||
} | ||
if err := os.Setenv(AuthTokenEnv, tsms.config.AuthToken); err != nil { | ||
return err | ||
} | ||
// init Twilio REST client | ||
tsms.client = t.NewRestClient() | ||
return nil | ||
} | ||
|
||
func (tsms *TwilioSMS) SendNotification(_ context.Context, notification *notifications.Notification) error { | ||
// create message with configured sender number and notification data | ||
params := &api.CreateMessageParams{} | ||
params.SetTo(notification.To) | ||
params.SetFrom(tsms.config.FromNumber) | ||
params.SetBody(notification.Body) | ||
// send the message | ||
_, err := tsms.client.Api.CreateMessage(params) | ||
return err | ||
} |