Documentation ¶
Overview ¶
Package tbcomctl provides common controls for telegram bots.
Index ¶
- Constants
- Variables
- func ButtonMarkup(b Boter, values []string, maxRowButtons int, cbFn func(*tb.Callback)) *tb.ReplyMarkup
- func ButtonPatternMarkup(b Boter, values []string, pattern []uint, cbFn func(*tb.Callback)) (*tb.ReplyMarkup, error)
- func ChatInfo(ch *tb.Chat) string
- func NewControllerChain(first Controller, cc ...Controller) func(m *tb.Message)
- func NewInputError(msg string) error
- func NoDebugLogger()
- func NoLogging()
- func Nvlstring(s string, ss ...string) string
- func Printer(lang string, fallback ...string) *message.Printer
- func PrivateOnly(fn func(m *tb.Message)) func(*tb.Message)
- func PrivateOnlyMsg(b Boter, msg string, fn func(m *tb.Message)) func(*tb.Message)
- func Sdump(m interface{}) 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 BotChecker
- type BotNotifier
- type Boter
- type BtnCallbackFunc
- type BtnLabel
- type Button
- type Controller
- type ErrFunc
- type ErrType
- type Error
- type Form
- func (fm *Form) Controller(name string) (Controller, bool)
- func (fm *Form) Data(r tb.Recipient) map[string]string
- func (fm *Form) Handler(m *tb.Message)
- func (fm *Form) OnTextMiddleware(onText func(m *tb.Message)) func(m *tb.Message)
- func (fm *Form) SetOverwrite(b bool) *Form
- func (fm *Form) SetRemoveButtons(b bool) *Form
- func (fm *Form) Value(ctrlName, recipient string) (string, bool)
- type Input
- func (c *Input) Bot() Boter
- func (c *Input) Form() *Form
- func (ip *Input) Handler(m *tb.Message)
- func (c *Input) Name() string
- func (ip *Input) OnTextMw(fn func(m *tb.Message)) func(*tb.Message)
- func (c *Input) OutgoingID(recipient string) (int, bool)
- func (c *Input) SetForm(fm *Form)
- func (c *Input) SetNext(ctrl Controller)
- func (c *Input) SetPrev(ctrl Controller)
- func (c *Input) SetValue(recipient string, value string)
- func (c *Input) Value(recipient string) (string, bool)
- type InputOption
- type KbdOption
- type Keyboard
- func (c *Keyboard) Bot() Boter
- func (c *Keyboard) Form() *Form
- func (k *Keyboard) InitForLanguages(lang ...string)
- func (k *Keyboard) Markup(lang string) *tb.ReplyMarkup
- func (c *Keyboard) Name() string
- func (c *Keyboard) OutgoingID(recipient string) (int, bool)
- func (c *Keyboard) SetForm(fm *Form)
- func (c *Keyboard) SetNext(ctrl Controller)
- func (c *Keyboard) SetPrev(ctrl Controller)
- func (c *Keyboard) SetValue(recipient string, value string)
- func (c *Keyboard) Value(recipient string) (string, bool)
- type KeyboardCmd
- type KeyboardCommands
- type Logger
- type Message
- func (c *Message) Bot() Boter
- func (c *Message) Form() *Form
- func (m *Message) Handler(msg *tb.Message)
- func (c *Message) Name() string
- func (c *Message) OutgoingID(recipient string) (int, bool)
- func (c *Message) SetForm(fm *Form)
- func (c *Message) SetNext(ctrl Controller)
- func (c *Message) SetPrev(ctrl Controller)
- func (c *Message) SetValue(recipient string, value string)
- func (c *Message) Value(recipient string) (string, bool)
- type MiddlewareFunc
- type MsgErrFunc
- type PBOption
- type Picklist
- func (c *Picklist) Bot() Boter
- func (p *Picklist) Callback(cb *tb.Callback)
- func (c *Picklist) Form() *Form
- func (p *Picklist) Handler(m *tb.Message)
- func (c *Picklist) Name() string
- func (c *Picklist) OutgoingID(recipient string) (int, bool)
- func (c *Picklist) SetForm(fm *Form)
- func (b Picklist) SetMaxButtons(n int)
- func (c *Picklist) SetNext(ctrl Controller)
- func (c *Picklist) SetPrev(ctrl Controller)
- func (c *Picklist) SetValue(recipient string, value string)
- func (c *Picklist) Value(recipient string) (string, bool)
- type PicklistOption
- func PickOptBtnPattern(pattern []uint) PicklistOption
- func PickOptErrFunc(fn ErrFunc) 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 (c *Rating) Bot() Boter
- func (c *Rating) Form() *Form
- func (rb *Rating) Markup(btns [2]Button) *tb.ReplyMarkup
- func (c *Rating) Name() string
- func (c *Rating) OutgoingID(recipient string) (int, bool)
- func (c *Rating) SetForm(fm *Form)
- func (c *Rating) SetNext(ctrl Controller)
- func (c *Rating) SetPrev(ctrl Controller)
- func (c *Rating) SetValue(recipient string, value string)
- func (c *Rating) Value(recipient string) (string, bool)
- type RatingFunc
- type RatingType
- type SCOption
- type StoredMessage
- type SubChecker
- func (c *SubChecker) Bot() Boter
- func (c *SubChecker) Form() *Form
- func (sc *SubChecker) Handler(m *tb.Message)
- func (c *SubChecker) Name() string
- func (c *SubChecker) OutgoingID(recipient string) (int, bool)
- func (c *SubChecker) SetForm(fm *Form)
- func (c *SubChecker) SetNext(ctrl Controller)
- func (c *SubChecker) SetPrev(ctrl Controller)
- func (c *SubChecker) SetValue(recipient string, value string)
- func (c *SubChecker) Value(recipient string) (string, bool)
- type TextFunc
- type ValuesFunc
Constants ¶
const ( MsgUnexpected = "🤯 (500) Unexpected error occurred." MsgRetry = "Incorrect choice." MsgChooseVal = "Choose 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 = "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"} )
var ErrAlreadyVoted = errors.New("already voted")
Functions ¶
func ButtonMarkup ¶
func ButtonMarkup(b Boter, values []string, maxRowButtons int, cbFn func(*tb.Callback)) *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 ¶ added in v0.2.4
func NewControllerChain ¶
func NewControllerChain(first Controller, cc ...Controller) func(m *tb.Message)
func NewInputError ¶
NewInputError returns an input error with msg.
func PrivateOnly ¶
PrivateOnly is the middleware that restricts the handler to only private messages.
func PrivateOnlyMsg ¶
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
Types ¶
type BotChecker ¶
type BotNotifier ¶
type Boter ¶
type Boter interface { Handle(endpoint interface{}, handler interface{}) Send(to tb.Recipient, what interface{}, options ...interface{}) (*tb.Message, error) Edit(msg tb.Editable, what interface{}, options ...interface{}) (*tb.Message, error) Respond(c *tb.Callback, resp ...*tb.CallbackResponse) error }
Boter is the interface to send messages.
type BtnCallbackFunc ¶
BtnCallbackFunc is being called once the user picks the value, it should return error if the value is incorrect, or ErrRetry if the retry should be performed.
type Controller ¶
type Controller interface { // Handler is the controller's message handler. Handler(m *tb.Message) // 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) // Bot returns the bot that is used to inialise the controller. Bot() Boter // 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 of the common controls implement. Controllers can be chained together
func ControllerFromCtx ¶
func ControllerFromCtx(ctx context.Context) (Controller, bool)
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
func NewForm ¶
func NewForm(ctrls ...Controller) *Form
func (*Form) Controller ¶
func (fm *Form) Controller(name string) (Controller, bool)
Controller returns the Form Controller by it's name.
func (*Form) OnTextMiddleware ¶
OnTextMiddleware returns the middleware for OnText handler.
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 Input ¶
type Input struct { // UniqName is the unique name of the field (used to create pipelines, not // shown to the user) UniqName string // OnTextFn is the message callback function called when user responds. If // it returns the error, user will be informed about it. OnTextFn MsgErrFunc // contains filtered or unexported fields }
func NewInput ¶
func NewInput(b BotNotifier, name string, textFn TextFunc, onTextFn MsgErrFunc, 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. msgFn is the function that should produce the text that user initially sees, onTextFn is the function that should process the user input. It should return an error if the user input is not accepted, and then user is offered to retry. It can format the return error with fmt.Errorf, as this is what user will see. next is allowed to be nil.
func NewInputText ¶
func NewInputText(b BotNotifier, name string, text string, onTextFn MsgErrFunc, opts ...InputOption) *Input
func (*Input) OnTextMw ¶
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) SetNext ¶
func (c *Input) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Input) SetPrev ¶
func (c *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
type KbdOption ¶ added in v0.2.5
type KbdOption func(k *Keyboard)
func KbdOptButtonsInRow ¶ added in v0.2.5
type Keyboard ¶
type Keyboard struct {
// contains filtered or unexported fields
}
func NewKeyboard ¶
func NewKeyboard(b Boter, cmds KeyboardCommands, opts ...KbdOption) *Keyboard
func (*Keyboard) InitForLanguages ¶
InitForLanguages initialises handlers for languages listed.
func (*Keyboard) Markup ¶
func (k *Keyboard) Markup(lang string) *tb.ReplyMarkup
Markup returns the markup to be sent to user.
func (*Keyboard) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*Keyboard) SetNext ¶
func (c *Keyboard) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Keyboard) SetPrev ¶
func (c *Keyboard) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type KeyboardCmd ¶ added in v0.2.6
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.
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) SetNext ¶
func (c *Message) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Message) SetPrev ¶
func (c *Message) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type MsgErrFunc ¶
MsgErrFunc is the function that processes the user input. If the input is invalid, it should return InputError with the message, then the user is offered to retry the input.
type Picklist ¶
type Picklist struct {
// contains filtered or unexported fields
}
func NewPicklist ¶
func NewPicklist(b Boter, name string, textFn TextFunc, valuesFn ValuesFunc, callbackFn BtnCallbackFunc, opts ...PicklistOption) *Picklist
NewPicklist creates a new picklist.
func NewPicklistText ¶
func NewPicklistText(b Boter, name string, text string, values []string, callbackFn BtnCallbackFunc, opts ...PicklistOption) *Picklist
NewPicklistText is a convenience function to return picklist with fixed text and values.
func (*Picklist) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (Picklist) SetMaxButtons ¶
func (b Picklist) SetMaxButtons(n int)
func (*Picklist) SetNext ¶
func (c *Picklist) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Picklist) SetPrev ¶
func (c *Picklist) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.
type PicklistOption ¶
type PicklistOption func(p *Picklist)
func PickOptBtnPattern ¶ added in v0.2.4
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 PickOptErrFunc ¶
func PickOptErrFunc(fn ErrFunc) 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(b Boter, callbackFn func(cb *tb.Callback), 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(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 (*Rating) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*Rating) SetNext ¶
func (c *Rating) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*Rating) SetPrev ¶
func (c *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 ¶
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(b BotChecker, name string, textFn TextFunc, chats []string, 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) Handler ¶
func (sc *SubChecker) Handler(m *tb.Message)
func (*SubChecker) OutgoingID ¶
OutgoingID returns the controller's outgoing message ID for the user.
func (*SubChecker) SetNext ¶
func (c *SubChecker) SetNext(ctrl Controller)
SetNext sets next controller in the chain.
func (*SubChecker) SetPrev ¶
func (c *SubChecker) SetPrev(ctrl Controller)
SetPrev sets the previous controller in the chain.