botty

package module
v0.0.0-...-79cf9fa Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CommandReload = tgbotapi.BotCommand{
		Command:     "reload",
		Description: "Reloads the current state",
	}
	CommandCancel = tgbotapi.BotCommand{
		Command:     "back",
		Description: "stop the current operation, go to the previous state",
	}
	CommandHelp = tgbotapi.BotCommand{
		Command:     "help",
		Description: "Show general help",
	}
	CommandMain = tgbotapi.BotCommand{
		Command:     "home",
		Description: "Go back to root state",
	}
	CommandUsers = tgbotapi.BotCommand{
		Command:     "users",
		Description: "Goes to the user management",
	}
)
View Source
var NoButtons buttonKeyboard = nil

Functions

func NewSession

func NewSession[T any](userId UserId, chatId ChatId, appState T, bot *Bot[T], botCtx context.Context, botApi TGApi) *session[T]

func RunTemplate

func RunTemplate(tpl string, values ...KeyValue) (string, error)

func RunTemplateMap

func RunTemplateMap(tpl string, valueMap map[string]any) (string, error)

Types

type AppStateManager

type AppStateManager[T any] interface {
	CreateAppState(userId UserId, chatId ChatId) T
	StoreSessionState(state StoredSessionState[T]) error

	// rename to list sessions
	LoadSessionStates() ([]StoredSessionState[T], error)
}

type Bot

type Bot[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](config *Config[T]) (*Bot[T], error)

func (*Bot[T]) AcceptUsers

func (b *Bot[T]) AcceptUsers(dur time.Duration)

func (*Bot[T]) ForeachSessionAsync

func (b *Bot[T]) ForeachSessionAsync(do func(session Session[T]))

func (*Bot[T]) Run

func (b *Bot[T]) Run(ctx context.Context) error

type Button

type Button string

func ConditionalButton

func ConditionalButton(condition func() bool, trueButton, falseButton Button) Button

func (Button) Is

func (c Button) Is(val string) bool

func (Button) S

func (c Button) S() string

type ButtonRow

type ButtonRow []Button

func NewConditionalRow

func NewConditionalRow(condition func() bool, row ButtonRow) ButtonRow

func NewRow

func NewRow(commands ...Button) ButtonRow

type CallbackQuery

type CallbackQuery interface {
	Data() string
	ID() string
	MessageID() MessageId
}

type ChatId

type ChatId int64

type ChatMessage

type ChatMessage interface {
	Text() string
}

type CommandHandler

type CommandHandler[T any] interface {
	Handle(bs Session[T], command string, args ...string) bool
}

type Config

type Config[T any] struct {
	// bot token
	Token string

	AppStateManager AppStateManager[T]

	RootState StateFactory[T]

	UserManager UserManager

	Connect func(token string) (TGApi, error)
}

func NewConfig

func NewConfig[T any](token string, appStateManager AppStateManager[T], userManager UserManager, rootState StateFactory[T]) *Config[T]

type DynamicKeyboard

type DynamicKeyboard[T any] struct {
	// contains filtered or unexported fields
}

func NewDynamicKeyboard

func NewDynamicKeyboard[T any]() *DynamicKeyboard[T]

func (*DynamicKeyboard[T]) AddButton

func (d *DynamicKeyboard[T]) AddButton(label string, handler func(bs Session[T]), startRowAfter int)

func (*DynamicKeyboard[T]) Handle

func (d *DynamicKeyboard[T]) Handle(bs Session[T], button Button) bool

func (*DynamicKeyboard[T]) Reset

func (d *DynamicKeyboard[T]) Reset()

func (*DynamicKeyboard[T]) Rows

func (d *DynamicKeyboard[T]) Rows() []ButtonRow

type FuncCommandHandler

type FuncCommandHandler[T any] func(bs Session[T], command string, args ...string) bool

func (FuncCommandHandler[T]) Handle

func (f FuncCommandHandler[T]) Handle(bs Session[T], command string, args ...string) bool

