notifiers

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: MIT Imports: 20 Imported by: 3

README

notifiers Go Report Card test GoDoc

multi notifiers adaptor

Features

  • Telegram
  • Mail by mailgun
  • Dingding
  • Printer
  • Combine(combine multi notifiers to one)
  • Limiter(add a interval between messages)

License

MIT © zcong1993

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotify = errors.New("notify error")

ErrNotify is notify error.

Functions

This section is empty.

Types

type Combine

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

Combine impl notifier, can combine multi notifiers to one.

func NewCombine

func NewCombine(notifiers ...Notifier) *Combine

NewCombine create a instance from other notifiers.

func (*Combine) Close

func (c *Combine) Close() error

Close impl Notifier.Close. It return multierr, use multierr.Errors unwrap to multi errors slice.

func (*Combine) GetName

func (c *Combine) GetName() string

GetName impl Notifier.GetName. Name is "combine " + inner notifier names.

func (*Combine) Notify

func (c *Combine) Notify(ctx context.Context, to string, msg Message) error

Notify impl Notifier.Notify. Call inner notifiers parallelly, and return a multierr, use multierr.Errors unwrap to multi errors slice.

func (*Combine) Wait added in v2.1.0

func (c *Combine) Wait()

Wait impl Notifier.Wait.

type Ding

type Ding struct {
	NoopCloser
	NoopWaiter
	// contains filtered or unexported fields
}

Ding impl notifier, notify msg by Dingding webhook.

func NewDing

func NewDing(webhook string, secret string) *Ding

NewDing create a instance.

func (*Ding) GetName

func (d *Ding) GetName() string

GetName impl Notifier.GetName.

func (*Ding) Notify

func (d *Ding) Notify(ctx context.Context, to string, msg Message) error

Notify impl Notifier.Notify.

type Limiter

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

Limiter impl notifier, can add a interval between messages. Limiter run a background goroutine to handle message notify.

func NewLimiter

func NewLimiter(notifier Notifier, interval time.Duration, msgChSize int) *Limiter

NewLimiter create a instance.

func (*Limiter) Close

func (l *Limiter) Close() error

Close impl Notifier.Close. It will wait unfinished messages before close.

func (*Limiter) GetErrorCh

func (l *Limiter) GetErrorCh() <-chan error

GetErrorCh return error message channel.

func (*Limiter) GetName

func (l *Limiter) GetName() string

GetName impl Notifier.GetName. Name is "limiter " + inner notifier name.

func (*Limiter) Notify

func (l *Limiter) Notify(ctx context.Context, to string, msg Message) error

Notify impl Notifier.Notify. This function is unblock, so return error always be nil. If you need error message, see Limiter.GetErrorCh().

func (*Limiter) Wait added in v2.1.0

func (l *Limiter) Wait()

Wait impl Notifier.Wait.

type Mailer

type Mailer struct {
	NoopCloser
	NoopWaiter
	// contains filtered or unexported fields
}

Mailer impl notifier, notify msg by mailgun API.

func NewMailer

func NewMailer(domain, privateKey, recipient, from string) *Mailer

NewMailer create a instance. The recipient is default receiver.

func (*Mailer) GetName

func (mc *Mailer) GetName() string

GetName impl Notifier.GetName.

func (*Mailer) Notify

func (mc *Mailer) Notify(ctx context.Context, to string, msg Message) error

Notify impl Notifier.Notify. If to is not set, will send msg to default recipient. Content is html format.

type Message

type Message struct {
	Content string `json:"content"`
	Title   string `json:"title"`
}

Message is notify msg type.

func MessageFromContent

func MessageFromContent(content string, opts ...Option) Message

MessageFromContent is a helper function creating a message from content and some options.

type NoopCloser

type NoopCloser struct{}

NoopCloser impl a noop io.Closer.

func (*NoopCloser) Close

func (nc *NoopCloser) Close() error

Close impl io.Closer.

type NoopWaiter added in v2.1.0

type NoopWaiter struct{}

NoopWaiter impl a noop Waiter.

func (*NoopWaiter) Wait added in v2.1.0

func (nw *NoopWaiter) Wait()

Wait imple wait.

type Notifier

type Notifier interface {
	// GetName return notifier type name
	GetName() string
	// Notify notify msg
	Notify(ctx context.Context, to string, msg Message) error
	// Close close notifier
	Close() error
	Wait()
}

Notifier is Notify interface.

type Option

type Option func(message *Message)

Option is config function type.

func WithTitle

func WithTitle(title string) Option

WithTitle set title to msg.

type Printer

type Printer struct {
	NoopWaiter
	// contains filtered or unexported fields
}

Printer impl notifier, notify msg to a io.Writer.

func NewPrinter

func NewPrinter(writer io.Writer) *Printer

NewPrinter create a instance.

func (*Printer) Close

func (p *Printer) Close() error

Close impl Notifier.Close.

func (*Printer) GetName

func (p *Printer) GetName() string

GetName impl Notifier.GetName.

func (*Printer) Notify

func (p *Printer) Notify(ctx context.Context, to string, msg Message) error

Notify impl Notifier.Notify. write json encoded msg to writer.

type Resp

type Resp struct {
	Errcode int64  `json:"errcode"`
	Errmsg  string `json:"errmsg"`
}

Resp is dingding api response.

type Telegram

type Telegram struct {
	NoopWaiter
	// contains filtered or unexported fields
}

Telegram impl notifier, notify msg by telegram bot.

func NewTelegram

func NewTelegram(token string, defaultToId int64) (*Telegram, error)

NewTelegram create a instance.

func (*Telegram) Close

func (tg *Telegram) Close() error

Close impl Notifier.Close.

func (*Telegram) GetName

func (tg *Telegram) GetName() string

GetName impl Notifier.GetName.

func (*Telegram) Notify

func (tg *Telegram) Notify(ctx context.Context, to string, msg Message) error

Notify impl Notifier.Notify. If to is not set, will send msg to defaultToId. The telegram message parse mode is html mode.

type TextMsg

type TextMsg struct {
	Msgtype string `json:"msgtype"`
	Text    struct {
		Content string `json:"content"`
	} `json:"text"`
}

TextMsg is dingding text message type.

Jump to

Keyboard shortcuts

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