handler

package
v0.0.0-...-4438170 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HandleFailed  = -2
	HandleSuccess = -1
)
View Source
const (
	BufferSize = 1000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConversationPersistence

type ConversationPersistence interface {
	// GetState & SetState tell conversation handlers how to retrieve & set conversation state.
	GetState(pk PersistenceKey) (int, bool)
	SetState(pk PersistenceKey, state int)
}

ConversationPersistence interface tells conversation where to store & how to retrieve the current state of the conversation, i. e. which "step" the given user is currently at.

type FilterFunc

type FilterFunc func(u *types.TelegramUpdate) bool

FilterFunc is used to check if this update should be processed by handler.

func And

func And(filters ...FilterFunc) FilterFunc

And filters updates that pass ALL of the provided filters.

func Any

func Any() FilterFunc

Any tells handler to process all updates.

func HasDocument

func HasDocument() FilterFunc

HasPhoto filters updates that contain a photo.

func IsAnyCommandMessage

func IsAnyCommandMessage() FilterFunc

IsAnyCommandMessage filters updates that contain a message and look like a command, i. e. have some text and start with a slash ("/"). If command contains bot username, it is also checked.

func IsCommandMessage

func IsCommandMessage(cmd string) FilterFunc

IsCommandMessage filters updates that contain a specific command. For example, IsCommandMessage("start") will handle a "/start" command. This will also allow the user to pass arguments, e. g. "/start foo bar". Commands in format "/start@bot_name" and "/start@bot_name foo bar" are also supported. If command contains bot username, it is also checked.

func IsMessage

func IsMessage() FilterFunc

IsMessage filters updates that look like message (text, photo, location etc.)

func Not

func Not(filter FilterFunc) FilterFunc

Not filters updates that do not pass the provided filter.

func Or

func Or(filters ...FilterFunc) FilterFunc

Or filters updates that pass ANY of the provided filters.

type HandleFunc

type HandleFunc func(u *types.TelegramUpdate) int

HandleFunc processes update.

type Handler

type Handler struct {
	Filter  FilterFunc
	Handles []HandleFunc
}

Handler defines a function that will handle updates that pass the filtering.

func NewCommandHandler

func NewCommandHandler(command string, handles ...HandleFunc) *Handler

NewCommandHandler is an extension for NewMessageHandler that creates a handler for updates that contain message with command.

func NewConversationHandler

func NewConversationHandler(
	persistence ConversationPersistence,
	states StateMap,
	cancelHandlers []*Handler,
) *Handler

NewConversationHandler creates a conversation handler.

func NewHandler

func NewHandler(filter FilterFunc, handles ...HandleFunc) *Handler

NewHandler creates a new generic handler.

func NewMessageHandler

func NewMessageHandler(filter FilterFunc, handles ...HandleFunc) *Handler

NewMessageHandler creates a handler for updates that contain message.

type KeyLock

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

func NewKeyLock

func NewKeyLock() *KeyLock

func (*KeyLock) Lock

func (l *KeyLock) Lock(key PersistenceKey)

func (*KeyLock) Unlock

func (l *KeyLock) Unlock(key PersistenceKey)

type LocalPersistence

type LocalPersistence struct {
	States *safemap.SafeMap[PersistenceKey, int]
}

func NewLocalPersistence

func NewLocalPersistence() *LocalPersistence

NewLocalPersistence creates new instance of LocalPersistence.

func (*LocalPersistence) GetState

func (p *LocalPersistence) GetState(pk PersistenceKey) (int, bool)

func (*LocalPersistence) SetState

func (p *LocalPersistence) SetState(pk PersistenceKey, state int)

type PersistenceKey

type PersistenceKey struct {
	UserID int64
	ChatID int64
}

PersistenceKey contains user & chat IDs. It is used to identify conversations with different users in different chats.

type StateMap

type StateMap map[int][]*Handler

StateMap is an alias to map of strings to handler slices.

type UpdateDispatcher

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

func NewUpdateDispatcher

func NewUpdateDispatcher() *UpdateDispatcher

func (*UpdateDispatcher) AddHandler

func (u *UpdateDispatcher) AddHandler(h *Handler) *UpdateDispatcher

func (*UpdateDispatcher) Dispatch

func (u *UpdateDispatcher) Dispatch(tu *types.TelegramUpdate)

Jump to

Keyboard shortcuts

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