tbot: github.com/yanzay/tbot Index | Files | Directories

package tbot

import "github.com/yanzay/tbot"

Index

Package Files

auth.go default_mux.go handler.go help_handler.go message.go message_processor.go mux.go router_mux.go server.go session_storage.go

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.

type DefaultMux Uses

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 Uses

func (dm *DefaultMux) DefaultHandler() *Handler

DefaultHandler returns default handler, nil if it's not set

func (*DefaultMux) FileHandler Uses

func (dm *DefaultMux) FileHandler() *Handler

FileHandler returns file handler, nil if there is no file handler

func (*DefaultMux) HandleDefault Uses

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 Uses

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 Uses

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 Uses

func (dm *DefaultMux) Handlers() Handlers

Handlers returns list of handlers currently presented in mux

func (*DefaultMux) Mux Uses

func (dm *DefaultMux) Mux(msg *Message) (*Handler, MessageVars)

Mux takes message content and returns corresponding handler and parsed vars from message

func (*DefaultMux) Reset Uses

func (db *DefaultMux) Reset(int64)

func (*DefaultMux) SetAlias Uses

func (dm *DefaultMux) SetAlias(route string, aliases ...string)

type Handler Uses

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

func NewHandler Uses

func NewHandler(f func(*Message), path string, description ...string) *Handler

NewHandler creates new handler and returns it

type HandlerFunction Uses

type HandlerFunction func(*Message)

HandlerFunction is a function that can process incoming messages

type Handlers Uses

type Handlers map[string]*Handler

Handlers is a lookup table of handlers, key - string pattern value - Handler

type InMemoryStorage Uses

type InMemoryStorage struct {
    sync.Mutex
    // contains filtered or unexported fields
}

func (*InMemoryStorage) Get Uses

func (ims *InMemoryStorage) Get(id int64) string

func (*InMemoryStorage) Reset Uses

func (ims *InMemoryStorage) Reset(id int64)

func (*InMemoryStorage) Set Uses

func (ims *InMemoryStorage) Set(id int64, path string)

type InlineKeyboardButtonsOption Uses

type InlineKeyboardButtonsOption func(*model.Message)

InlineKeyboardButtonsOption is a functional option for inline keyboard buttons

type KeyboardOption Uses

type KeyboardOption func(*model.Message)

KeyboardOption is a functional option for custom keyboards

type Message Uses

type Message struct {
    *model.Message
    Vars MessageVars
    // contains filtered or unexported fields
}

Message is a received message from chat, with parsed variables

func (*Message) Download Uses

func (m *Message) Download(dir string) error

Download file from FileHandler

func (*Message) Reply Uses

func (m *Message) Reply(reply string, options ...MessageOption)

Reply to the user with plain text

func (*Message) ReplyAudio Uses

func (m *Message) ReplyAudio(filepath string)

ReplyAudio sends audio file to chat

func (*Message) ReplyDocument Uses

func (m *Message) ReplyDocument(filepath string)

ReplyDocument sends generic file (not audio, voice, image) to the chat

func (*Message) ReplyInlineKeyboard Uses

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 Uses

func (m *Message) ReplyKeyboard(text string, buttons [][]string, options ...KeyboardOption)

ReplyKeyboard sends custom reply keyboard to the user.

func (*Message) ReplyLocation Uses

func (m *Message) ReplyLocation(longitude, latitude float64)

ReplyLocation sends location reply to the user.

func (*Message) ReplyPhoto Uses

func (m *Message) ReplyPhoto(filepath string, caption ...string)

ReplyPhoto sends photo to the chat. Has optional caption.

func (*Message) ReplySticker Uses

func (m *Message) ReplySticker(filepath string)

ReplySticker sends sticker to the chat.

func (*Message) ReplyVideo Uses

func (m *Message) ReplyVideo(filepath string, caption ...string)

ReplyVideo sends video to the chat. Has optional caption.

func (*Message) Replyf Uses

func (m *Message) Replyf(reply string, values ...interface{})

Replyf is a formatted reply to the user with plain text, with parameters like in fmt.Printf

func (*Message) RequestContactButton Uses

func (m *Message) RequestContactButton(text string, button string, options ...KeyboardOption)

RequestContactButton sends custom reply contact button to the user.

func (*Message) RequestLocationButton Uses

func (m *Message) RequestLocationButton(text string, button string, options ...KeyboardOption)

RequestLocationButton sends custom reply location keyboard to the user.

func (*Message) SetReplyChannel Uses

func (m *Message) SetReplyChannel(ch chan *model.Message)

SetReplyChannel sets channel for custom reply handling, e. g. for tests

func (*Message) Text Uses

func (m *Message) Text() string

Text returns message text

type MessageOption Uses

type MessageOption func(*model.Message)

MessageOption is a functional option for text messages

