Documentation ¶
Overview ¶
Package base defines basic types and a wrapper around the original tgbotapi.BotAPI struct.
Index ¶
- func NewReplier(appenv *ApplicationEnv, reqenv *RequestEnv, msg *tgbotapi.Message) func(string)
- type ApplicationEnv
- type BotAPI
- func (bot *BotAPI) GetName() string
- func (bot *BotAPI) GetStandardAPI() *tgbotapi.BotAPI
- func (bot *BotAPI) Reply(msg *tgbotapi.Message, text string)
- func (bot *BotAPI) ReplyWithInlineKeyboard(msg *tgbotapi.Message, text string, buttons []tgbotapi.InlineKeyboardButton)
- func (bot *BotAPI) ReplyWithKeyboard(msg *tgbotapi.Message, text string, options []string)
- func (bot *BotAPI) ReplyWithMarkdown(msg *tgbotapi.Message, text string)
- func (bot *BotAPI) ReplyWithMessageCustomizer(msg *tgbotapi.Message, text string, customizer MessageCustomizer)
- func (bot *BotAPI) Request(c tgbotapi.Chattable) error
- func (bot *BotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
- func (bot *BotAPI) SetCommands(locpool *loc.Pool, langCodes []string, handlers []CommandHandler)
- type CallbackHandler
- type CommandHandler
- type CommandHandlerTrait
- type CommandScope
- type ExtendedBotAPI
- type FakeBotAPI
- func (bot *FakeBotAPI) ClearOutput()
- func (bot *FakeBotAPI) GetName() string
- func (bot *FakeBotAPI) GetOutput() interface{}
- func (bot *FakeBotAPI) GetStandardAPI() *tgbotapi.BotAPI
- func (bot *FakeBotAPI) Reply(_ *tgbotapi.Message, text string)
- func (bot *FakeBotAPI) ReplyWithInlineKeyboard(_ *tgbotapi.Message, text string, _ []tgbotapi.InlineKeyboardButton)
- func (bot *FakeBotAPI) ReplyWithKeyboard(_ *tgbotapi.Message, text string, _ []string)
- func (bot *FakeBotAPI) ReplyWithMarkdown(_ *tgbotapi.Message, text string)
- func (bot *FakeBotAPI) ReplyWithMessageCustomizer(_ *tgbotapi.Message, text string, _ MessageCustomizer)
- func (bot *FakeBotAPI) Request(c tgbotapi.Chattable) error
- func (bot *FakeBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
- func (bot *FakeBotAPI) SetCommands(*loc.Pool, []string, []CommandHandler)
- type InlineHandler
- type MessageCustomizer
- type MessageHandler
- type RequestEnv
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewReplier ¶
func NewReplier(appenv *ApplicationEnv, reqenv *RequestEnv, msg *tgbotapi.Message) func(string)
NewReplier is a shortcut to reply with text in the user's language. Example:
reply := base.NewReplier(handler.appenv, reqenv, msg) reply("messages.success")
Types ¶
type ApplicationEnv ¶
type ApplicationEnv struct { // Bot is used when you need to send a request to Telegram Bot API. Bot ExtendedBotAPI // Database is a reference to a [sql.DB]-like object. Database *pgxpool.Pool // Ctx is a context of the application; It's state will be switched to Done when the application is received the SIGTERM signal. Ctx context.Context }
ApplicationEnv is a container for all application scoped resources.
type BotAPI ¶
type BotAPI struct {
// contains filtered or unexported fields
}
BotAPI is a wrapper around the original tgbotapi.BotAPI struct. It implements the ExtendedBotAPI interface.
func (*BotAPI) GetStandardAPI ¶
func (*BotAPI) ReplyWithInlineKeyboard ¶
func (*BotAPI) ReplyWithKeyboard ¶
func (*BotAPI) ReplyWithMarkdown ¶
func (*BotAPI) ReplyWithMessageCustomizer ¶
func (bot *BotAPI) ReplyWithMessageCustomizer(msg *tgbotapi.Message, text string, customizer MessageCustomizer)
func (*BotAPI) Request ¶
Request is a simple wrapper around tgbotapi.BotAPI.Request.
func (*BotAPI) Send ¶
Send is an even simpler wrapper around tgbotapi.BotAPI.Send.
func (*BotAPI) SetCommands ¶
func (bot *BotAPI) SetCommands(locpool *loc.Pool, langCodes []string, handlers []CommandHandler)
type CallbackHandler ¶
type CallbackHandler interface { GetCallbackPrefix() string Handle(reqenv *RequestEnv, query *tgbotapi.CallbackQuery) }
CallbackHandler is a handler for the tgbotapi.CallbackQuery update type.
type CommandHandler ¶
type CommandHandler interface { MessageHandler GetCommands() []string GetScopes() []CommandScope }
CommandHandler is a MessageHandler which is considered as a public command that will be registered automatically. https://core.telegram.org/bots/api#setmycommands Inject CommandHandlerTrait to get the default implementation of CanHandle().
func ConvertHandlersToCommands ¶
func ConvertHandlersToCommands(handlers []MessageHandler) []CommandHandler
type CommandHandlerTrait ¶
type CommandHandlerTrait struct {
HandlerRefForTrait CommandHandler
}
func (CommandHandlerTrait) CanHandle ¶
func (t CommandHandlerTrait) CanHandle(_ *RequestEnv, msg *tgbotapi.Message) bool
type CommandScope ¶ added in v0.3.0
type CommandScope string
const ( CommandScopeDefault CommandScope = "default" CommandScopeAllPrivateChats CommandScope = "all_private_chats" CommandScopeAllGroupChats CommandScope = "all_group_chats" CommandScopeAllChatAdmins CommandScope = "all_chat_administrators" )
type ExtendedBotAPI ¶
type ExtendedBotAPI interface { // GetName returns the name of the bot got from the getMe() request. GetName() string // SetCommands for the bot. Use [ConvertHandlersToCommands] to pass [MessageHandler] as argument for this method. // Descriptions must be provided as keys for [loc.Context] in the format "commands.%s.description". // https://core.telegram.org/bots/api#setmycommands SetCommands(locpool *loc.Pool, langCodes []string, handlers []CommandHandler) // ReplyWithMessageCustomizer is the most common method to send text messages as a reply. Use this method if you want // to change several options like a message in Markdown with an inline keyboard. ReplyWithMessageCustomizer(*tgbotapi.Message, string, MessageCustomizer) // Reply with just a text message, without any customizations. Reply(msg *tgbotapi.Message, text string) ReplyWithMarkdown(msg *tgbotapi.Message, text string) // ReplyWithKeyboard uses a one time reply keyboard. // https://core.telegram.org/bots/api#replykeyboardmarkup ReplyWithKeyboard(msg *tgbotapi.Message, text string, options []string) // ReplyWithInlineKeyboard attaches an inline keyboard to the message. // https://core.telegram.org/bots/api#inlinekeyboardmarkup ReplyWithInlineKeyboard(msg *tgbotapi.Message, text string, buttons []tgbotapi.InlineKeyboardButton) // Request is the most common method that can be used to send any request to Telegram. Request(tgbotapi.Chattable) error // Send is like the Request method but returns the sent message back with non-empty ID field. Send(tgbotapi.Chattable) (tgbotapi.Message, error) // GetStandardAPI lets you use all standard methods of the library. GetStandardAPI() *tgbotapi.BotAPI }
ExtendedBotAPI is a set of more convenient methods to work with Telegram Bot API.
type FakeBotAPI ¶
type FakeBotAPI struct {
// contains filtered or unexported fields
}
FakeBotAPI is a mock for the BotAPI struct. Use the GetOutput() method to get either the text of the sent message, or the request itself.
func (*FakeBotAPI) ClearOutput ¶
func (bot *FakeBotAPI) ClearOutput()
ClearOutput deletes all data from internal buffers.
func (*FakeBotAPI) GetName ¶
func (bot *FakeBotAPI) GetName() string
func (*FakeBotAPI) GetOutput ¶
func (bot *FakeBotAPI) GetOutput() interface{}
GetOutput returns either a string after usage of Reply*() methods or a tgbotapi.Chattable after Request()
func (*FakeBotAPI) GetStandardAPI ¶
func (bot *FakeBotAPI) GetStandardAPI() *tgbotapi.BotAPI
func (*FakeBotAPI) ReplyWithInlineKeyboard ¶
func (bot *FakeBotAPI) ReplyWithInlineKeyboard(_ *tgbotapi.Message, text string, _ []tgbotapi.InlineKeyboardButton)
func (*FakeBotAPI) ReplyWithKeyboard ¶
func (bot *FakeBotAPI) ReplyWithKeyboard(_ *tgbotapi.Message, text string, _ []string)
func (*FakeBotAPI) ReplyWithMarkdown ¶
func (bot *FakeBotAPI) ReplyWithMarkdown(_ *tgbotapi.Message, text string)
func (*FakeBotAPI) ReplyWithMessageCustomizer ¶
func (bot *FakeBotAPI) ReplyWithMessageCustomizer(_ *tgbotapi.Message, text string, _ MessageCustomizer)
func (*FakeBotAPI) SetCommands ¶
func (bot *FakeBotAPI) SetCommands(*loc.Pool, []string, []CommandHandler)
type InlineHandler ¶
type InlineHandler interface { CanHandle(reqenv *RequestEnv, query *tgbotapi.InlineQuery) bool Handle(reqenv *RequestEnv, query *tgbotapi.InlineQuery) }
InlineHandler is a handler for the tgbotapi.InlineQuery update type.
type MessageCustomizer ¶
type MessageCustomizer func(msgConfig *tgbotapi.MessageConfig)
MessageCustomizer is a function that can change the message before it will be sent to Telegram. See BotAPI.ReplyWithMessageCustomizer for more information.
var ( NoOpCustomizer MessageCustomizer = func(msgConfig *tgbotapi.MessageConfig) {} MarkdownCustomizer MessageCustomizer = func(msgConfig *tgbotapi.MessageConfig) { msgConfig.ParseMode = tgbotapi.ModeMarkdown } )
type MessageHandler ¶
type MessageHandler interface { CanHandle(reqenv *RequestEnv, msg *tgbotapi.Message) bool Handle(reqenv *RequestEnv, msg *tgbotapi.Message) }
MessageHandler is a handler for the tgbotapi.Message update type.
type RequestEnv ¶
type RequestEnv struct { // Lang is a localization container. You can get a message in the user's language by key, using its [loc.Context.Tr] method. Lang *loc.Context // Options is a container for user options fetched from the database. Options settings.UserOptions }
RequestEnv is a container for all request related common resources. It's passed to all kinds of handlers.
func NewRequestEnv ¶
func NewRequestEnv(langCtx *loc.Context, opts settings.UserOptions) *RequestEnv