Documentation
¶
Overview ¶
Package contracts defines the interfaces and data types for message providers.
This package contains:
- Message types (Email, SMS, PushNotification, ChatMessage)
- Sender interfaces (EmailSender, SMSSender, PushSender, ChatSender)
- Common types (Attachment, SendResult)
Interface Design ¶
All sender interfaces follow a consistent pattern:
type XxxSender interface {
Send(ctx context.Context, message *Xxx) (*SendResult, error)
Name() string
}
This design allows:
- Easy provider switching
- Consistent error handling
- Context-based timeout/cancellation
Usage ¶
func sendWelcomeEmail(sender contracts.EmailSender) error {
_, err := sender.Send(ctx, &contracts.Email{
To: []string{"user@example.com"},
Subject: "Welcome!",
HTML: "<h1>Hello!</h1>",
})
return err
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attachment ¶
type Attachment struct {
Filename string // Name of the file
ContentType string // MIME type of the file
Data []byte // File content
}
Attachment represents a file attachment for messages
type ChatButton ¶
type ChatButton struct {
ID string // Button identifier
Text string // Button display text
URL string // Optional: URL for link buttons
Phone string // Optional: phone number for call buttons
}
ChatButton represents an interactive button in a chat message
type ChatMessage ¶
type ChatMessage struct {
From string // Sender ID or phone number
To []string // Recipient IDs or phone numbers
Message string // Text message content
TemplateID string // Optional: template ID for WhatsApp Business API
TemplateParams []string // Optional: template parameters
MediaURL string // Optional: URL to media (image, video, document)
MediaType string // Optional: "image", "video", "audio", "document"
Buttons []ChatButton // Optional: interactive buttons
ReplyToID string // Optional: message ID to reply to
Metadata map[string]string // Optional: custom metadata
}
ChatMessage represents a message to be sent via chat/social platforms (WhatsApp, Telegram, Facebook Messenger, etc.)
type ChatSender ¶
type ChatSender interface {
// Send sends a chat message and returns the result
Send(ctx context.Context, message *ChatMessage) (*SendResult, error)
// Name returns the provider name (e.g., "whatsapp", "telegram")
Name() string
}
ChatSender defines the contract for sending chat/social media messages
type Email ¶
type Email struct {
From string // Sender email address
FromName string // Sender display name
To []string // Recipients
CC []string // Carbon copy recipients
BCC []string // Blind carbon copy recipients
ReplyTo string // Reply-to address
Subject string // Email subject
HTML string // HTML body content
PlainText string // Plain text body content
Attachments []Attachment // File attachments
Headers map[string]string // Custom email headers
}
Email represents an email message to be sent
type EmailSender ¶
type EmailSender interface {
// Send sends an email and returns the result
Send(ctx context.Context, email *Email) (*SendResult, error)
// Name returns the provider name (e.g., "mailgun", "sendgrid")
Name() string
}
EmailSender defines the contract for sending emails
type PushNotification ¶
type PushNotification struct {
DeviceTokens []string // Target device tokens
Title string // Notification title
Body string // Notification body
Data map[string]string // Custom data payload
Badge *int // Badge count (optional)
Sound string // Notification sound (optional)
}
PushNotification represents a push notification to be sent
type PushSender ¶
type PushSender interface {
// Send sends a push notification and returns the result
Send(ctx context.Context, notification *PushNotification) (*SendResult, error)
// Name returns the provider name (e.g., "firebase", "apns")
Name() string
}
PushSender defines the contract for sending push notifications
type SMS ¶
type SMS struct {
From string // Sender phone number or alphanumeric ID
To []string // Recipient phone numbers (E.164 format recommended)
Message string // SMS content (max 160 chars for single SMS)
}
SMS represents an SMS message to be sent
type SMSSender ¶
type SMSSender interface {
// Send sends an SMS and returns the result
Send(ctx context.Context, sms *SMS) (*SendResult, error)
// Name returns the provider name (e.g., "twilio", "cmcom")
Name() string
}
SMSSender defines the contract for sending SMS messages
type SendResult ¶
type SendResult struct {
ID string // Provider-specific message ID
StatusCode int // HTTP status code from the provider
Message string // Human-readable status message
Meta map[string]string // Additional provider-specific metadata
}
SendResult represents the result of sending a message