Documentation ¶
Index ¶
- Variables
- type Button
- type ButtonMode
- type CallbackHandlerRes
- type ChatMember
- type Command
- type CommandHandlerRes
- type Description
- type ErrorHandlerRes
- type File
- type FileSend
- type FileSendStream
- type FileType
- type HandlerSource
- type InitHandlerRes
- type MessageHandlerRes
- type MessageSent
- type ParseMode
- type PrimeHandlerRes
- type SendMessageData
- type Session
- func (s *Session) ChatIDGet() int64
- func (s *Session) SlotDel(slot string) error
- func (s *Session) SlotGet(slot string, data interface{}) (bool, error)
- func (s *Session) SlotSave(slot string, data interface{}) error
- func (s *Session) StateGet() (SessionState, bool, error)
- func (s *Session) UpdateChain() *UpdateChain
- func (s *Session) UserFirstNameGet() string
- func (s *Session) UserIDGet() int64
- func (s *Session) UserLastNameGet() string
- func (s *Session) UserNameGet() string
- type SessionState
- type Settings
- type SettingsBot
- type SettingsBotProxy
- type SettingsBotWebhook
- type State
- type StateHandlerRes
- type Telegram
- func (t *Telegram) ChatMemberGet(chatID, userID int64) (ChatMember, error)
- func (t *Telegram) DownloadFile(file File, dstPath string) error
- func (t *Telegram) DownloadFileStream(file File) (io.ReadCloser, error)
- func (t *Telegram) GetUpdates(ctx context.Context) error
- func (t *Telegram) Processing() error
- func (t *Telegram) SelfIDGet() int64
- func (t *Telegram) SendMessage(chatID int64, messageID int, msgData SendMessageData) ([]MessageSent, error)
- func (t *Telegram) UpdateAbsorb(update Update) error
- func (t *Telegram) UploadFile(chatID int64, file FileSend) (MessageSent, error)
- func (t *Telegram) UploadFileStream(chatID int64, file FileSendStream, r io.Reader) (MessageSent, error)
- func (t *Telegram) UsrCtxGet() interface{}
- type Update
- type UpdateChain
- func (uc *UpdateChain) CallbackQueryIDGet() string
- func (uc *UpdateChain) FilesGet(t Telegram) ([]File, error)
- func (uc *UpdateChain) Get() []Update
- func (uc *UpdateChain) MessageTextGet() []string
- func (uc *UpdateChain) MessagesIDGet() int
- func (uc *UpdateChain) MessagesIDsGet() []int
- func (uc *UpdateChain) TypeGet() UpdateType
- type UpdateType
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCallbackDataFormat contains error "wrong callback data format" ErrCallbackDataFormat = errors.New("wrong callback data format") // ErrDescriptionState contains error "session state not defined in bot description" ErrDescriptionStateMissing = errors.New("session state not defined in bot description") // ErrUpdatesChanClosed contains error "updates channel has been closed" ErrUpdatesChanClosed = errors.New("updates channel has been closed") // ErrUpdateChainZeroLen contains error "update has zero len" ErrUpdateChainZeroLen = errors.New("update has zero len") // ErrUpdateWrongType contains error "update has wrong type" ErrUpdateWrongType = errors.New("update has wrong type") // ErrSessionNotExist contains error "session does not exist" ErrSessionNotExist = errors.New("session does not exist") )
Functions ¶
This section is empty.
Types ¶
type Button ¶
type Button struct { // Button text Text string // Defines a button identifier for processing in handler Identifier string // Defines a button mode for processing in handler ("data" (default), "url", "switch") Mode ButtonMode }
Button contains buttons data for state
type ButtonMode ¶
type ButtonMode int
ButtonMode it's a type of button mode (see https://core.telegram.org/bots/api#inlinekeyboardbutton for details)
const ( ButtonModeData ButtonMode = iota ButtonModeURL ButtonModeSwitch )
func (ButtonMode) String ¶
func (b ButtonMode) String() string
type CallbackHandlerRes ¶
type CallbackHandlerRes struct { // NextState contains next session state NextState SessionState }
CallbackHandlerRes contains data returned by the CallbackHandler
type ChatMember ¶
type ChatMember tgbotapi.ChatMember
ChatMember it's an alias for tgbotapi.ChatMember
type Command ¶
type Command struct { // Command able to execute by user (without leading // '/' character) Command string // Command description that users will see in Telegram Description string // Handler to processing command received from user Handler func(t *Telegram, s *Session, cmd string, args string) (CommandHandlerRes, error) }
Command contains data for command
type CommandHandlerRes ¶
type CommandHandlerRes struct { // NextState contains next session state NextState SessionState }
CommandHandlerRes contains data returned by the CommandHandler
type Description ¶
type Description struct { // Commands contains Telegram commands available for bot. // Each record it's a command name (without leading // '/' character), its description and handler Commands []Command // States contains the states description. // Map key it's a state name that must be set with the // tg.SessState() function States map[SessionState]State // InitHandler is a handler to processing Telegram updates // when session has not been started yet. // This element returns only next state. InitHandler func(t *Telegram, s *Session) (InitHandlerRes, error) // ErrorHandler is a handler called if any other handlers returned an error ErrorHandler func(t *Telegram, s *Session, e error) (ErrorHandlerRes, error) // PrimeHandler is a handler called before any user action handlers, i.e. // CommandHandler, InitHandler, MessageHandler, CallbackHandler. // If PrimeHandler returns an error, ErrorHandler will be called. // If PrimeHandler returns a `sessionContinue` as a new session state, following handlers // will be called. Otherwise session will be switched to specified state. PrimeHandler func(t *Telegram, s *Session, hs HandlerSource) (PrimeHandlerRes, error) // DestroyHandler is a handler called before session will be destroyed DestroyHandler func(t *Telegram, s *Session) error }
Description describes bot
type ErrorHandlerRes ¶
type ErrorHandlerRes struct { // New state to switch the session. // All values of NextState must exist in States map // within the bot description NextState SessionState }
ErrorHandlerRes contains data returned by the ErrorHandler
type FileSend ¶
type FileSend struct { FileType FileType FilePath string Caption string ParseMode ParseMode Buttons [][]Button }
FileSend contains options for sending file to Telegram
type FileSendStream ¶
type FileSendStream struct { FileType FileType FileName string FileSize int64 Caption string ParseMode ParseMode Buttons [][]Button }
FileSendStream contains options for sending file to Telegram as stream
type HandlerSource ¶
type HandlerSource string
HandlerSource is a type of source handler where PrimeHandler was called
const ( HandlerSourceInit HandlerSource = "init" HandlerSourceCommand HandlerSource = "command" HandlerSourceMessage HandlerSource = "message" HandlerSourceCallback HandlerSource = "callback" )
func (HandlerSource) String ¶
func (hs HandlerSource) String() string
type InitHandlerRes ¶
type InitHandlerRes struct { // New state to switch the session. // All values of NextState must exist in States map // within the bot description NextState SessionState }
InitHandlerRes contains data returned by the InitHandler
type MessageHandlerRes ¶
type MessageHandlerRes struct { // NextState contains next session state NextState SessionState }
MessageHandlerRes contains data returned by the MessageHandler
type PrimeHandlerRes ¶
type PrimeHandlerRes struct { // New state to switch the session. // All values of NextState must exist in States map // within the bot description. NextState SessionState }
PrimeHandlerRes contains data returned by the PrimeHandler
type SendMessageData ¶
type SendMessageData struct { // Message defines a message text will sent to user Message string // ParseMode defines a Telegram message Parse mode ParseMode ParseMode // DisableWebPagePreview defines whether or not // disabling web page preview in messages DisableWebPagePreview bool // Button defines buttons for message Buttons [][]Button // `ButtonState` set a state from bot description // with callback handler for spcified buttons ButtonState SessionState }
SendMessageData contains an options for message
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
session it is a session context structure
func (*Session) StateGet ¶
func (s *Session) StateGet() (SessionState, bool, error)
stateGet gets current session state
func (*Session) UpdateChain ¶
func (s *Session) UpdateChain() *UpdateChain
UpdateChain gets update chain from session
func (*Session) UserFirstNameGet ¶
UserFirstNameGet gets current session user first name
func (*Session) UserLastNameGet ¶
UserLastNameGet gets current session user last name
func (*Session) UserNameGet ¶
UserNameGet gets current session user name
type SessionState ¶
type SessionState struct {
// contains filtered or unexported fields
}
func SessState ¶
func SessState(stateName string) SessionState
SessState creates a specified session state
func SessStateBreak ¶
func SessStateBreak() SessionState
SessStateBreak creates a `break` session state
func SessStateContinue ¶
func SessStateContinue() SessionState
SessStateBreak creates a `continue` session state
func SessStateDestroy ¶
func SessStateDestroy() SessionState
SessStateBreak creates a `destroy` session state
func (SessionState) String ¶
func (s SessionState) String() string
type Settings ¶
type Settings struct { BotSettings SettingsBot RedisHost string UpdateQueueWait time.Duration }
Settings contains data to setting up bot
type SettingsBot ¶
type SettingsBot struct { BotAPI string Webhook *SettingsBotWebhook Proxy *SettingsBotProxy }
SettingsBot contains settings for Telegram bot
type SettingsBotProxy ¶
SettingsBotProxy contains proxy settings for Telegram bot
type SettingsBotWebhook ¶
SettingsBotWebhook contains settings to set Telegram webhook
type State ¶
type State struct { // Handler to processing new bot state. StateHandler func(t *Telegram, s *Session) (StateHandlerRes, error) // Handler to processing messages received from user MessageHandler func(t *Telegram, s *Session) (MessageHandlerRes, error) // Handler to processing callbacks received from user for specific state of session CallbackHandler func(t *Telegram, s *Session, identifier string) (CallbackHandlerRes, error) // Handler to processing sent message to telegram. // E.g. useful for get sent messages ID SentHandler func(t *Telegram, s *Session, messages []MessageSent) error }
State contains session state description
type StateHandlerRes ¶
type StateHandlerRes struct { // Message contains message text to be sent to user. // Message can not be zero length Message string // ParseMode defines a Telegram message Parse mode ParseMode ParseMode // DisableWebPagePreview defines whether or not // disabling web page preview in messages DisableWebPagePreview bool // Buttons contains buttons for message to be sent to user. // If Buttons has zero length message will not contains buttons Buttons [][]Button // NextState defines next state for current session. // NextState will be ignored if MessageHandler defined for state NextState SessionState // Whether or not stick message. If true appropriate message will // be updated when a new state initiate by the `update` of callback type StickMessage bool }
StateHandlerRes contains data returned by the StateHandler
type Telegram ¶
type Telegram struct {
// contains filtered or unexported fields
}
Telegram it is a module context structure
func Init ¶
func Init(s Settings, description Description, usrCtx interface{}) (Telegram, error)
Init initializes Telegram bot
func (*Telegram) ChatMemberGet ¶
func (t *Telegram) ChatMemberGet(chatID, userID int64) (ChatMember, error)
func (*Telegram) DownloadFile ¶
DownloadFile downloads file from Telegram to specified path
func (*Telegram) DownloadFileStream ¶
func (t *Telegram) DownloadFileStream(file File) (io.ReadCloser, error)
DownloadFileStream returns io.ReadCloser to download specified file
func (*Telegram) GetUpdates ¶
GetUpdates creates to Telegram API and processes a receiving updates
func (*Telegram) Processing ¶
Processing processes available updates from queue
func (*Telegram) SendMessage ¶
func (t *Telegram) SendMessage(chatID int64, messageID int, msgData SendMessageData) ([]MessageSent, error)
sendMessage sends specified message to client Messages can be of two types: either new message, or edit existing message (if messageID is set).
func (*Telegram) UpdateAbsorb ¶
UpdateAbsorb absorbs specified `update` and put it into queue
func (*Telegram) UploadFile ¶
func (t *Telegram) UploadFile(chatID int64, file FileSend) (MessageSent, error)
UploadFile uploads file as to Telegram
func (*Telegram) UploadFileStream ¶
func (t *Telegram) UploadFileStream(chatID int64, file FileSendStream, r io.Reader) (MessageSent, error)
UploadFileStream uploads file to Telegram by specified reader
type UpdateChain ¶
type UpdateChain struct {
// contains filtered or unexported fields
}
UpdateChain contains chain of updates
func (*UpdateChain) CallbackQueryIDGet ¶
func (uc *UpdateChain) CallbackQueryIDGet() string
CallbackQueryIDGet gets callback ID from first update element from chain. Chain must have callback type
func (*UpdateChain) FilesGet ¶
func (uc *UpdateChain) FilesGet(t Telegram) ([]File, error)
FilesGet gets files from update chain. At the time only Photo, Document and Voice types are supported
func (*UpdateChain) MessageTextGet ¶
func (uc *UpdateChain) MessageTextGet() []string
MessageTextGet gets messages text or captions for every update from chain. Chain must have message type
func (*UpdateChain) MessagesIDGet ¶
func (uc *UpdateChain) MessagesIDGet() int
MessagesIDGet gets update id from first update element from chain
func (*UpdateChain) MessagesIDsGet ¶
func (uc *UpdateChain) MessagesIDsGet() []int
MessagesIDsGet gets update ids from updates chain
type UpdateType ¶
type UpdateType int
UpdateType is a type of update chain
const ( // UpdateTypeNone - type `none` for update chain. // No type has not been set yet or chain has been cleaned up. UpdateTypeNone UpdateType = iota // UpdateTypeUnknown - unknown update type UpdateTypeUnknown // UpdateTypeMessage - type message UpdateTypeMessage // UpdateTypeCallback - type callback UpdateTypeCallback )
func (UpdateType) String ¶
func (u UpdateType) String() string