sendmsg

package module
v0.0.0-...-834dad9 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: Apache-2.0 Imports: 7 Imported by: 1

README

Message sender abstraction

Usage

go get github.com/demdxx/sendmsg
package main

import (
  "fmt"
  "log"

  "github.com/demdxx/sendmsg"
  "github.com/demdxx/sendmsg/email"
  "github.com/demdxx/sendmsg/template"
)

func main() {
  smtpSender, err := email.New(
    email.WithConfig(email.Config{
      URL:         "smtp.gmail.com",
      FromAddress: "hello@mail.com",
      FromName:    "Mailer",
      Password:    "password",
      Port:        587,
    }),
    // Default vars for all messages
    email.WithVars(map[string]any{
      "company": "My Awesome Company",
    }),
  )

  if err != nil {
    log.Fatal(err)
  }

  // ...
  templateStorage := template.NewDefaultStorage(registerTmpl, resetPasswordTmpl)
  messanger := sendmsg.NewDefaultMessanger(templateStorage).
                       RegisterSender("email", smtpSender)

  // ...
  err := messanger.Send(ctx,
    sendmsg.WithTemplate("register"),
    sendmsg.WithSender("email"),
    sendmsg.WithVars(map[string]any{
      "name": "Mr. Smith", 
      "email": "smith@mail.com",
    }),
  )
}

TODO

  • Add Telegram sender implementation
  • Add Slack sender implementation
  • Add WhatsApp sender implementation
  • Add Viber sender implementation
  • Add Facebook sender implementation
  • Add Equipos sender implementation
  • Add Line sender implementation
  • Add WeChat sender implementation
  • Add Skype sender implementation
  • Add Signal sender implementation
  • Add Discord sender implementation
  • Add Snapchat sender implementation
  • Add SMS sender implementation
  • Add EMail sender implementation

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTemplateStorageNotDefined = errors.New("template storage is not defined")
	ErrRecipientsNotDefined      = errors.New("recipients are not defined")
	ErrSubjectNotDefined         = errors.New("subject is not defined")
	ErrContentNotDefined         = errors.New("content is not defined")
	ErrNoSenders                 = errors.New("no senders defined")
	ErrMessageNotSent            = errors.New("message not sent")
	ErrTemplateNotFound          = template.ErrTemplateNotFound
)

Functions

This section is empty.

Types

type Attach

type Attach interface {
	Name() string
	ContentType() string
	Content() []byte
}

type DefaultMessage

type DefaultMessage struct {
	Recipients []string
	CC         []string
	BCC        []string
	Subject    string
	HTML       string
	PlainText  string
	Attaches   []Attach
	Vars       map[string]any
}

func (*DefaultMessage) Complete

func (m *DefaultMessage) Complete() error

func (*DefaultMessage) GetAttaches

func (m *DefaultMessage) GetAttaches() []Attach

func (*DefaultMessage) GetBCC

func (m *DefaultMessage) GetBCC() []string

func (*DefaultMessage) GetCC

func (m *DefaultMessage) GetCC() []string

func (*DefaultMessage) GetHTML

func (m *DefaultMessage) GetHTML(ctx context.Context, vars map[string]any) (string, error)

func (*DefaultMessage) GetPlainText

func (m *DefaultMessage) GetPlainText(ctx context.Context, vars map[string]any) (string, error)

func (*DefaultMessage) GetRecipients

func (m *DefaultMessage) GetRecipients() []string

func (*DefaultMessage) GetSubject

func (m *DefaultMessage) GetSubject(ctx context.Context, vars map[string]any) (string, error)

func (*DefaultMessage) GetVars

func (m *DefaultMessage) GetVars() map[string]any

type DefaultMessanger

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

DefaultMessanger implements sendmsg.Messanger interface

func NewDefaultMessanger

func NewDefaultMessanger(tmplStorage template.Storage) *DefaultMessanger

NewDefaultMessanger creates new messanger

