Documentation
¶
Index ¶
- Variables
- func Empty(context.Context) error
- func GetAPI(ctx context.Context) *telegram.API
- func GetUpdate(ctx context.Context) *telegram.Update
- func IsWebhook(ctx context.Context) bool
- func WithAPI(ctx context.Context, api *telegram.API) context.Context
- func WithSession(ctx context.Context, item SessionData) context.Context
- func WithUpdate(ctx context.Context, u *telegram.Update) context.Context
- type Bot
- func (b *Bot) ErrorFunc(errFunc ErrorFunc)
- func (b *Bot) Handle(handler Handler)
- func (b *Bot) HandleFunc(handler HandlerFunc)
- func (b *Bot) Serve(ctx context.Context) error
- func (b *Bot) ServeByWebhook(ctx context.Context) (http.HandlerFunc, error)
- func (b *Bot) ServeWithConfig(ctx context.Context, cfg telegram.UpdateCfg) error
- func (b *Bot) Use(middleware ...MiddlewareFunc)
- type CallbackFunc
- type CommandFunc
- type Commander
- type ErrorFunc
- type Handler
- type HandlerFunc
- type InlineCallback
- type MiddlewareFunc
- func Callbacks(callbacks map[string]InlineCallback) MiddlewareFunc
- func Commands(commands map[string]Commander) MiddlewareFunc
- func Recover() MiddlewareFunc
- func RecoverWithConfig(cfg RecoverCfg) MiddlewareFunc
- func Session(getSession func(context.Context) ([]byte, UpdateFunc, error)) MiddlewareFunc
- func SessionWithConfig(cfg SessionConfig) MiddlewareFunc
- type RecoverCfg
- type SessionConfig
- type SessionData
- type UpdateFunc
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultRecoverConfig is the default recover middleware config. DefaultRecoverConfig = RecoverCfg{ StackSize: 4 << 10, DisablePrintStack: false, } // DefaultRecoverLogger is used to print recover information // if RecoverCfg.LogFunc is not set DefaultRecoverLogger = log.New(os.Stderr, "", log.LstdFlags) )
Functions ¶
func GetAPI ¶
GetAPI takes telegram API from context. Raises panic if context doesn't have API instance.
func GetUpdate ¶
GetUpdate takes telegram Update from context. Raises panic if context doesn't have Update instance.
func WithAPI ¶
WithAPI returns context with telegram api inside. Use GetAPI to take api from context.
func WithSession ¶
func WithSession(ctx context.Context, item SessionData) context.Context
WithSession returns a new context with SessionData inside.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
A Bot object helps to work with telegram bot api using handlers.
Bot initialization is not thread safe.
func NewWithAPI ¶
NewWithAPI returns bot with custom API client
func (*Bot) ErrorFunc ¶
ErrorFunc set a ErrorFunc, that handles error returned from handlers/middlewares.
func (*Bot) HandleFunc ¶
func (b *Bot) HandleFunc(handler HandlerFunc)
HandleFunc takes HandlerFunc and sets handler.
func (*Bot) Serve ¶
Serve runs update cycle with default update config. Offset is zero and timeout is 30 seconds.
func (*Bot) ServeByWebhook ¶
ServeByWebhook returns webhook handler, that can handle incoming telegram webhook messages.
Use IsWebhook function to identify webhook updates.
func (*Bot) ServeWithConfig ¶
ServeWithConfig runs update cycle with custom update config.
func (*Bot) Use ¶
func (b *Bot) Use(middleware ...MiddlewareFunc)
Use adds middleware to a middleware chain.
type CallbackFunc ¶
CallbackFunc defines a function to handle callbacks. Implements InlineCallback interface.
type CommandFunc ¶
CommandFunc defines a function to handle commands. Implements Commander interface.
type HandlerFunc ¶
HandlerFunc defines a function to serve on update message. Implements Handler interface.
type InlineCallback ¶
InlineCallback interface describes inline callback function.
type MiddlewareFunc ¶
MiddlewareFunc defines a function to process middleware.
func Callbacks ¶
func Callbacks(callbacks map[string]InlineCallback) MiddlewareFunc
Callbacks middleware takes map of callbacks. It runs associated InlineCallback if update messages has a callback query. Callback path is divided by ":". Empty callback (e.x. "": InlineCallback) used as a default callback handler. Nil callback (e.x. "smth": nil) used as an EmptyHandler Take a look on examples/callbacks/main.go to know more.
func Commands ¶
func Commands(commands map[string]Commander) MiddlewareFunc
Commands middleware takes map of commands. It runs associated Commander if update messages has a command message. Empty command (e.x. "": Commander) used as a default Commander. Nil command (e.x. "cmd": nil) used as an EmptyHandler Take a look on examples/commands/main.go to know more.
func Recover ¶
func Recover() MiddlewareFunc
Recover returns a middleware which recovers from panics anywhere in the chain and returns nil error. It prints recovery information to standard log.
func RecoverWithConfig ¶
func RecoverWithConfig(cfg RecoverCfg) MiddlewareFunc
RecoverWithConfig returns a middleware which recovers from panics anywhere in the chain and returns nil error. It takes RecoverCfg to configure itself.
func Session ¶
func Session(getSession func(context.Context) ([]byte, UpdateFunc, error)) MiddlewareFunc
Session is a default middleware to work with sessions. getSession function should receive request context and return []bytes of current session, UpdateFunc that is invoked if session is modified error if something goes wrong.
func SessionWithConfig ¶
func SessionWithConfig(cfg SessionConfig) MiddlewareFunc
SessionWithConfig takes SessionConfig and returns SessionMiddleware
type RecoverCfg ¶
type RecoverCfg struct { // StackSize is the stack size to be printed. // Optional, with default value as 4 KB. StackSize int // EnableStackAll enables formatting stack traces of all // other goroutines into buffer after the trace // for the current goroutine. // Optional, with default value as false. EnableStackAll bool // DisablePrintStack disables printing stack trace. // Optional, with default value as false. DisablePrintStack bool // LogFunc uses to write recover data to your own logger. // Please do not use stack arg after function execution, // because it will be freed. LogFunc func(ctx context.Context, cause error, stack []byte) }
RecoverCfg defines the config for recover middleware.
type SessionConfig ¶
type SessionConfig struct { // Encode takes SessionData and return encoded version Encode func(interface{}) ([]byte, error) // Decode takes encoded session data and fill dst struct Decode func(data []byte, dst interface{}) error // GetSession function should receive request context and return // []bytes of current session and UpdateFunc // that is invoked if session is modified GetSession func(context.Context) ([]byte, UpdateFunc, error) }
SessionConfig helps to configure Session Middleware
type SessionData ¶
type SessionData map[string]interface{}
SessionData describes key:value data
func GetSession ¶
func GetSession(ctx context.Context) SessionData
GetSession returns SessionData or nil for current context
type UpdateFunc ¶
UpdateFunc describes a func to update session data