twilio

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package twilio delivers AuthKit's emails through Twilio SendGrid and its text messages through Twilio Messaging.

email, err := twilio.NewEmail(twilio.EmailConfig{
	APIKey:    os.Getenv("SENDGRID_API_KEY"),
	FromEmail: "hello@myapp.com",
	AppName:   "MyApp",
})
if err != nil {
	return err
}
sms, err := twilio.NewSMS(twilio.SMSConfig{
	AccountSID:          os.Getenv("TWILIO_ACCOUNT_SID"),
	AuthToken:           os.Getenv("TWILIO_AUTH_TOKEN"),
	MessagingServiceSID: os.Getenv("TWILIO_MESSAGING_SERVICE_SID"),
	AppName:             "MyApp",
})
if err != nil {
	return err
}
deps := authkit.Deps{Postgres: db, Email: email, SMS: sms}

Each sender renders every message kind from a built-in template in the message's Language (Spanish for "es", else English). Set Render to supply your own content for some kinds; returning false falls back to the built-in one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Email

type Email struct {
	// contains filtered or unexported fields
}

Email delivers AuthKit's emails through the Twilio SendGrid Mail Send API. It is an authkit.EmailSender: wire it as authkit.Deps.Email.

func NewEmail

func NewEmail(cfg EmailConfig) (*Email, error)

NewEmail validates cfg and returns an Email.

func (*Email) CheckHealth added in v0.149.0

func (e *Email) CheckHealth(ctx context.Context) error

CheckHealth verifies, without sending an email, that this sender can deliver: the API key is valid and has the mail.send scope, and FromEmail is on a valid authenticated domain or is a verified sender. A key that can't read its domains and senders can't tell the second part, which counts as healthy.

func (*Email) Send

func (e *Email) Send(ctx context.Context, msg iam.EmailMessage) error

Send renders msg (Render, else the built-in template for msg.Kind) and delivers it.

type EmailConfig

type EmailConfig struct {
	// APIKey is a Twilio SendGrid API key.
	APIKey    string
	FromEmail string
	FromName  string
	// AppName names the app in the built-in copy (default "Auth").
	AppName string
	// Client sends the API requests (default: a 10s-timeout client).
	Client *http.Client
	// Categories and CustomArgs tag every email.
	Categories []string
	CustomArgs map[string]string
	// Render returns the content for msg, or false to use the built-in
	// template for msg.Kind.
	Render func(ctx context.Context, msg iam.EmailMessage) (EmailContent, bool)
}

EmailConfig configures NewEmail. APIKey and FromEmail are required.

type EmailContent

type EmailContent struct {
	Subject string
	Text    string
	HTML    string
	// Categories and CustomArgs are added to EmailConfig's.
	Categories []string
	CustomArgs map[string]string
}

EmailContent is one rendered email.

type SMS

type SMS struct {
	// contains filtered or unexported fields
}

SMS delivers AuthKit's text messages through the Twilio Messaging API and confirms delivery before Send returns. It is an authkit.SMSSender: wire it as authkit.Deps.SMS.

func NewSMS

func NewSMS(cfg SMSConfig) (*SMS, error)

NewSMS validates cfg and returns an SMS.

func (*SMS) CheckHealth

func (s *SMS) CheckHealth(ctx context.Context) error

CheckHealth verifies, without sending an SMS, that this sender can deliver: the credentials work, the Messaging Service exists and has an attached sender, and any toll-free sender has completed Twilio toll-free verification (the silent failure behind error 30032).

func (*SMS) Send

func (s *SMS) Send(ctx context.Context, msg iam.SMSMessage) error

Send renders msg (Render, else the built-in template for msg.Kind), sends it and waits up to 12s for a delivery verdict: a definite failure is an error, a message still in flight is not.

type SMSConfig

type SMSConfig struct {
	AccountSID          string
	AuthToken           string
	MessagingServiceSID string
	// AppName names the app in the built-in copy (default "Auth").
	AppName string
	// Client sends the API requests (default: a 10s-timeout client).
	Client *http.Client
	// DeliveryPollInterval is the gap between delivery status polls (default
	// 750ms).
	DeliveryPollInterval time.Duration
	// Render returns the body for msg, or false to use the built-in template
	// for msg.Kind.
	Render func(ctx context.Context, msg iam.SMSMessage) (string, bool)
}

SMSConfig configures NewSMS. AccountSID, AuthToken and MessagingServiceSID are required.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL