Documentation ¶
Index ¶
- Constants
- type Bot
- type Command
- type CommandOption
- type CommandScope
- func CommandScopeAllChatAdministrators(lc ...string) CommandScope
- func CommandScopeAllGroupChats(lc ...string) CommandScope
- func CommandScopeAllPrivateChats(lc ...string) CommandScope
- func CommandScopeChat(chatID int64, lc ...string) CommandScope
- func CommandScopeChatAdministrators(chatID int64, lc ...string) CommandScope
- func CommandScopeChatMember(chatID, userID int64, lc ...string) CommandScope
- func CommandScopeDefault(lc ...string) CommandScope
- func CommandScopeNoScope() CommandScope
- type Context
- func (c *Context) Command() string
- func (c *Context) CommandArgs() string
- func (c *Context) FromChat() *tgbotapi.Chat
- func (c *Context) IsCommand() bool
- func (c *Context) Message() *tgbotapi.Message
- func (c *Context) ReplyHTML(text string, opts ...MessageOption) error
- func (c *Context) ReplyMarkdown(text string, opts ...MessageOption) error
- func (c *Context) ReplyText(text string, opts ...MessageOption) error
- func (c *Context) SendReply(chat tgbotapi.Chattable) error
- func (c *Context) SentFrom() *tgbotapi.User
- func (c *Context) Update() *tgbotapi.Update
- func (c *Context) WithContext(ctx context.Context) *Context
- type ErrHandler
- type Handler
- type MessageOption
- func WithChatId(chatId int64) MessageOption
- func WithDisableWebPagePreview(disable bool) MessageOption
- func WithHTML() MessageOption
- func WithInlineKeyboardMarkup(markup tgbotapi.InlineKeyboardMarkup) MessageOption
- func WithKeyboardMarkup(markup tgbotapi.ReplyKeyboardMarkup) MessageOption
- func WithMarkdown() MessageOption
- func WithMarkdownV2() MessageOption
- func WithRemoveKeyboard(markup tgbotapi.ReplyKeyboardRemove) MessageOption
- type Option
- func WithBufferSize(size int) Option
- func WithContext(ctx context.Context) Option
- func WithDisableAutoSetupCommands(v bool) Option
- func WithDisableHandleAllUpdateOnStop(v bool) Option
- func WithErrorHandler(h ErrHandler) Option
- func WithGetUpdatesAllowedUpdates(v ...string) Option
- func WithGetUpdatesLimit(limit int) Option
- func WithGetUpdatesTimeout(timeout int) Option
- func WithPanicHandler(h PanicHandler) Option
- func WithTimeout(d time.Duration) Option
- func WithUndefinedCmdHandler(h Handler) Option
- func WithUpdatesHandler(handler UpdatesHandler) Option
- func WithWorkersNum(n int) Option
- func WithWorkersPool(p Pool) Option
- type PanicHandler
- type Pool
- type UpdatesHandler
Examples ¶
Constants ¶
const ( ScopeTypeDefault = "default" ScopeTypeAllPrivateChats = "all_private_chats" ScopeTypeAllGroupChats = "all_group_chats" ScopeTypeAllChatAdministrators = "all_chat_administrators" ScopeTypeChat = "chat" ScopeTypeChatAdministrators = "chat_administrators" ScopeTypeChatMember = "chat_member" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
Bot wrapper the telegram bot.
func NewBot ¶
NewBot new a telegram bot.
Example ¶
api, err := tgbotapi.NewBotAPI("xxx") if err != nil { panic(err) } pool, err := ants.NewPool(10000, ants.WithExpiryDuration(10*time.Second)) if err != nil { panic(err) } bot := tgbot.NewBot(api, tgbot.WithTimeout(2*time.Second), tgbot.WithWorkersPool(tgbot.NewAntsPool(pool)), tgbot.WithUpdatesHandler(func(ctx *tgbot.Context) { err := ctx.ReplyText(ctx.Message().Text, func(c *tgbotapi.MessageConfig) { c.ReplyToMessageID = ctx.Message().MessageID }) if err != nil { log.Printf("reply text error: %s", err) } }), tgbot.WithUndefinedCmdHandler(func(ctx *tgbot.Context) error { return ctx.ReplyMarkdown("*unknown command*", tgbot.WithDisableWebPagePreview(false)) }), tgbot.WithErrorHandler(func(err error) { log.Println(err) }), ) bot.AddCommands( tgbot.NewCommand("ping", "ping the bot", func(ctx *tgbot.Context) error { return ctx.ReplyMarkdown("pong") }, tgbot.WithHide(true), tgbot.WithScopes( tgbot.CommandScopeDefault(), tgbot.CommandScopeAllGroupChats(), tgbot.CommandScopeChat(100), ), ), ) if err := bot.Run(); err != nil { panic(err) }
Output:
func (*Bot) AddCommands ¶
AddCommands add commands to the bot.
func (*Bot) ClearBotCommands ¶
func (*Bot) CommandsWithScope ¶
func (bot *Bot) CommandsWithScope() map[CommandScope][]*Command
type Command ¶
type Command struct { Name string Description string Handler Handler // contains filtered or unexported fields }
Command is telegram command.
func NewCommand ¶
func NewCommand(name, desc string, handler Handler, opts ...CommandOption) *Command
func (*Command) Scopes ¶
func (c *Command) Scopes() []CommandScope
type CommandOption ¶
type CommandOption func(cmd *Command)
func WithHide ¶
func WithHide(v bool) CommandOption
func WithScopes ¶
func WithScopes(scopes ...CommandScope) CommandOption
type CommandScope ¶
CommandScope is command scope for telegram.
func CommandScopeAllChatAdministrators ¶
func CommandScopeAllChatAdministrators(lc ...string) CommandScope
CommandScopeAllChatAdministrators represents the scope of bot commands, covering all group and supergroup chat administrators.
func CommandScopeAllGroupChats ¶
func CommandScopeAllGroupChats(lc ...string) CommandScope
CommandScopeAllGroupChats represents the scope of bot commands, covering all group and supergroup chats.
func CommandScopeAllPrivateChats ¶
func CommandScopeAllPrivateChats(lc ...string) CommandScope
CommandScopeAllPrivateChats represents the scope of bot commands, covering all private chats.
func CommandScopeChat ¶
func CommandScopeChat(chatID int64, lc ...string) CommandScope
CommandScopeChat represents the scope of bot commands, covering a specific chat.
func CommandScopeChatAdministrators ¶
func CommandScopeChatAdministrators(chatID int64, lc ...string) CommandScope
CommandScopeChatAdministrators represents the scope of bot commands, covering all administrators of a specific group or supergroup chat.
func CommandScopeChatMember ¶
func CommandScopeChatMember(chatID, userID int64, lc ...string) CommandScope
CommandScopeChatMember represents the scope of bot commands, covering a specific member of a group or supergroup chat.
func CommandScopeDefault ¶
func CommandScopeDefault(lc ...string) CommandScope
CommandScopeDefault represents the default scope of bot commands.
func CommandScopeNoScope ¶
func CommandScopeNoScope() CommandScope
type Context ¶
func (*Context) CommandArgs ¶
CommandArgs return command arguments if message is non-nil.
func (*Context) ReplyHTML ¶
func (c *Context) ReplyHTML(text string, opts ...MessageOption) error
ReplyHTML reply to the current chat, text format is HTML.
func (*Context) ReplyMarkdown ¶
func (c *Context) ReplyMarkdown(text string, opts ...MessageOption) error
ReplyMarkdown reply to the current chat, text format is markdown.
func (*Context) ReplyText ¶
func (c *Context) ReplyText(text string, opts ...MessageOption) error
ReplyText reply to the current chat.
type MessageOption ¶
type MessageOption func(c *tgbotapi.MessageConfig)
func WithDisableWebPagePreview ¶
func WithDisableWebPagePreview(disable bool) MessageOption
WithDisableWebPagePreview disable web page preview.
func WithInlineKeyboardMarkup ¶
func WithInlineKeyboardMarkup(markup tgbotapi.InlineKeyboardMarkup) MessageOption
WithInlineKeyboardMarkup set inline keyboard.
func WithKeyboardMarkup ¶
func WithKeyboardMarkup(markup tgbotapi.ReplyKeyboardMarkup) MessageOption
WithKeyboardMarkup set keyboard.
func WithMarkdownV2 ¶
func WithMarkdownV2() MessageOption
WithMarkdownV2 set parse mode to markdown v2.
func WithRemoveKeyboard ¶
func WithRemoveKeyboard(markup tgbotapi.ReplyKeyboardRemove) MessageOption
WithRemoveKeyboard remove keyboard.
type Option ¶
type Option func(o *options)
func WithBufferSize ¶
WithBufferSize set the buffer size for receive updates.
func WithDisableAutoSetupCommands ¶
WithDisableAutoSetupCommands disable auto setup telegram commands.
func WithDisableHandleAllUpdateOnStop ¶
WithDisableHandleAllUpdateOnStop disable handle all updates on stop.
func WithErrorHandler ¶
func WithErrorHandler(h ErrHandler) Option
WithErrorHandler set error handler.
func WithGetUpdatesAllowedUpdates ¶
WithGetUpdatesAllowedUpdates set allowed updates.
func WithGetUpdatesLimit ¶
WithGetUpdatesLimit set the get updates limit.
func WithGetUpdatesTimeout ¶
WithGetUpdatesTimeout set the get updates updateTimeout, timeout unit is seconds, max is 50 second.
func WithPanicHandler ¶
func WithPanicHandler(h PanicHandler) Option
WithPanicHandler set panic handler.
func WithUndefinedCmdHandler ¶
WithUndefinedCmdHandler set how to handle undefined commands.
func WithUpdatesHandler ¶
func WithUpdatesHandler(handler UpdatesHandler) Option
WithUpdatesHandler set the updates handler.
func WithWorkersNum ¶
WithWorkersNum set the number of workers to process updates.
func WithWorkersPool ¶
WithWorkersPool set the worker pool for execute handler if the workersPool is non-nil.
type Pool ¶
func NewAntsPool ¶
func NewAntsPool(p *ants.Pool) Pool