type GlobalMessageHandler

type GlobalMessageHandler[T any] func(bs Session[T], message *tgbotapi.Message) bool

type HandlerMap

type HandlerMap[T any] map[string]CommandHandler[T]

func (HandlerMap[T]) Handle

func (hm HandlerMap[T]) Handle(bs Session[T], command string, args ...string) bool

func (HandlerMap[T]) Set

func (hm HandlerMap[T]) Set(command string, sc CommandHandler[T])

type InlineButton

type InlineButton struct {
	Label string
	Data  string
}

func NewInlineButton

func NewInlineButton(label, data string) InlineButton

func TernaryButton

func TernaryButton(cond bool, trueButton, falseButton InlineButton) InlineButton

type InlineButtonAction

type InlineButtonAction[T any] struct {
	Label  string
	Data   string
	Action func(param T) error
}

func NewInlineButtonAction

func NewInlineButtonAction[T any](label, data string, action func(param T) error) *InlineButtonAction[T]

type InlineKeyboard

type InlineKeyboard []InlineRow

func NewInlineKeyboard

func NewInlineKeyboard(rows ...InlineRow) InlineKeyboard

type InlineMessageHandler

type InlineMessageHandler[T any] func(bs Session[T], query string) (string, InlineKeyboard, error)

type InlineRow

type InlineRow []InlineButton

func NewInlineRow

func NewInlineRow(buttons ...InlineButton) InlineRow

type KeyValue

type KeyValue interface {
	Key() string
	Value() any
}

func KV

func KV(key string, value interface{}) KeyValue

type KeyValues

type KeyValues []KeyValue

func TplValues

func TplValues(values ...KeyValue) KeyValues

type Keyboard

type Keyboard interface {
	Buttons() []ButtonRow
}

func NewButtonKeyboard

func NewButtonKeyboard(rows ...ButtonRow) Keyboard

type Message

type Message interface {
	UpdateMessage(queryId string, text string, opts ...SendMessageOption)
	RemoveKeyboardForMessage()
	ID() int
}

remove that type. It indicates we could use it outside of a session but we shouldn't. Instead the context should have an updater-interface that modifies a messsage and the message-id becomes its own (int)-type

type MessageId

type MessageId int64

type MockBot

type MockBot[T any] struct {
	LastMessage tgbotapi.MessageConfig
	NumMsgSent  int
	// contains filtered or unexported fields
}

func NewMockBot

func NewMockBot[T any](cfg *Config[T]) (*MockBot[T], error)

func (*MockBot[T]) CreateSession

func (mb *MockBot[T]) CreateSession(userId UserId) (Session[T], error)

func (*MockBot[T]) Err

func (mb *MockBot[T]) Err() error

func (*MockBot[T]) LastMessageButtons

func (mb *MockBot[T]) LastMessageButtons() []string

func (*MockBot[T]) LastMessageText

func (mb *MockBot[T]) LastMessageText() string

func (*MockBot[T]) Send

func (mb *MockBot[T]) Send(userId UserId, text string)

func (*MockBot[T]) Stop

func (mb *MockBot[T]) Stop()

type PromptOption

type PromptOption func(opts *promptOptions)

func PromptDropStates

func PromptDropStates(states int) PromptOption

func PromptMessagef

func PromptMessagef(format string, args ...interface{}) PromptOption

type SendMessageOption

type SendMessageOption func(options *sendMessageOptions)

func SendMessageInlineKeyboard

func SendMessageInlineKeyboard(keyboard InlineKeyboard) SendMessageOption

func SendMessageKeepKeyboard

func SendMessageKeepKeyboard() SendMessageOption

func SendMessageWithKeyboard

func SendMessageWithKeyboard(keyboard Keyboard) SendMessageOption

func SendMessageWithNotification

func SendMessageWithNotification() SendMessageOption

type Session

type Session[T any] interface {
	SendMessage(text string, opts ...SendMessageOption) Message
	SendTemplateMessage(template string, values KeyValues, opts ...SendMessageOption) Message
	UpdateMessageForCallback(queryId string, messageId MessageId, text string, opts ...SendMessageOption)

	Fail(message string, formatErrorMsg string, args ...interface{})

	RootState() State[T]
	PushState(state State[T])
	PopState()
	ReplaceState(state State[T])
	ResetToState(state State[T])
	DropStates(n int)
	SendError(err error)
	CurrentState() State[T]

	RemoveKeyboardForMessage(messageId MessageId)

	// returns the current user ID
	UserId() UserId

	AcceptUsers(duration time.Duration)

	BotName() (string, error)

	Context() context.Context

	State() T

	LastUserAction() time.Time
}

type State

type State[T any] interface {
	Activate(bs Session[T])
	Return(bs Session[T])
	HandleMessage(bs Session[T], msg ChatMessage) bool
	HandleCommand(bs Session[T], command string, args ...string) bool
	HandleCallbackQuery(bs Session[T], query CallbackQuery) bool

	// called before leaving the state (either by pushing another state on top of it or popping it)
	BeforeLeave(bs Session[T])
}

func NewMessageHandler

func NewMessageHandler[T any](handleQuery InlineMessageHandler[T]) State[T]

func NewMultiMessageHandler

func NewMultiMessageHandler[T any](handlers ...InlineMessageHandler[T]) State[T]

func PromptState

func PromptState[T any](yesHandler func(), options ...PromptOption) State[T]

func SelectState

func SelectState[O, T any](text string, items []O, accept func(bs Session[T], item O)) State[T]

func SelectToDeleteUser

func SelectToDeleteUser[T any](uStorage UserManager, users []User) State[T]

func UsersList

func UsersList[T any](uStorage UserManager) State[T]

type StateBuilder

type StateBuilder[T any] struct {
	// contains filtered or unexported fields
}

func NewStateBuilder

func NewStateBuilder[T any]() *StateBuilder[T]

func (*StateBuilder[T]) Build

func (sb *StateBuilder[T]) Build() State[T]

func (*StateBuilder[T]) OnActivate

func (sb *StateBuilder[T]) OnActivate(activator func(bs Session[T])) *StateBuilder[T]

func (*StateBuilder[T]) OnBeforeLeave

func (sb *StateBuilder[T]) OnBeforeLeave(handler func(bs Session[T])) *StateBuilder[T]

func (*StateBuilder[T]) OnButton

func (sb *StateBuilder[T]) OnButton(button Button, handler func(bs Session[T], message ChatMessage)) *StateBuilder[T]

func (*StateBuilder[T]) OnCallbackQuery

func (sb *StateBuilder[T]) OnCallbackQuery(handler func(bs Session[T], query CallbackQuery) bool) *StateBuilder[T]

func (*StateBuilder[T]) OnInlineButton

func (sb *StateBuilder[T]) OnInlineButton(button InlineButton, handler func(bs Session[T], query CallbackQuery) bool) *StateBuilder[T]

func (*StateBuilder[T]) OnMessage

func (sb *StateBuilder[T]) OnMessage(handleMessage func(bs Session[T], message ChatMessage)) *StateBuilder[T]

type StateFactory

type StateFactory[T any] func() State[T]

type StoredSessionState

type StoredSessionState[T any] struct {
	UserID     UserId
	ChatID     ChatId
	LastAction time.Time
	State      T
}

type TGApi

type TGApi interface {
	Request(c tgbotapi.Chattable) (*tgbotapi.APIResponse, error)
	Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
	GetMe() (tgbotapi.User, error)
	GetUpdatesChan(config tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel
	StopReceivingUpdates()
}

type User

type User struct {
	ID   UserId
	Name string
}

type UserId

type UserId int64

type UserManager

type UserManager interface {
	ListUsers() ([]User, error)
	AddUser(userID UserId, userName string) error
	UserExists(userID UserId) bool
	DeleteUser(userID UserId) error
}

Jump to

Keyboard shortcuts

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