Documentation ¶
Index ¶
- Constants
- Variables
- type DefaultMux
- func (dm *DefaultMux) DefaultHandler() *Handler
- func (dm *DefaultMux) FileHandler() *Handler
- func (dm *DefaultMux) HandleDefault(handler HandlerFunction, description ...string)
- func (dm *DefaultMux) HandleFile(handler HandlerFunction, description ...string)
- func (dm *DefaultMux) HandleFunc(path string, handler HandlerFunction, description ...string)
- func (dm *DefaultMux) Handlers() Handlers
- func (dm *DefaultMux) Mux(msg *Message) (*Handler, MessageVars)
- func (db *DefaultMux) Reset(int64)
- func (dm *DefaultMux) SetAlias(route string, aliases ...string)
- type Handler
- type HandlerFunction
- type Handlers
- type InMemoryStorage
- type InlineKeyboardButtonsOption
- type KeyboardOption
- type Message
- func (m *Message) Download(dir string) error
- func (m *Message) Reply(reply string, options ...MessageOption)
- func (m *Message) ReplyAudio(filepath string)
- func (m *Message) ReplyDocument(filepath string)
- func (m *Message) ReplyInlineKeyboard(text string, inlineButtons []map[string]string, ...)
- func (m *Message) ReplyKeyboard(text string, buttons [][]string, options ...KeyboardOption)
- func (m *Message) ReplyLocation(longitude, latitude float64)
- func (m *Message) ReplyPhoto(filepath string, caption ...string)
- func (m *Message) ReplySticker(filepath string)
- func (m *Message) ReplyVideo(filepath string, caption ...string)
- func (m *Message) Replyf(reply string, values ...interface{})
- func (m *Message) RequestContactButton(text string, button string, options ...KeyboardOption)
- func (m *Message) RequestLocationButton(text string, button string, options ...KeyboardOption)
- func (m *Message) SetReplyChannel(ch chan *model.Message)
- func (m *Message) Text() string
- type MessageOption
- type MessageVars
- type Middleware
- type Mux
- type Node
- type RouterMux
- func (rm *RouterMux) DefaultHandler() *Handler
- func (rm *RouterMux) FileHandler() *Handler
- func (rm *RouterMux) HandleDefault(handler HandlerFunction, description ...string)
- func (rm *RouterMux) HandleFile(handler HandlerFunction, description ...string)
- func (rm *RouterMux) HandleFunc(path string, handler HandlerFunction, description ...string)
- func (rm *RouterMux) Handlers() Handlers
- func (rm *RouterMux) Mux(msg *Message) (*Handler, MessageVars)
- func (rm *RouterMux) Reset(chatID int64)
- func (rm *RouterMux) SetAlias(route string, aliases ...string)
- type Server
- func (s *Server) AddMiddleware(mid Middleware)
- func (s *Server) Handle(path string, reply string, description ...string)
- func (s *Server) HandleDefault(handler HandlerFunction, description ...string)
- func (s *Server) HandleFile(handler HandlerFunction, description ...string)
- func (s *Server) HandleFunc(path string, handler HandlerFunction, description ...string)
- func (s *Server) HelpHandler(m *Message)
- func (s *Server) ListenAndServe() error
- func (s *Server) Reset(chatID int64)
- func (s *Server) Send(chatID int64, text string) error
- func (s *Server) SendMessage(m *model.Message) error
- func (s *Server) SendRaw(endpoint string, params map[string]string) error
- func (s *Server) SetAlias(route string, aliases ...string)
- type ServerOption
- type SessionStorage
Constants ¶
const ( RouteBack = "<..>" RouteRoot = "" RouteRefresh = "<.>" )
Variables ¶
var DisablePreview = func(msg *model.Message) { msg.DisablePreview = true }
DisablePreview option disables web page preview when sending links.
var OneTimeKeyboard = func(msg *model.Message) { msg.OneTimeKeyboard = true }
OneTimeKeyboard option sends keyboard that hides after the user use it once.
var WithDataInlineButtons = func(msg *model.Message) { msg.WithDataInlineButtons = true }
WithDataInlineButtons option send inline keyboard buttons with data for catch a callback.
var WithMarkdown = func(msg *model.Message) { msg.Markdown = true }
WithMarkdown option enables Markdown style formatting for text messages.
var WithURLInlineButtons = func(msg *model.Message) { msg.WithURLInlineButtons = true }
WithURLInlineButtons option send inline keyboard buttons as url.
Functions ¶
This section is empty.
Types ¶
type DefaultMux ¶
type DefaultMux struct {
// contains filtered or unexported fields
}
DefaultMux is a default multiplexer, supports parametrized commands. Parameters should be enclosed with curly brackets, like in "/say {hi}" - "hi" is a parameter.
func (*DefaultMux) DefaultHandler ¶
func (dm *DefaultMux) DefaultHandler() *Handler
DefaultHandler returns default handler, nil if it's not set
func (*DefaultMux) FileHandler ¶
func (dm *DefaultMux) FileHandler() *Handler
FileHandler returns file handler, nil if there is no file handler
func (*DefaultMux) HandleDefault ¶
func (dm *DefaultMux) HandleDefault(handler HandlerFunction, description ...string)
HandleDefault adds new default handler, when nothing matches with message, "description" is for "/help" handler.
func (*DefaultMux) HandleFile ¶
func (dm *DefaultMux) HandleFile(handler HandlerFunction, description ...string)
HandleFile adds file handler. When the user uploads Document, Download method will be available for *Message.
func (*DefaultMux) HandleFunc ¶
func (dm *DefaultMux) HandleFunc(path string, handler HandlerFunction, description ...string)
HandleFunc adds new handler function to mux, "description" is for "/help" handler.
func (*DefaultMux) Handlers ¶
func (dm *DefaultMux) Handlers() Handlers
Handlers returns list of handlers currently presented in mux
func (*DefaultMux) Mux ¶
func (dm *DefaultMux) Mux(msg *Message) (*Handler, MessageVars)
Mux takes message content and returns corresponding handler and parsed vars from message
func (*DefaultMux) Reset ¶ added in v0.4.1
func (db *DefaultMux) Reset(int64)
func (*DefaultMux) SetAlias ¶ added in v0.4.1
func (dm *DefaultMux) SetAlias(route string, aliases ...string)
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a struct that represents any message handler with handler function, description, pattern and parsed variables
type HandlerFunction ¶
type HandlerFunction func(*Message)
HandlerFunction is a function that can process incoming messages
type InMemoryStorage ¶ added in v0.4.1
func (*InMemoryStorage) Get ¶ added in v0.4.1
func (ims *InMemoryStorage) Get(id int64) string
func (*InMemoryStorage) Reset ¶ added in v0.4.1
func (ims *InMemoryStorage) Reset(id int64)
func (*InMemoryStorage) Set ¶ added in v0.4.1
func (ims *InMemoryStorage) Set(id int64, path string)
type InlineKeyboardButtonsOption ¶ added in v0.4.1
InlineKeyboardButtonsOption is a functional option for inline keyboard buttons
type KeyboardOption ¶
KeyboardOption is a functional option for custom keyboards
type Message ¶
type Message struct { *model.Message Vars MessageVars // contains filtered or unexported fields }
Message is a received message from chat, with parsed variables
func (*Message) Reply ¶
func (m *Message) Reply(reply string, options ...MessageOption)
Reply to the user with plain text
func (*Message) ReplyAudio ¶
ReplyAudio sends audio file to chat
func (*Message) ReplyDocument ¶
ReplyDocument sends generic file (not audio, voice, image) to the chat
func (*Message) ReplyInlineKeyboard ¶ added in v0.4.1
func (m *Message) ReplyInlineKeyboard(text string, inlineButtons []map[string]string, options ...InlineKeyboardButtonsOption)
ReplyInlineKeyboard sends custom inline reply keyboard waiting for data callback to the user.
func (*Message) ReplyKeyboard ¶
func (m *Message) ReplyKeyboard(text string, buttons [][]string, options ...KeyboardOption)
ReplyKeyboard sends custom reply keyboard to the user.
func (*Message) ReplyLocation ¶ added in v0.4.1
ReplyLocation sends location reply to the user.
func (*Message) ReplyPhoto ¶
ReplyPhoto sends photo to the chat. Has optional caption.
func (*Message) ReplySticker ¶
ReplySticker sends sticker to the chat.
func (*Message) ReplyVideo ¶ added in v0.4.1
ReplyVideo sends video to the chat. Has optional caption.
func (*Message) Replyf ¶
Replyf is a formatted reply to the user with plain text, with parameters like in fmt.Printf
func (*Message) RequestContactButton ¶ added in v0.4.1
func (m *Message) RequestContactButton(text string, button string, options ...KeyboardOption)
RequestContactButton sends custom reply contact button to the user.
func (*Message) RequestLocationButton ¶ added in v0.4.1
func (m *Message) RequestLocationButton(text string, button string, options ...KeyboardOption)
RequestLocationButton sends custom reply location keyboard to the user.
func (*Message) SetReplyChannel ¶ added in v0.4.1
SetReplyChannel sets channel for custom reply handling, e. g. for tests
type MessageOption ¶ added in v0.4.1
MessageOption is a functional option for text messages
type MessageVars ¶
MessageVars is a parsed message variables lookup table
type Middleware ¶
type Middleware func(HandlerFunction) HandlerFunction
Middleware function takes HandlerFunction and returns HandlerFunction. Should call it's argument function inside, if needed.
func NewAuth ¶
func NewAuth(whitelist interface{}) Middleware
NewAuth creates Middleware for white-list based authentication according to username, userid or chatid list. purpose: to prevent the access to bots from another users or groups
type Mux ¶
type Mux interface { Mux(*Message) (*Handler, MessageVars) HandleFunc(string, HandlerFunction, ...string) HandleFile(HandlerFunction, ...string) HandleDefault(HandlerFunction, ...string) SetAlias(string, ...string) Reset(int64) Handlers() Handlers DefaultHandler() *Handler FileHandler() *Handler }
Mux interface represents message multiplexer
func NewRouterMux ¶ added in v0.4.1
func NewRouterMux(storage SessionStorage) Mux
NewRouterMux creates new RouterMux Takes SessionStorage to store users' sessions state
type RouterMux ¶ added in v0.4.1
type RouterMux struct {
// contains filtered or unexported fields
}
RouterMux is a tree-route multiplexer
func (*RouterMux) DefaultHandler ¶ added in v0.4.1
DefaultHandler returns default handler, nil if it's not set
func (*RouterMux) FileHandler ¶ added in v0.4.1
func (*RouterMux) HandleDefault ¶ added in v0.4.1
func (rm *RouterMux) HandleDefault(handler HandlerFunction, description ...string)
HandleDefault adds new default handler, when nothing matches with message,
func (*RouterMux) HandleFile ¶ added in v0.4.1
func (rm *RouterMux) HandleFile(handler HandlerFunction, description ...string)
func (*RouterMux) HandleFunc ¶ added in v0.4.1
func (rm *RouterMux) HandleFunc(path string, handler HandlerFunction, description ...string)
HandleFunc adds new handler function to mux.
func (*RouterMux) Handlers ¶ added in v0.4.1
Handlers returns list of handlers currently presented in mux
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a telegram bot server. Looks and feels like net/http.
func NewServer ¶
func NewServer(token string, options ...ServerOption) (*Server, error)
NewServer creates new Server with Telegram API Token and default /help handler using go default http client
func (*Server) AddMiddleware ¶
func (s *Server) AddMiddleware(mid Middleware)
AddMiddleware adds new Middleware for server
func (*Server) Handle ¶
Handle is a shortcut for HandleFunc to reply just with static text, "description" is for "/help" handler.
func (*Server) HandleDefault ¶
func (s *Server) HandleDefault(handler HandlerFunction, description ...string)
HandleDefault delegates HandleDefault to the current Mux
func (*Server) HandleFile ¶
func (s *Server) HandleFile(handler HandlerFunction, description ...string)
HandleFile adds file handler for user uploads.
func (*Server) HandleFunc ¶
func (s *Server) HandleFunc(path string, handler HandlerFunction, description ...string)
HandleFunc delegates HandleFunc to the current Mux
func (*Server) HelpHandler ¶
HelpHandler is a default handler for /help, shows available commands and their description
func (*Server) ListenAndServe ¶
ListenAndServe starts Server, returns error on failure
func (*Server) SendMessage ¶ added in v0.4.1
SendMessage method sends a Message object to the user. MessageType and ChatID are required for sending a proper message to a chat.
type ServerOption ¶
type ServerOption func(*Server)
ServerOption is a functional option for Server
func WithHttpClient ¶ added in v0.4.1
func WithHttpClient(client *http.Client) ServerOption
WithHttpClient sets custom http client for server.
func WithMux ¶
func WithMux(m Mux) ServerOption
WithMux sets custom mux for server. Should satisfy Mux interface.
func WithWebhook ¶
func WithWebhook(url string, addr string) ServerOption
WithWebhook returns ServerOption for given Webhook URL and Server address to listen. e.g. WithWebook("https://bot.example.com/super/url", "0.0.0.0:8080")
type SessionStorage ¶ added in v0.4.1
func NewSessionStorage ¶ added in v0.4.1
func NewSessionStorage() SessionStorage