Documentation
¶
Index ¶
- Variables
- func Escape2(str string) string
- type Action
- type ActionFunc
- type Behaviour
- type Bot
- func (bot *Bot) API() *tgbotapi.BotAPI
- func (bot *Bot) Data() any
- func (bot *Bot) DeleteCommands()
- func (bot *Bot) GotSession(sid SessionID) (*Session, bool)
- func (bot *Bot) Me() User
- func (bot *Bot) Run() error
- func (bot *Bot) Send(sid SessionID, v Sendable) (*Message, error)
- func (bot *Bot) SendRaw(sid SessionID, v tgbotapi.Chattable) (*Message, error)
- func (bot *Bot) Sendf(sid SessionID, format string, v ...any) (*Message, error)
- func (b *Bot) SetBehaviour(beh *Behaviour) *Bot
- func (bot *Bot) SetCommands(scope tgbotapi.BotCommandScope, cmdMap CommandMap) error
- func (bot *Bot) SetData(v any) *Bot
- func (bot *Bot) SetDebug(debug bool) *Bot
- func (b *Bot) SetSessions(sessions SessionMap) *Bot
- type Button
- func (btn Button) Go(pth Widget) Button
- func (btn Button) GoWithArg(pth Widget, arg any) Button
- func (btn Button) Key() string
- func (btn Button) Rand() Button
- func (btn Button) ToTelegram() apix.KeyboardButton
- func (btn Button) ToTelegramInline() apix.InlineKeyboardButton
- func (btn Button) WithAction(a Action) Button
- func (btn Button) WithData(dat string) Button
- func (btn Button) WithSendLocation(ok bool) Button
- func (btn Button) WithURL(format string, v ...any) Button
- type ButtonMap
- type ButtonRow
- type Chat
- type Command
- type CommandCompo
- type CommandMap
- type CommandName
- type CommandType
- type Component
- type Context
- func (c Context) Arg() any
- func (c Context) As(sid SessionID) (Context, bool)
- func (c Context) Bot() *Bot
- func (c Context) CallbackUpdate() *Update
- func (c Context) GetFile(fileID FileID) (io.ReadCloser, string, error)
- func (c Context) Go(pth Widget) error
- func (c Context) GoRet(pth Widget) UI
- func (c Context) Input() chan Update
- func (c Context) Path() Widget
- func (c Context) PathHistory() []Widget
- func (c Context) ReadFile(fileID FileID) ([]byte, string, error)
- func (c Context) ReadString(promptf string, args ...any) string
- func (c Context) Run(a Action)
- func (c Context) RunCompo(compo Component) (*UpdateChan, error)
- func (c Context) RunWidget(widget Widget) (*UpdateChan, error)
- func (c Context) Send(v Sendable) (*Message, error)
- func (c Context) Sendf(format string, v ...any) (*Message, error)
- func (c Context) Sendf2(format string, v ...any) (*Message, error)
- func (c Context) SendfHTML(format string, v ...any) (*Message, error)
- func (c Context) SendfR(format string, v ...any) (*Message, error)
- func (c Context) Session() Session
- func (c Context) SessionData() any
- func (c Context) SessionID() SessionID
- func (c Context) SessionScope() SessionScope
- func (c Context) SetPathHistory(hist []Widget)
- func (c Context) SetSessionData(v any)
- func (c Context) SkipUpdate(u Update)
- func (c Context) Update(updater Updater) error
- func (c Context) WithArg(v any) Context
- func (c Context) WithInput(input *UpdateChan) Context
- func (c Context) WithUpdate(u Update) Context
- type ContextType
- type File
- func (f *File) Caption(caption string) *File
- func (f *File) Data(data string) *File
- func (f *File) Document() *File
- func (f *File) Name(name string) *File
- func (f *File) NeedsUpload() bool
- func (f *File) Photo() *File
- func (f *File) SendConfig(sid SessionID, bot *Bot) SendConfig
- func (f *File) SendData() string
- func (f *File) Type() FileType
- func (f *File) Upload(upload bool) *File
- func (f *File) UploadData() (string, io.Reader, error)
- type FileConfig
- type FileID
- type FileType
- type FilterFunc
- type Filterer
- type Func
- type Inline
- type InlineCompo
- type InvoiceCompo
- type Keyboard
- func (kbd Keyboard) ButtonMap() ButtonMap
- func (kbd Keyboard) Inline() Inline
- func (kbd Keyboard) List(btns ...Button) Keyboard
- func (kbd Keyboard) MakeButtonMap() ButtonMap
- func (kbd *Keyboard) RemoveRow(i int)
- func (kbd Keyboard) Reply() Reply
- func (kbd Keyboard) Row(btns ...Button) Keyboard
- func (kbd Keyboard) RowNum() int
- func (kbd Keyboard) WithAction(a Action) Keyboard
- type Location
- type LocationCompo
- type Maker
- type Message
- type MessageCompo
- func (compo *MessageCompo) Delete(c Context) error
- func (compo *MessageCompo) Filter(_ Update) bool
- func (compo *MessageCompo) HTML() *MessageCompo
- func (compo *MessageCompo) Inline(inline Inline) *InlineCompo
- func (msg *MessageCompo) Location(lat, long float64) *LocationCompo
- func (compo *MessageCompo) MD() *MessageCompo
- func (compo *MessageCompo) MD2() *MessageCompo
- func (compo *MessageCompo) Panel(c Context, paneler Paneler) *PanelCompo
- func (compo *MessageCompo) PanelPager(c Context, startPage, size int, pager PanelPager) *PanelPagerCompo
- func (msg *MessageCompo) Reply(reply Reply) *ReplyCompo
- func (compo *MessageCompo) SendConfig(sid SessionID, bot *Bot) SendConfig
- func (compo *MessageCompo) Serve(c Context)
- func (compo *MessageCompo) SetMessage(msg *Message)
- func (compo *MessageCompo) Update(c Context) error
- type MessageId
- type MessageMap
- type PanelCompo
- type PanelFunc
- type PanelPage
- type PanelPager
- type PanelPagerCompo
- type PanelPagerFunc
- type Paneler
- type PhotoConfig
- type Poll
- type PollCompo
- type RenderFunc
- type Reply
- type ReplyCompo
- type Rowser
- type RowserFunc
- type SendConfig
- type Sendable
- type Server
- type Session
- type SessionID
- type SessionMap
- type SessionScope
- type UI
- type Update
- type UpdateChan
- type Updater
- type User
- type Widget
- type WidgetGo
- type WidgetSpecial
- type WrongUpdateType
Constants ¶
This section is empty.
Variables ¶
var ( ScreenNotExistErr = errors.New("screen does not exist") SessionNotExistErr = errors.New("session does not exist") KeyboardNotExistErr = errors.New("keyboard does not exist") NotAvailableErr = errors.New("the context is not available") EmptyKeyboardTextErr = errors.New("got empty text for a keyboard") ActionNotDefinedErr = errors.New("action was not defined") MapCollisionErr = errors.New("map collision occured") ContextNotExistErr = errors.New("the context does not exist") StatusCodeErr = errors.New("not success response status code") NotSendErr = errors.New("could not send message") NoWidgetForScreenErr = errors.New("no widget defined for the screen") EmptyCompoErr = errors.New("empty component") EmptyWidgetErr = errors.New("empty widget") )
var (
Back = Widget(widgetBack)
)
var (
NewRawMessage = tgbotapi.NewMessage
)
var (
UnknownFileTypeErr = errors.New("unknown file type")
)
Functions ¶
Types ¶
type ActionFunc ¶
type ActionFunc func(Context)
func (ActionFunc) Act ¶
func (af ActionFunc) Act(c Context)
type Behaviour ¶
The type describes behaviour for the bot in personal chats.
func (*Behaviour) SetInit ¶
The Action will be called on session creation, not when starting or restarting the bot with the Start Action.
func (*Behaviour) SetRootWidget ¶
func (b *Behaviour) SetScreens(screens ScreenMap) *Behaviour { b.Screens = screens return b }
// Sets the root node of the Behaviour. // Mostly used for commands and such stuff.
func (b *Behaviour) SetRootNode(node *RootNode) *Behaviour { b.Screens = node.ScreenMap() return b }
The function sets as the standard root widget CommandWidget and its commands..
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
The wrapper around Telegram API.
func (*Bot) DeleteCommands ¶
func (bot *Bot) DeleteCommands()
func (*Bot) GotSession ¶
Get session by its ID. Can be used for any scope including private, group and channel.
func (*Bot) Send ¶
Send the Renderable to the specified session client side. Can be used for both group and private sessions because SessionID represents both for chat IDs.
func (*Bot) SetBehaviour ¶
func (*Bot) SetCommands ¶
func (bot *Bot) SetCommands( scope tgbotapi.BotCommandScope, cmdMap CommandMap, ) error
Setting the command on the user side.
func (*Bot) SetSessions ¶
func (b *Bot) SetSessions(sessions SessionMap) *Bot
type Button ¶
type Button struct { Text string Data string URL string SendLocation bool Action Action // Used to skip buttons in generating by functions. Valid bool }
The type wraps Telegram API's button to provide Action functionality.
func (Button) Rand ¶
Randomize buttons data to make the key unique. No guaranties about collisions though.
func (Button) ToTelegram ¶
func (btn Button) ToTelegram() apix.KeyboardButton
func (Button) ToTelegramInline ¶
func (btn Button) ToTelegramInline() apix.InlineKeyboardButton
func (Button) WithAction ¶
Set the action when pressing the button. By default is nil and does nothing.
func (Button) WithSendLocation ¶
Sets whether the button must send owner's location.
type ButtonMap ¶
func (ButtonMap) LocationButton ¶
type Command ¶
type Command struct { Name CommandName Type CommandType Description string Action Action Widget Widget WidgetArg any }
func NewCommand ¶
func NewCommand(name CommandName, desc string) Command
func (Command) ToAPI ¶ added in v0.18.0
func (c Command) ToAPI() tgbotapi.BotCommand
Convert command into the tgbotapi.BotCommand
func (Command) WithAction ¶
func (Command) WithWidget ¶
type CommandCompo ¶
type CommandCompo struct { PreStart Action Commands CommandMap Usage Action }
The type is used to recognize commands and execute its actions and widgets .
func NewCommandCompo ¶
func NewCommandCompo(cmds ...Command) *CommandCompo
Returns new empty CommandCompo.
func (*CommandCompo) Filter ¶
func (widget *CommandCompo) Filter( u Update, ) bool
Filtering all the non commands.
func (*CommandCompo) SetCommands ¶
func (w *CommandCompo) SetCommands(cmds ...Command) *CommandCompo
Set the commands to handle.
func (*CommandCompo) SetPreStart ¶
func (w *CommandCompo) SetPreStart(a Action) *CommandCompo
Set the prestart action.
func (*CommandCompo) SetUsage ¶
func (w *CommandCompo) SetUsage(a Action) *CommandCompo
Set the usage action.
type CommandMap ¶
type CommandMap map[CommandName]Command
type CommandName ¶
type CommandName string
type CommandType ¶
type CommandType uint8
const ( PrivateCommandType CommandType = iota GroupCommandType ChannelCommandType )
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Interface to interact with the user.
func (Context) As ¶
Run commands as other user. Was implemented to make other user to leave the bot at first but maybe you will find another usage for this. Returns users context by specified session ID or false if the user is not logged in.
func (Context) CallbackUpdate ¶
func (Context) Input ¶
Get the input for current widget. Should be used inside handlers (aka "Serve").
func (Context) PathHistory ¶
Return context's session's path history.
func (Context) ReadString ¶
Simple way to read strings for widgets with the specified prompt.
func (Context) RunCompo ¶ added in v0.21.0
func (c Context) RunCompo(compo Component) (*UpdateChan, error)
func (Context) RunWidget ¶ added in v0.21.0
func (c Context) RunWidget(widget Widget) (*UpdateChan, error)
Run widget in background returning the new input channel for it.
func (Context) Sendf ¶
Sends the formatted with fmt.Sprintf message to the user using default Markdown parsing format.
func (Context) SessionData ¶
func (Context) SessionScope ¶
func (c Context) SessionScope() SessionScope
func (Context) SetPathHistory ¶ added in v0.21.4
func (Context) SetSessionData ¶
func (Context) SkipUpdate ¶
Only for the root widget usage. Skip the update sending it down to the underlying widget.
func (Context) WithInput ¶
func (c Context) WithInput(input *UpdateChan) Context
func (Context) WithUpdate ¶
type ContextType ¶
type ContextType uint8
The type represents type of current context the processing is happening in.
const ( NoContextType ContextType = iota WidgetContextType ActionContextType )
type File ¶
type File struct { MessageCompo // contains filtered or unexported fields }
The type implements the structure to easily send files to the client.
func NewFile ¶
Create the new file with the specified reader. By default it NeedsUpload is set to true.
func (*File) NeedsUpload ¶
func (*File) SendConfig ¶
func (f *File) SendConfig( sid SessionID, bot *Bot, ) SendConfig
type FileConfig ¶
type FileConfig = tgbotapi.FileConfig
type FilterFunc ¶
func (FilterFunc) Filter ¶
func (f FilterFunc) Filter( u Update, ) bool
type Filterer ¶
type Filterer interface { // Return true if should filter the update // and not send it inside the widget. Filter(Update) bool }
Implementing the interface provides way to know exactly what kind of updates the widget needs.
type Func ¶
type Func func(Context)
General type function to define actions, single component widgets and components themselves.
type Inline ¶
type Inline struct {
Keyboard
}
The type represents keyboard to be emdedded into the messages (inline in Telegram terms).
func (Inline) ToAPI ¶ added in v0.18.0
func (kbd Inline) ToAPI() tgbotapi.InlineKeyboardMarkup
Convert the inline keyboard to markup for the tgbotapi.
type InlineCompo ¶
type InlineCompo struct { MessageCompo Inline }
The type implements message with an inline keyboard.
func (*InlineCompo) Filter ¶
func (compo *InlineCompo) Filter(u Update) bool
Implementing the Filterer interface.
func (*InlineCompo) OnOneUpdate ¶
func (compo *InlineCompo) OnOneUpdate(c Context, u Update) error
func (*InlineCompo) SendConfig ¶
func (compo *InlineCompo) SendConfig( sid SessionID, bot *Bot, ) SendConfig
Implementing the Sendable interface.
func (*InlineCompo) Serve ¶
func (compo *InlineCompo) Serve(c Context)
Implementing the Server interface.
func (*InlineCompo) Update ¶
func (compo *InlineCompo) Update(c Context) error
Update the component on the client side. Requires exactly the pointer but not the value cause it changes insides of the structure.
type InvoiceCompo ¶
type InvoiceCompo struct { *MessageCompo tgbotapi.InvoiceConfig }
func (*InvoiceCompo) SendConfig ¶
func (compo *InvoiceCompo) SendConfig( sid SessionID, bot *Bot, ) *SendConfig
type Keyboard ¶
type Keyboard struct { // The action is called if there is no // defined action for the button. Action Action Rows []ButtonRow }
The general keyboard type used both in Reply and Inline.
func NewKeyboard ¶
Returns the new keyboard with specified rows.
func (Keyboard) ButtonMap ¶
Returns the map of buttons. Where the key is button data and the value is Action.
func (Keyboard) MakeButtonMap ¶
Returns the map of buttons on the most fresh version of the keyboard.
func (Keyboard) Reply ¶
Convert the keyboard to the more specific reply one. By default OneTime = true.
func (Keyboard) WithAction ¶
Set the default action when no button provides key to the data we got.
type LocationCompo ¶
type LocationCompo struct { MessageCompo Location }
func (*LocationCompo) SendConfig ¶
func (compo *LocationCompo) SendConfig( sid SessionID, bot *Bot, ) SendConfig
type MessageCompo ¶
type MessageCompo struct { // Low level Message represents // the already sent to the client message. // Will be nil if the message is not rendered to the client. Message *Message // Parsing mode for the text: HTML, MD, MD2... ParseMode string // The text to display. Text string }
Simple text message component type.
func Messagef ¶
func Messagef(format string, v ...any) *MessageCompo
Return new message with the specified text formatted with the fmt.Sprintf function.
func (*MessageCompo) Delete ¶
func (compo *MessageCompo) Delete(c Context) error
Calling the method removes the message on the client side and sets the Message in the component to nil.
func (*MessageCompo) Filter ¶
func (compo *MessageCompo) Filter(_ Update) bool
Filter that skips everything. Messages cannot do anything with updates.
func (*MessageCompo) HTML ¶
func (compo *MessageCompo) HTML() *MessageCompo
Set the HTML parsing mode.
func (*MessageCompo) Inline ¶
func (compo *MessageCompo) Inline(inline Inline) *InlineCompo
Transform the message component into one with reply keyboard.
func (*MessageCompo) Location ¶
func (msg *MessageCompo) Location( lat, long float64, ) *LocationCompo
Transform the message component into the location one.
func (*MessageCompo) MD ¶
func (compo *MessageCompo) MD() *MessageCompo
Set the default Markdown parsing mode.
func (*MessageCompo) MD2 ¶
func (compo *MessageCompo) MD2() *MessageCompo
Set the Markdown 2 parsing mode.
func (*MessageCompo) Panel ¶
func (compo *MessageCompo) Panel( c Context, paneler Paneler, ) *PanelCompo
Transform to the panel with dynamic rows.
func (*MessageCompo) PanelPager ¶ added in v0.20.0
func (compo *MessageCompo) PanelPager( c Context, startPage, size int, pager PanelPager, ) *PanelPagerCompo
func (*MessageCompo) Reply ¶
func (msg *MessageCompo) Reply(reply Reply) *ReplyCompo
Transform the message component into one with reply keyboard.
func (*MessageCompo) SendConfig ¶
func (compo *MessageCompo) SendConfig( sid SessionID, bot *Bot, ) SendConfig
Implementing the Sendable interface.
func (*MessageCompo) Serve ¶
func (compo *MessageCompo) Serve(c Context)
Empty serving to use messages in rendering.
func (*MessageCompo) SetMessage ¶
func (compo *MessageCompo) SetMessage(msg *Message)
Implementing the Sendable interface. Also used for embedding for things like InlineCompo etc.
func (*MessageCompo) Update ¶
func (compo *MessageCompo) Update(c Context) error
Call the function after the message was sent.
type MessageMap ¶
type PanelCompo ¶
type PanelCompo struct { InlineCompo Paneler Paneler }
The type represents the inline panel with scrollable via buttons content. Can be used for example to show users via SQL and offset or something like that.
func (*PanelCompo) Update ¶
func (panel *PanelCompo) Update(c Context) error
Implementing the Updater.
type PanelFunc ¶ added in v0.22.0
type PanelFunc func(*PanelCompo, Context) []ButtonRow
func (PanelFunc) GetPanelRows ¶ added in v0.22.0
func (fn PanelFunc) GetPanelRows( panel *PanelCompo, c Context, ) []ButtonRow
type PanelPager ¶ added in v0.20.0
type PanelPager interface { GetPanelPage( panel *PanelPagerCompo, c Context, page, size int, ) PanelPage }
type PanelPagerCompo ¶ added in v0.20.0
type PanelPagerCompo struct { PanelCompo // contains filtered or unexported fields }
func (*PanelPagerCompo) GetPanelRows ¶ added in v0.22.0
func (panel *PanelPagerCompo) GetPanelRows( pcompo *PanelCompo, c Context, ) []ButtonRow
type PanelPagerFunc ¶ added in v0.20.0
type PanelPagerFunc func( panel *PanelPagerCompo, c Context, page, size int, ) PanelPage
func (PanelPagerFunc) GetPanelPage ¶ added in v0.20.0
func (fn PanelPagerFunc) GetPanelPage( panel *PanelPagerCompo, c Context, page, size int, ) PanelPage
type Paneler ¶ added in v0.22.0
type Paneler interface {
GetPanelRows(*PanelCompo, Context) []ButtonRow
}
type PhotoConfig ¶
type PhotoConfig = tgbotapi.PhotoConfig
type RenderFunc ¶
The way to describe custom function based Widgets.
func (RenderFunc) Render ¶
func (fn RenderFunc) Render(c Context) UI
type Reply ¶
type Reply struct { Keyboard // If true will be removed after one press. OneTime bool // If true will remove the keyboard on send. Remove bool }
The type represents reply keyboards.
func (Reply) WithOneTime ¶
Set if the keyboard should be hidden after one of buttons is pressede.
func (Reply) WithRemove ¶
Set if we should remove current keyboard on the user side when sending the keyboard.
type ReplyCompo ¶
type ReplyCompo struct { MessageCompo Reply }
The type implements reply keyboard widget.
func (*ReplyCompo) Filter ¶
func (compo *ReplyCompo) Filter( u Update, ) bool
Implementing the Server interface.
func (*ReplyCompo) SendConfig ¶
func (compo *ReplyCompo) SendConfig( sid SessionID, bot *Bot, ) SendConfig
Implementing the sendable interface.
type Rowser ¶
Using the interface and all related is deprecated. Use the Paneler interface and function.
type RowserFunc ¶
func (RowserFunc) MakeRows ¶
func (fn RowserFunc) MakeRows(c Context) []ButtonRow
type SendConfig ¶
The type is used as an endpoint to send messages via bot.Send .
func (SendConfig) ToAPI ¶ added in v0.18.0
func (config SendConfig) ToAPI() tgbotapi.Chattable
Convert to the bot.Api.Send format.
type Sendable ¶
type Sendable interface { SendConfig(SessionID, *Bot) SendConfig SetMessage(*Message) }
Implementing the interface provides way to define what message will be sent to the side of a user.
type Server ¶
type Server interface {
Serve(Context)
}
Implementing the interface provides the way to define how to handle updates.
type Session ¶
type Session struct { // ID of the chat of the user. ID SessionID Scope SessionScope // Custom value for each user. Data any // contains filtered or unexported fields }
The type represents current state of user interaction per each of them.
func NewSession ¶
func NewSession(bot *Bot, id SessionID, scope SessionScope) *Session
Return new empty session.
type SessionID ¶ added in v0.18.0
type SessionID int64
Represents unique value to identify chats. In fact is simply ID of the chat.
type SessionMap ¶
The type represents map of sessions using as key.
func (SessionMap) Add ¶
func (sm SessionMap) Add( bot *Bot, sid SessionID, scope SessionScope, ) *Session
Add new empty session by it's ID.
type SessionScope ¶
type SessionScope uint8
The way to determine where the context is related to.
const ( NoSessionScope SessionScope = iota PrivateSessionScope GroupSessionScope ChannelSessionScope )
type UI ¶
type UI []Component
The type that represents endpoint user interface via set of components that will work on the same screen in the same time.
type Update ¶
func (Update) DocumentID ¶ added in v0.18.0
func (Update) DocumentMimeType ¶
func (*Update) DocumentName ¶
func (Update) DocumentSize ¶
func (Update) HasDocument ¶
type UpdateChan ¶
type UpdateChan struct {
// contains filtered or unexported fields
}
The type represents general update channel.
func (*UpdateChan) Chan ¶
func (updates *UpdateChan) Chan() chan Update
func (*UpdateChan) Closed ¶
func (updates *UpdateChan) Closed() bool
Returns true if the channel is closed.
func (*UpdateChan) Read ¶
func (updates *UpdateChan) Read() (Update, bool)
Read an update from the channel.
func (*UpdateChan) Send ¶
func (updates *UpdateChan) Send(u Update) bool
Send an update to the channel. Returns true if the update was sent.
type Updater ¶ added in v0.22.0
Implementing the type provides way to update stuff on the client side. Things like panels, messages etc.
type WidgetSpecial ¶ added in v0.18.0
type WidgetSpecial int
func (WidgetSpecial) Render ¶ added in v0.18.0
func (w WidgetSpecial) Render(_ Context) UI
type WrongUpdateType ¶
type WrongUpdateType struct {
Type string
}
func (WrongUpdateType) Error ¶
func (wut WrongUpdateType) Error() string