telebot

package
v0.0.0-...-b7abf87 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2017 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 Empty

func Empty(context.Context) error

Empty does nothing.

func GetAPI

func GetAPI(ctx context.Context) *telegram.API

GetAPI takes telegram API from context. Raises panic if context doesn't have API instance.

func GetUpdate

func GetUpdate(ctx context.Context) *telegram.Update

GetUpdate takes telegram Update from context. Raises panic if context doesn't have Update instance.

func IsWebhook

func IsWebhook(ctx context.Context) bool

IsWebhook returns true if update received by webhook

func WithAPI

func WithAPI(ctx context.Context, api *telegram.API) context.Context

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.

func WithUpdate

func WithUpdate(ctx context.Context, u *telegram.Update) context.Context

WithUpdate returns context with telegram Update inside. Use GetUpdate to take Update from context.

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 New

func New(token string) *Bot

New returns bot with default api client

func NewWithAPI

func NewWithAPI(api *telegram.API) *Bot

NewWithAPI returns bot with custom API client

func (*Bot) ErrorFunc

func (b *Bot) ErrorFunc(errFunc ErrorFunc)

ErrorFunc set a ErrorFunc, that handles error returned from handlers/middlewares.

func (*Bot) Handle

func (b *Bot) Handle(handler Handler)

Handle setups handler to handle telegram updates.

func (*Bot) HandleFunc

func (b *Bot) HandleFunc(handler HandlerFunc)

HandleFunc takes HandlerFunc and sets handler.

func (*Bot) Serve

func (b *Bot) Serve(ctx context.Context) error

Serve runs update cycle with default update config. Offset is zero and timeout is 30 seconds.

func (*Bot) ServeByWebhook

func (b *Bot) ServeByWebhook(ctx context.Context) (http.HandlerFunc, error)

ServeByWebhook returns webhook handler, that can handle incoming telegram webhook messages.

Use IsWebhook function to identify webhook updates.

func (*Bot) ServeWithConfig

func (b *Bot) ServeWithConfig(ctx context.Context, cfg telegram.UpdateCfg) error

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

type CallbackFunc func(ctx context.Context, data string) error

CallbackFunc defines a function to handle callbacks. Implements InlineCallback interface.

func (CallbackFunc) Callback

func (c CallbackFunc) Callback(ctx context.Context, data string) error

Callback method handles command on message update.

type CommandFunc

type CommandFunc func(ctx context.Context, arg string) error

CommandFunc defines a function to handle commands. Implements Commander interface.

func (CommandFunc) Command

func (c CommandFunc) Command(ctx context.Context, arg string) error

Command method handles command on message update.

type Commander

type Commander interface {
	Command(ctx context.Context, arg string) error
}

A Commander takes command message.

type ErrorFunc

type ErrorFunc func(ctx context.Context, err error)

ErrorFunc handles error, if

type Handler

type Handler interface {
	Handle(context.Context) error
}

A Handler takes update message.

func EmptyHandler

func EmptyHandler() Handler

EmptyHandler returns a handler that does nothing.

func StringHandler

func StringHandler(text string) Handler

StringHandler sends user a text

type HandlerFunc

type HandlerFunc func(context.Context) error

HandlerFunc defines a function to serve on update message. Implements Handler interface.

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(c context.Context) error

Handle method handles message update.

type InlineCallback

type InlineCallback interface {
	Callback(ctx context.Context, data string) error
}

InlineCallback interface describes inline callback function.

type MiddlewareFunc

type MiddlewareFunc func(next Handler) Handler

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

type UpdateFunc func(data []byte) error

UpdateFunc describes a func to update session data

Jump to

Keyboard shortcuts

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