telegram

package
v2.0.0-...-4a423d9 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ModeHTML is one of the modes for sending messages.
	ModeHTML = telegram.ModeHTML
	// ModeMarkdown is the default mode for sending messages.
	ModeMarkdown = telegram.ModeMarkdown
)

Variables

This section is empty.

Functions

func SendWithParseMode

func SendWithParseMode(parseMode string) notify.SendOption

SendWithParseMode is a send option that sets the parse mode of the message.

Types

type Option

type Option = func(*Service)

Option is a function that can be used to configure the telegram service.

func WithClient

func WithClient(client *telegram.BotAPI) Option

WithClient sets the telegram client. This is useful if you want to use a custom client.

func WithContinueOnErr

func WithContinueOnErr(continueOnErr bool) Option

WithContinueOnErr sets the continue on error flag. If set to true, the service will continue sending the message to the next recipient even if an error occurred.

func WithDryRun

func WithDryRun(dryRun bool) Option

WithDryRun sets the dry run flag. If set to true, messages will not be sent.

func WithLogger

func WithLogger(logger onelog.Logger) Option

WithLogger sets the logger. The default logger is a no-op logger.

func WithMessageRenderer

func WithMessageRenderer(builder func(conf *SendConfig) string) Option

WithMessageRenderer sets the message renderer. The default function will put the subject and message on separate lines.

Example:

telegram.WithMessageRenderer(func(conf *SendConfig) string {
	var builder strings.Builder

	builder.WriteString(conf.subject)
	builder.WriteString("\n")
	builder.WriteString(conf.message)

	return builder.String()
})

func WithName

func WithName(name string) Option

WithName sets the name of the service. The default name is "telegram".

func WithParseMode

func WithParseMode(mode string) Option

WithParseMode sets the parse mode for sending messages. The default is ModeHTML.

func WithRecipients

func WithRecipients(chatIDs ...int64) Option

WithRecipients sets the chat IDs that should receive messages. You can add more chat IDs by calling AddRecipients.

type SendConfig

type SendConfig struct {
	Subject       string
	Message       string
	Attachments   []notify.Attachment
	Metadata      map[string]any
	DryRun        bool
	ContinueOnErr bool

	ParseMode string
}

SendConfig represents the configuration needed for sending a message.

This struct complies with the notify.SendConfig interface and allows you to alter the behavior of the send function. This can be achieved by either passing send options to the send function or by manipulating the fields of this struct in your custom message renderer.

All fields of this struct are exported to offer maximum flexibility to users. However, users must be aware that they are responsible for managing thread-safety and other similar concerns when manipulating these fields directly.

func (*SendConfig) SetAttachments

func (c *SendConfig) SetAttachments(attachments ...notify.Attachment)

SetAttachments adds attachments to the message. This method is needed as part of the notify.SendConfig interface.

func (*SendConfig) SetContinueOnErr

func (c *SendConfig) SetContinueOnErr(continueOnErr bool)

SetContinueOnErr sets the continue on error flag of the message. This method is needed as part of the notify.SendConfig interface.

func (*SendConfig) SetDryRun

func (c *SendConfig) SetDryRun(dryRun bool)

SetDryRun sets the dry run flag of the message. This method is needed as part of the notify.SendConfig interface.

func (*SendConfig) SetMetadata

func (c *SendConfig) SetMetadata(metadata map[string]any)

SetMetadata sets the metadata of the message. This method is needed as part of the notify.SendConfig interface.

type Service

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

Service is the telegram service. It is used to send messages to Telegram chats.

func New

func New(token string, opts ...Option) (*Service, error)

New creates a new telegram service. It returns an error if the telegram client could not be created.

func (*Service) AddRecipients

func (s *Service) AddRecipients(chatIDs ...int64)

AddRecipients adds chat IDs that should receive messages.

func (*Service) Name

func (s *Service) Name() string

Name returns the name of the service.

func (*Service) Send

func (s *Service) Send(ctx context.Context, subject, message string, opts ...notify.SendOption) error

Send sends a message to all chats that are configured to receive messages. It returns an error if the message could not be sent.

Jump to

Keyboard shortcuts

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