type MessageVars Uses

type MessageVars map[string]string

MessageVars is a parsed message variables lookup table

type Middleware Uses

type Middleware func(HandlerFunction) HandlerFunction

Middleware function takes HandlerFunction and returns HandlerFunction. Should call it's argument function inside, if needed.

func NewAuth Uses

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 Uses

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 NewDefaultMux Uses

func NewDefaultMux() Mux

NewDefaultMux creates new DefaultMux

func NewRouterMux Uses

func NewRouterMux(storage SessionStorage) Mux

NewRouterMux creates new RouterMux Takes SessionStorage to store users' sessions state

type Node Uses

type Node struct {
    // contains filtered or unexported fields
}

func NewNode Uses

func NewNode(parent *Node, route string, handler *Handler) *Node

type RouterMux Uses

type RouterMux struct {
    // contains filtered or unexported fields
}

RouterMux is a tree-route multiplexer

func (*RouterMux) DefaultHandler Uses

func (rm *RouterMux) DefaultHandler() *Handler

DefaultHandler returns default handler, nil if it's not set

func (*RouterMux) FileHandler Uses

func (rm *RouterMux) FileHandler() *Handler

func (*RouterMux) HandleDefault Uses

func (rm *RouterMux) HandleDefault(handler HandlerFunction, description ...string)

HandleDefault adds new default handler, when nothing matches with message,

func (*RouterMux) HandleFile Uses

func (rm *RouterMux) HandleFile(handler HandlerFunction, description ...string)

func (*RouterMux) HandleFunc Uses

func (rm *RouterMux) HandleFunc(path string, handler HandlerFunction, description ...string)

HandleFunc adds new handler function to mux.

func (*RouterMux) Handlers Uses

func (rm *RouterMux) Handlers() Handlers

Handlers returns list of handlers currently presented in mux

func (*RouterMux) Mux Uses

func (rm *RouterMux) Mux(msg *Message) (*Handler, MessageVars)

Mux takes message content and returns corresponding handler

func (*RouterMux) Reset Uses

func (rm *RouterMux) Reset(chatID int64)

func (*RouterMux) SetAlias Uses

func (rm *RouterMux) SetAlias(route string, aliases ...string)

SetAlias sets aliases for specified route.

type Server Uses

type Server struct {
    // contains filtered or unexported fields
}

Server is a telegram bot server. Looks and feels like net/http.

func NewServer Uses

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 Uses

func (s *Server) AddMiddleware(mid Middleware)

AddMiddleware adds new Middleware for server

func (*Server) Handle Uses

func (s *Server) Handle(path string, reply string, description ...string)

Handle is a shortcut for HandleFunc to reply just with static text, "description" is for "/help" handler.

func (*Server) HandleDefault Uses

func (s *Server) HandleDefault(handler HandlerFunction, description ...string)

HandleDefault delegates HandleDefault to the current Mux

func (*Server) HandleFile Uses

func (s *Server) HandleFile(handler HandlerFunction, description ...string)

HandleFile adds file handler for user uploads.

func (*Server) HandleFunc Uses

func (s *Server) HandleFunc(path string, handler HandlerFunction, description ...string)

HandleFunc delegates HandleFunc to the current Mux

func (*Server) HelpHandler Uses

func (s *Server) HelpHandler(m *Message)

HelpHandler is a default handler for /help, shows available commands and their description

func (*Server) ListenAndServe Uses

func (s *Server) ListenAndServe() error

ListenAndServe starts Server, returns error on failure

func (*Server) Reset Uses

func (s *Server) Reset(chatID int64)

func (*Server) Send Uses

func (s *Server) Send(chatID int64, text string) error

func (*Server) SendMessage Uses

func (s *Server) SendMessage(m *model.Message) error

SendMessage method sends a Message object to the user. MessageType and ChatID are required for sending a proper message to a chat.

func (*Server) SendRaw Uses

func (s *Server) SendRaw(endpoint string, params map[string]string) error

SendRaw sends direct request to telegram api

func (*Server) SetAlias Uses

func (s *Server) SetAlias(route string, aliases ...string)

type ServerOption Uses

type ServerOption func(*Server)

ServerOption is a functional option for Server

func WithHttpClient Uses

func WithHttpClient(client *http.Client) ServerOption

WithHttpClient sets custom http client for server.

func WithMux Uses

func WithMux(m Mux) ServerOption

WithMux sets custom mux for server. Should satisfy Mux interface.

func WithWebhook Uses

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 Uses

type SessionStorage interface {
    Set(int64, string)
    Get(int64) string
    Reset(int64)
}

func NewSessionStorage Uses

func NewSessionStorage() SessionStorage

Directories

PathSynopsis
internal/adapter
model

Package tbot imports 12 packages (graph). Updated 2018-12-16. Refresh now. Tools for package owners.