Documentation
¶
Overview ¶
Package tbcomctl provides common controls for telegram bots.
Index ¶
- Constants
- Variables
- func ButtonMarkup(c tb.Context, values []string, maxRowButtons int, ...) *tb.ReplyMarkup
- func ButtonPatternMarkup(c tb.Context, values []string, pattern []uint, cbFn tb.HandlerFunc) (*tb.ReplyMarkup, error)
- func ChatInfo(ch *tb.Chat) string
- func NewControllerChain(first Controller, cc ...Controller) tb.HandlerFuncdeprecated
- func NewInputError(msg string) error
- func NoDebugLogger()
- func NoLogging()
- func Nvlstring(s string, ss ...string) string
- func OrganizeButtons(btns []tb.Btn, btnInRow int) []tb.Row
- func Printer(lang string, fallback ...string) *message.Printer
- func PrinterContext(c tb.Context, fallback ...string) *message.Printer
- func PrivateOnly(fn tb.HandlerFunc) tb.HandlerFunc
- func PrivateOnlyMsg(msg string, fn tb.HandlerFunc) tb.HandlerFunc
- func Sdump(m interface{}) string
- func SenderInfo(c tb.Context) string
- func SetDebugLogger(l Logger)
- func SetLogger(l Logger)
- func Userinfo(u *tb.User) string
- func WithController(ctx context.Context, ctrl Controller) context.Context
- type BtnLabel
- type Button
- type Callbacker
- type Controller
- type ErrType
- type Error
- type ErrorHandler
- type Form
- func (fm *Form) Controller(name string) (Controller, bool)
- func (fm *Form) Data(r tb.Recipient) map[string]string
- func (fm *Form) Handler(c tb.Context) error
- func (fm *Form) OnTextMiddleware(onText tb.HandlerFunc) tb.HandlerFunc
- func (fm *Form) SetOverwrite(b bool) *Form
- func (fm *Form) SetRemoveButtons(b bool) *Form
- func (fm *Form) Value(ctrlName, recipient string) (string, bool)
- type HandleContextFunc
- type Input
- func (cc *Input) Form() *Form
- func (ip *Input) Handler(c tb.Context) error
- func (cc *Input) Name() string
- func (ip *Input) OnTextMw(fn tb.HandlerFunc) tb.HandlerFunc
- func (cc *Input) OutgoingID(recipient string) (int, bool)
- func (cc *Input) SetForm(fm *Form)
- func (cc *Input) SetNext(ctrl Controller)
- func (cc *Input) SetPrev(ctrl Controller)
- func (cc *Input) SetValue(recipient string, value string)
- func (cc *Input) Value(recipient string) (string, bool)
- type InputOption
- type KbdOption
- type Keyboard
- func (cc *Keyboard) Form() *Form
- func (k *Keyboard) InitForLanguages(b *tb.Bot, lang ...string)
- func (k *Keyboard) Markup(b *tb.Bot, lang string) *tb.ReplyMarkup
- func (cc *Keyboard) Name() string
- func (cc *Keyboard) OutgoingID(recipient string) (int, bool)
- func (cc *Keyboard) SetForm(fm *Form)
- func (cc *Keyboard) SetNext(ctrl Controller)
- func (cc *Keyboard) SetPrev(ctrl Controller)
- func (cc *Keyboard) SetValue(recipient string, value string)
- func (cc *Keyboard) Value(recipient string) (string, bool)
- type KeyboardCmd
- type KeyboardCommands
- type Logger
- type Message
- func (cc *Message) Form() *Form
- func (m *Message) Handler(c tb.Context) error
- func (cc *Message) Name() string
- func (cc *Message) OutgoingID(recipient string) (int, bool)
- func (cc *Message) SetForm(fm *Form)
- func (cc *Message) SetNext(ctrl Controller)
- func (cc *Message) SetPrev(ctrl Controller)
- func (cc *Message) SetValue(recipient string, value string)
- func (cc *Message) Value(recipient string) (string, bool)
- type PBOption
- type Picklist
- func (cc *Picklist) Form() *Form
- func (p *Picklist) Handler(c tb.Context) error
- func (cc *Picklist) Name() string
- func (cc *Picklist) OutgoingID(recipient string) (int, bool)
- func (cc *Picklist) SetForm(fm *Form)
- func (b Picklist) SetMaxButtons(n int)
- func (cc *Picklist) SetNext(ctrl Controller)
- func (cc *Picklist) SetPrev(ctrl Controller)
- func (cc *Picklist) SetValue(recipient string, value string)
- func (cc *Picklist) Value(recipient string) (string, bool)
- type PicklistOption
- func PickOptBtnBack(texter Texter) PicklistOption
- func PickOptBtnPattern(pattern []uint) PicklistOption
- func PickOptDefaultSendOptions(opts *tb.SendOptions) PicklistOption
- func PickOptErrHandler(h ErrorHandler) PicklistOption
- func PickOptFallbackLang(lang string) PicklistOption
- func PickOptMaxInlineButtons(n int) PicklistOption
- func PickOptNoUpdate(b bool) PicklistOption
- func PickOptOverwrite(b bool) PicklistOption
- func PickOptPrivateOnly(b bool) PicklistOption
- func PickOptRemoveButtons(b bool) PicklistOption
- type PostButtons
- type RBOption
- type Rating
- func (cc *Rating) Form() *Form
- func (rb *Rating) Markup(b *tb.Bot, btns [2]Button) *tb.ReplyMarkup
- func (cc *Rating) Name() string
- func (cc *Rating) OutgoingID(recipient string) (int, bool)
- func (cc *Rating) SetForm(fm *Form)
- func (cc *Rating) SetNext(ctrl Controller)
- func (cc *Rating) SetPrev(ctrl Controller)
- func (cc *Rating) SetValue(recipient string, value string)
- func (cc *Rating) Value(recipient string) (string, bool)
- type RatingFunc
- type RatingType
- type SCOption
- type StoredMessage
- type SubChecker
- func (cc *SubChecker) Form() *Form
- func (sc *SubChecker) Handler(c tb.Context) error
- func (cc *SubChecker) Name() string
- func (cc *SubChecker) OutgoingID(recipient string) (int, bool)
- func (cc *SubChecker) SetForm(fm *Form)
- func (cc *SubChecker) SetNext(ctrl Controller)
- func (cc *SubChecker) SetPrev(ctrl Controller)
- func (cc *SubChecker) SetValue(recipient string, value string)
- func (cc *SubChecker) Value(recipient string) (string, bool)
- type TVC
- func (t *TVC) Callback(ctx context.Context, c tb.Context) error
- func (t *TVC) OnError(ctx context.Context, c tb.Context, err error) error
- func (t *TVC) Text(ctx context.Context, c tb.Context) (string, error)
- func (t *TVC) Values(ctx context.Context, c tb.Context) ([]string, error)
- func (t *TVC) WithCallbackFn(fn HandleContextFunc) *TVC
- func (t *TVC) WithErrorFn(fn func(ctx context.Context, c tb.Context, err error)) *TVC
- func (t *TVC) WithTextFn(fn func(context.Context, tb.Context) (string, error)) *TVC
- func (t *TVC) WithValuesFn(fn func(context.Context, tb.Context) ([]string, error)) *TVC
- type TextCallbacker
- type TextValueCallbacker
- type TextValuer
- type Texter
- type ValueResolver
- type Valuer
Constants ¶
const ( MsgUnexpected = "🤯 (500) Unexpected error occurred." MsgRetry = "Incorrect choice." MsgChooseVal = "Choose the value from the list:" MsgOK = "✅" MsgVoteCounted = "✅ Vote counted." MsgSubCheck = "? Check subscription >>" MsgSubNoSub = "❌ You're not subscribed to one or more of the required channels." )
const (
// FallbackLang is the default fallback language.
FallbackLang = "en-US"
)
const (
None = "<none>"
)
Variables ¶
var ( // ErrRetry should be returned by CallbackFunc if the retry should be performed. ErrRetry = &Error{Type: TErrRetry, Msg: "retry", Alert: true} // ErrNoChange should be returned if the user picked the same value as before, and no update needed. ErrNoChange = &Error{Type: TErrNoChange, Msg: "no change"} // BackPressed is a special type of error indicating that callback handler should call the previous handler. BackPressed = errors.New("back") //lint:ignore ST1012 it is what it is )
var ErrAlreadyVoted = errors.New("already voted")
Functions ¶
func ButtonMarkup ¶
func ButtonMarkup(c tb.Context, values []string, maxRowButtons int, cbFn func(c tb.Context) error) *tb.ReplyMarkup
ButtonMarkup returns the button markup for the message. It creates handlers for all the buttons assigning the cbFn callback function to each of them. Values must be unique. maxRowButtons is maximum number of buttons in a row.
func ButtonPatternMarkup ¶
func ButtonPatternMarkup(c tb.Context, values []string, pattern []uint, cbFn tb.HandlerFunc) (*tb.ReplyMarkup, error)
func NewControllerChain
deprecated
func NewControllerChain(first Controller, cc ...Controller) tb.HandlerFunc
NewControllerChain returns the controller chain.
Deprecated: use NewForm instead. NewControllerChain will be removed in the next versions.
func NewInputError ¶
NewInputError returns an input error with msg.
func OrganizeButtons ¶ added in v4.0.2
OrganizeButtons organizes buttons in rows, at most btnInRow per row.
func Printer ¶
Printer returns the Message Printer for the desired lang. If the lang is not valid, the fallback languages will be used, if set.
func PrinterContext ¶
PrinterContext returns the Message Printer set to the language of the sender. It is a convenience wrapper around Printer.
func PrivateOnly ¶
func PrivateOnly(fn tb.HandlerFunc) tb.HandlerFunc
PrivateOnly is the middleware that restricts the handler to only private messages.
func PrivateOnlyMsg ¶
func PrivateOnlyMsg(msg string, fn tb.HandlerFunc) tb.HandlerFunc
PrivateOnlyMsg returns the handler that will reject non-private messages (eg. sent in groups) with i18n formatted message.
func SenderInfo ¶
SenderInfo is the convenience function to log the sender info in the context.
func SetDebugLogger ¶
func SetDebugLogger(l Logger)
SetDebugLogger sets the debug logger which is used to output debug messages, if you must. By default, debug logging is disabled.
func WithController ¶
func WithController(ctx context.Context, ctrl Controller) context.Context
WithController adds the controller to the context.
Types ¶
type Callbacker ¶
type Callbacker interface { // Callback should process the handler's callback. // TODO: describe supported error return values. Callback(ctx context.Context, c tb.Context) error }
Callbacker defines the interface for the callback function.
type Controller ¶
type Controller interface { // Handler is the controller's message handler. Handler(c tb.Context) error // Name returns the name of the control assigned to it on creation. When // Controller is a part of a form, one can call Form.Controller(name) method // to get the controller. Name() string // SetNext sets the next handler, when control is part of a form. SetNext(Controller) // SetPrev sets the previous handler. SetPrev(Controller) // SetForm assigns the form to the controller, this will allow controller to // address other controls in a form by name. SetForm(*Form) // Form returns the form associated with the controller. Form() *Form // Value returns the value stored in the controller for the recipient. Value(recipient string) (string, bool) // OutgoingID should return the value of the outgoing message ID for the // user and true if the message is present or false otherwise. OutgoingID(recipient string) (int, bool) }
Controller is the interface that some common controls implement. Controllers can be chained together. Controller is everything that can interact with user, i.e. a message that waits for the user input, or a picklist that presents the user with a set of buttons. Caller may want to create a custom controller to include in the form.
func ControllerFromCtx ¶
func ControllerFromCtx(ctx context.Context) (Controller, bool)
ControllerFromCtx returns the controller from the context.
type ErrorHandler ¶
type ErrorHandler interface { // OnError should process the error. OnError(ctx context.Context, c tb.Context, err error) }
ErrorHandler is an optional interface for some controls, that the caller might implement so that errors occurred while handling the Callback could be handled by the caller as well.
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
Form is an abstraction that presents controllers in a way of a form. It can be viewed as an interactive form that user might fill in. Each of the controllers records the value that was entered, or selected, by the user. At any stage, caller may call the Data member function providing User ID as an argument, and that will return all the values in a mapping between the controller name and the user input that will contain all the values, entered by the user so far.
func NewForm ¶
func NewForm(ctrls ...Controller) *Form
NewForm creates a new Form from a set of Controllers. The Controllers will be called in the same order they will appear in the argument list. Controllers must all have a unique name (within a form), otherwise NewForm will panic.
func (*Form) Controller ¶
func (fm *Form) Controller(name string) (Controller, bool)
Controller returns the Form Controller by it's name.
func (*Form) Handler ¶
Handler is the form handler. It calls the handler of the first controller in the chain.
func (*Form) OnTextMiddleware ¶
func (fm *Form) OnTextMiddleware(onText tb.HandlerFunc) tb.HandlerFunc
OnTextMiddleware returns the middleware for the OnText handler.
var f Form tb.Handle(OnText, f.OnTextMiddleware(/*other handlers*/))
func (*Form) SetOverwrite ¶
SetOverwrite sets the overwrite flag on all controllers within the form.
func (*Form) SetRemoveButtons ¶
SetRemoveButtons sets the remove buttons flag on all controllers within the form.
type HandleContextFunc ¶
HandleContextFunc is the callback function that is being called within controller callbacks. It should return the errors specific to controller requirements for the input to be processed.
type Input ¶
type Input struct { // UniqName is the unique name of the field (used to create pipelines, not // shown to the user) UniqName string // contains filtered or unexported fields }
func NewInput ¶
func NewInput(name string, tc TextCallbacker, opts ...InputOption) *Input
NewInput text creates a new text input, optionally chaining with the `next` handler. One must use Handle as a handler for bot endpoint, and then hook the OnText to OnTextMw. TextCallbacker.Text should produce the text that user initially sees, TextCallbacker.Callback is the function that should process the user input. It should return an InputError if the user input is not accepted, and in this case the user is offered to retry the input. It can format the return error with fmt.Errorf, as this is what will be presented to the user.
func NewInputText ¶
func NewInputText(name string, text string, onTextFn HandleContextFunc, opts ...InputOption) *Input
NewIntputText is the shortcut to create the Input instance with static text. onTextFn should return InputError if the user input is not valid.
func (*Input) OnTextMw ¶
func (ip *Input) OnTextMw(fn tb.HandlerFunc) tb.HandlerFunc
OnTextMw returns the middleware that should wrap the OnText handler. It will process the message only if control awaits for this particular user input.
func (*Input) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*Input) SetForm ¶
func (cc *Input) SetForm(fm *Form)
SetForm links the controller to the form.
func (*Input) SetNext ¶
func (cc *Input) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Input) SetPrev ¶
func (cc *Input) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type InputOption ¶
type InputOption func(*Input)
func IOptNoReply ¶
func IOptNoReply(b bool) InputOption
func IOptPrivateOnly ¶
func IOptPrivateOnly(b bool) InputOption
func IOptValueResolver ¶ added in v4.0.1
func IOptValueResolver(fn ValueResolver) InputOption
type Keyboard ¶
type Keyboard struct {
// contains filtered or unexported fields
}
func NewKeyboard ¶
func NewKeyboard(cmds KeyboardCommands, opts ...KbdOption) *Keyboard
func (*Keyboard) InitForLanguages ¶
InitForLanguages initialises handlers for languages listed.
func (*Keyboard) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*Keyboard) SetForm ¶
func (cc *Keyboard) SetForm(fm *Form)
SetForm links the controller to the form.
func (*Keyboard) SetNext ¶
func (cc *Keyboard) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Keyboard) SetPrev ¶
func (cc *Keyboard) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type KeyboardCmd ¶
type KeyboardCmd struct { Label BtnLabel Handler tb.HandlerFunc }
type KeyboardCommands ¶
type KeyboardCommands []KeyboardCmd
type Logger ¶
type Logger interface { Print(v ...interface{}) Println(v ...interface{}) Printf(format string, a ...interface{}) }
Logger is the interface for logging.
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message is the controller that sends a message. It can be used to send a confirmation message at the end of the Form.
func NewMessage ¶
NewMessage creates new Message Controller. One must pass Bot instance, name of the controller, text function that returns the desired message and optionally any sendOpts that will be supplied to telebot.Bot.Send.
func NewMessageText ¶
NewMessageText is a convenience wrapper for NewMessage with a fixed text.
func (*Message) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*Message) SetForm ¶
func (cc *Message) SetForm(fm *Form)
SetForm links the controller to the form.
func (*Message) SetNext ¶
func (cc *Message) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Message) SetPrev ¶
func (cc *Message) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type Picklist ¶
type Picklist struct {
// contains filtered or unexported fields
}
func NewPicklist ¶
func NewPicklist(name string, tvc TextValueCallbacker, opts ...PicklistOption) *Picklist
NewPicklist creates a new picklist.
func (*Picklist) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*Picklist) SetForm ¶
func (cc *Picklist) SetForm(fm *Form)
SetForm links the controller to the form.
func (Picklist) SetMaxButtons ¶
func (b Picklist) SetMaxButtons(n int)
func (*Picklist) SetNext ¶
func (cc *Picklist) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Picklist) SetPrev ¶
func (cc *Picklist) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type PicklistOption ¶
type PicklistOption func(p *Picklist)
func PickOptBtnBack ¶
func PickOptBtnBack(texter Texter) PicklistOption
func PickOptBtnPattern ¶
func PickOptBtnPattern(pattern []uint) PicklistOption
PickOptBtnPattern sets the inline markup button pattern. Each unsigned integer in the pattern represents the number of buttons shown on each of the rows.
Example:
pattern: []uint{1, 2, 3} will produce the following markup for the picklist choices +-------------------+ | Picklist text | +-------------------+ | button 1 | +---------+---------+ | button 2| button 3| +------+--+---+-----+ | btn4 | btn5 | btn6| +------+------+-----+
func PickOptDefaultSendOptions ¶
func PickOptDefaultSendOptions(opts *tb.SendOptions) PicklistOption
PickOptDefaultSendOptions allows to set the default send options
func PickOptErrHandler ¶
func PickOptErrHandler(h ErrorHandler) PicklistOption
func PickOptFallbackLang ¶
func PickOptFallbackLang(lang string) PicklistOption
func PickOptMaxInlineButtons ¶
func PickOptMaxInlineButtons(n int) PicklistOption
func PickOptNoUpdate ¶
func PickOptNoUpdate(b bool) PicklistOption
PickOptNoUpdate sets the No Update option. If No Update is set, the text is not updated once the user makes their choice.
func PickOptOverwrite ¶
func PickOptOverwrite(b bool) PicklistOption
func PickOptPrivateOnly ¶
func PickOptPrivateOnly(b bool) PicklistOption
func PickOptRemoveButtons ¶
func PickOptRemoveButtons(b bool) PicklistOption
PickOptRemoveButtons set the Remove Buttons option. If Remove Buttons is set, the inline buttons will be removed once the user make the choice.
type PostButtons ¶
type PostButtons struct {
// contains filtered or unexported fields
}
func NewPostButtons ¶
func NewPostButtons(callbackFn func(c tb.Context) error, opts ...PBOption) *PostButtons
NewPostButtons creates an instance of PostButtons. The callbackFunction is the function that will be assigned and called for each button press, so it should handle all possible values.
func (*PostButtons) Markup ¶
func (pb *PostButtons) Markup(c tb.Context, labels []string, pattern ...uint) (*tb.ReplyMarkup, error)
Markup returns the markup with buttons labeled with labels.
func (PostButtons) SetMaxButtons ¶
func (b PostButtons) SetMaxButtons(n int)
type RBOption ¶
type RBOption func(*Rating)
func RBOptShowPostRating ¶
RBOptShowPostRating enables counter of total upvotes/downvotes.
func RBOptShowVoteCounter ¶
RBOptShowVoteCounter enables post rating between up/down vote buttons
type Rating ¶
type Rating struct {
// contains filtered or unexported fields
}
Rating is a struct for attaching post rating.
func NewRating ¶
func NewRating(fn RatingFunc, opts ...RBOption) *Rating
func (*Rating) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*Rating) SetForm ¶
func (cc *Rating) SetForm(fm *Form)
SetForm links the controller to the form.
func (*Rating) SetNext ¶
func (cc *Rating) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Rating) SetPrev ¶
func (cc *Rating) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type RatingFunc ¶
RatingFunc is the function called by callback, given the message, user and the button index it should update the records and return the new buttons with updated values for the posting, it must maintain count of votes inhouse.
type RatingType ¶
type RatingType int
type StoredMessage ¶
StoredMessage represents the stored message in the database.
func (StoredMessage) MessageSig ¶
func (m StoredMessage) MessageSig() (string, int64)
type SubChecker ¶
type SubChecker struct {
// contains filtered or unexported fields
}
SubChecker is controller to check the chat subscription.
func NewSubChecker ¶
func NewSubChecker(name string, t Texter, chats []int64, opts ...SCOption) *SubChecker
NewSubChecker creates new subscription checker that checks the subscription on the desired channels. Boter must be added to channels for this to work.
func (*SubChecker) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*SubChecker) SetForm ¶
func (cc *SubChecker) SetForm(fm *Form)
SetForm links the controller to the form.
func (*SubChecker) SetNext ¶
func (cc *SubChecker) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*SubChecker) SetPrev ¶
func (cc *SubChecker) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type TVC ¶
type TVC struct { TextFn func(context.Context, tb.Context) (string, error) ValuesFn func(context.Context, tb.Context) ([]string, error) CBfn HandleContextFunc ErrorFn func(ctx context.Context, c tb.Context, err error) }
TVC is an implementation of TextValuerCallbacker.
func NewStaticTVC ¶
func NewStaticTVC(text string, values []string, callbackFn HandleContextFunc) *TVC
NewStaticTVC is a convenience constructor for TVC (TextValueCallbacker) with static text and values.
func (*TVC) Text ¶
Text callse the TextFn with contexts. ctx is legacy from the times when there was not telebot.Context.
func (*TVC) WithCallbackFn ¶
func (t *TVC) WithCallbackFn(fn HandleContextFunc) *TVC
WithCallbackFn sets the callback function to fn.
func (*TVC) WithErrorFn ¶
WithErrorFn sets the error handling function to fn.
func (*TVC) WithTextFn ¶
WithTextFn sets the text function to fn.
type TextCallbacker ¶
type TextCallbacker interface { Texter Callbacker }
TextCallbacker combines Texter and Callbacker interfaces.
type TextValueCallbacker ¶
type TextValueCallbacker interface { Texter Valuer Callbacker }
TextValueCallbacker combines Texter, Valuer and Callbacker interfaces.
type TextValuer ¶
TextValuer combines Texter and Valuer interfaces.