msgr

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 12 Imported by: 0

README

Fyro Messenger

Configuration

type ClientOpts struct {
	// Path to email layout, locales, and templates
	TemplatesRoot string
	DefaultFrom   string
	MailProvider  provider.MailProvider
	SMSProvider   provider.SMSProvider
	DefaultLocale string
	// Dynamic data to be used in the layout
	LayoutData MessageData
}

Create a client instance using:

msgr.NewClient(ClientOpts{})

Messages

Each message has its own templates and locales.

type AddMessageOpts struct {
	Name            string           // Must be unique
	MailChannelOpts *MailChannelOpts // Email channel options
}

Register messages using:

mailer.AddMessage(AddMessageOpts{})

Templates

File structure should be as follows:

templates
  layout_mail.html.tmpl // Root layout
	layout_sms.html.tmpl // Root layout
  locale.en.yml
  locale.zh-cn.yml
  [entryName]/
    index_mail.html.tmpl
		index_mail.text.tmpl
		index_sms.text.tmpl
    locale.en.yml
    locale.zh-cn.yml

Locale files should be named locale.[lang].yml

Subject

Message locale files must include a mandatory subject message entry, this is templated using data passed in.

# YAML
subject: Hello {{ .Name }}

Sending

type SendOpts struct {
	MessageName string
	MailTo      string // If MailTo is defined, it will send email
	SMSTo       string // If SMSTo is defined, it will send SMS
	Data        MessageData
	Locale      string
}

Example

Check example/preview.go for a working example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMessage = errors.New("invalid message")
	ErrNoProviders    = errors.New("no providers found")
	ErrInvalidFormat  = errors.New(`invalid format, needs to be "html" or "text"`)
)

Functions

func RenderHTML

func RenderHTML(opts RenderOpts) (string, error)

func RenderText

func RenderText(opts RenderOpts) (string, error)

Types

type AddMessageOpts

type AddMessageOpts struct {
	Name            string          // Must be unique
	MailChannelOpts MailChannelOpts // Email channel options
}

type Channel

type Channel string
const (
	MailChannel Channel = "mail"
	SMSChannel  Channel = "sms"
	PushChannel Channel = "push"
)

type ClientOpts

type ClientOpts struct {
	// Path to email layout, locales, and templates
	TemplatesRoot string
	// Set the mail provider and default opts
	MailProvider provider.MailProvider
	MailOpts     *MailChannelOpts
	// SMS options
	SMSProvider provider.SMSProvider
	// Push options
	PushProviders *provider.PushProviders
	DefaultLocale string
	// Fixed data to be used in the layout
	LayoutData MessageData
}

type ComposeMailOpts added in v0.0.5

type ComposeMailOpts struct {
	Message Message
	Locale  string
	Data    MessageData
}

type ComposePushOpts added in v0.0.5

type ComposePushOpts struct {
	Message Message
	Locale  string
	Data    MessageData
}

type ComposeSMSOpts added in v0.0.5

type ComposeSMSOpts struct {
	Message Message
	Locale  string
	Data    MessageData
}

type MailChannelOpts

type MailChannelOpts struct {
	From    string
	ReplyTo string
}

type MailContents added in v0.0.5

type MailContents struct {
	Subject  string
	HTMLBody string
	TextBody string
}

Mail composition

type Message

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

func NewMessage

func NewMessage(opts NewMessageOpts) (*Message, error)

func (*Message) Localizer

func (msg *Message) Localizer(locale string) *i18n.Localizer

func (*Message) TemplateFiles

func (msg *Message) TemplateFiles(channel Channel, format RenderFormat) []string

type MessageData

type MessageData map[string]any

type Messenger

type Messenger struct {
	LayoutData MessageData
	// contains filtered or unexported fields
}

func NewClient

func NewClient(opts ClientOpts) (*Messenger, error)

func (*Messenger) AddMessage

func (msgr *Messenger) AddMessage(opts AddMessageOpts) error

func (*Messenger) ComposeMail added in v0.0.5

func (msgr *Messenger) ComposeMail(opts ComposeMailOpts) (*MailContents, error)

func (*Messenger) ComposePush added in v0.0.5

func (msgr *Messenger) ComposePush(opts ComposePushOpts) (*PushContents, error)

func (*Messenger) ComposeSMS added in v0.0.5

func (msgr *Messenger) ComposeSMS(opts ComposeSMSOpts) (*SMSContents, error)

SMS composition

func (*Messenger) GetMessage added in v0.0.5

func (msgr *Messenger) GetMessage(name string) (*Message, error)

func (*Messenger) LayoutFile

func (msgr *Messenger) LayoutFile(channel Channel, format RenderFormat) string

func (*Messenger) Send

func (msgr *Messenger) Send(opts SendOpts) error

type NewMessageOpts

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

type PushContents added in v0.0.5

type PushContents struct {
	Title string
	Body  string
}

Push composition

type RenderFormat

type RenderFormat string
var (
	RenderKindText RenderFormat = "text"
	RenderKindHTML RenderFormat = "html"
)

type RenderOpts

type RenderOpts struct {
	Templates     []string
	Data          MessageData
	Locale        string
	LayoutBundle  *i18n.Bundle
	MessageBundle *i18n.Bundle
}

type SMSContents added in v0.0.5

type SMSContents struct {
	Body string
}

type SendOpts

type SendOpts struct {
	MessageName string
	MailTo      string                // If MailTo is defined, it will send email
	SMSTo       string                // If SMSTo is defined, it will send SMS
	PushTo      []provider.PushDevice // If pushTo has devices, it will send via push
	Data        MessageData
	Locale      string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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