func (*DefaultMessanger) RegisterSender

func (m *DefaultMessanger) RegisterSender(name string, sender Sender) *DefaultMessanger

RegisterSender registers new sender

func (*DefaultMessanger) Send

func (m *DefaultMessanger) Send(ctx context.Context, opts ...SendOption) error

Send sends the message to all senders that are allowed

type Message

type Message interface {
	GetRecipients() []string
	GetCC() []string
	GetBCC() []string

	GetSubject(ctx context.Context, vars map[string]any) (string, error)
	GetHTML(ctx context.Context, vars map[string]any) (string, error)
	GetPlainText(ctx context.Context, vars map[string]any) (string, error)
	GetAttaches() []Attach
	GetVars() map[string]any

	Complete() error
}

type Messanger

type Messanger interface {
	Send(ctx context.Context, opts ...SendOption) error
}

Messanger interface for sending messages into abstract channel

type SendOption

type SendOption func(*SendOptions)

func WithAttaches

func WithAttaches(attaches ...Attach) SendOption

WithAttaches sets the list of attachments

func WithContent

func WithContent(html, text string) SendOption

WithContent sets the content of the message

func WithMessage

func WithMessage(message Message) SendOption

WithMessage sets the message to send.

func WithRecipients

func WithRecipients(recipients, cc, bcc []string) SendOption

WithRecipients sets the list of recipients

func WithSender

func WithSender(senders ...string) SendOption

WithSender sets the list of allowed senders

func WithSubject

func WithSubject(subject string) SendOption

WithSubject sets the subject of the message

func WithTemplate

func WithTemplate(name string) SendOption

WithTemplate sets the template name to send.

func WithVars

func WithVars(vars map[string]any) SendOption

WithVars sets the list of variables

func WithoutSender

func WithoutSender(senders ...string) SendOption

WithoutSender sets the list of not allowed senders

type SendOptions

type SendOptions struct {
	AllowedSenders    []string
	NotAllowedSenders []string

	Message      Message
	TemplateName string
	Vars         map[string]any

	Recipients []string
	CC         []string
	BCC        []string

	Subject  string
	HTML     string
	Text     string
	Attaches []Attach
}

func (*SendOptions) GetMessage

func (o *SendOptions) GetMessage(storage template.Storage) (Message, error)

GetMessage returns the message to send

func (*SendOptions) IsAllowed

func (o *SendOptions) IsAllowed(sender string) bool

IsAllowed checks if the sender is allowed to send the message

type Sender

type Sender interface {
	Send(ctx context.Context, message Message) error
}

Sender is the interface for sending messages to specific message target like email, sms, etc.

type TemplateMessage

type TemplateMessage struct {
	Recipients []string
	CC         []string
	BCC        []string
	Template   itemplater
	Attaches   []Attach
	Vars       map[string]any
}

func (*TemplateMessage) Complete

func (m *TemplateMessage) Complete() error

func (*TemplateMessage) GetAttaches

func (m *TemplateMessage) GetAttaches() []Attach

func (*TemplateMessage) GetBCC

func (m *TemplateMessage) GetBCC() []string

func (*TemplateMessage) GetCC

func (m *TemplateMessage) GetCC() []string

func (*TemplateMessage) GetHTML

func (m *TemplateMessage) GetHTML(ctx context.Context, vars map[string]any) (string, error)

func (*TemplateMessage) GetPlainText

func (m *TemplateMessage) GetPlainText(ctx context.Context, vars map[string]any) (string, error)

func (*TemplateMessage) GetRecipients

func (m *TemplateMessage) GetRecipients() []string

func (*TemplateMessage) GetSubject

func (m *TemplateMessage) GetSubject(ctx context.Context, vars map[string]any) (string, error)

func (*TemplateMessage) GetVars

func (m *TemplateMessage) GetVars() map[string]any

Directories

Path Synopsis
internal
sender

Jump to

Keyboard shortcuts

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