Documentation ¶
Index ¶
- type AuthorizationPolicy
- type ChatConfigRepository
- func (chatConfig *ChatConfigRepository) Get(chatID int) string
- func (chatConfig *ChatConfigRepository) GetWithStruct(chatID int, obj interface{})
- func (chatConfig *ChatConfigRepository) Remove(chatID int)
- func (chatConfig *ChatConfigRepository) Set(chatID int, JSON string)
- func (chatConfig *ChatConfigRepository) SetWithStruct(chatID int, obj interface{})
- type CommandHandler
- type HelpHandler
- type InlineHandler
- type Margelet
- func (margelet *Margelet) AddCommandHandler(command string, handler CommandHandler, auth ...AuthorizationPolicy)
- func (margelet *Margelet) AddMessageHandler(handler MessageHandler)
- func (margelet *Margelet) AddSessionHandler(command string, handler SessionHandler, auth ...AuthorizationPolicy)
- func (margelet *Margelet) AnswerInlineQuery(config tgbotapi.InlineConfig) (tgbotapi.APIResponse, error)
- func (margelet *Margelet) GetConfigRepository() *ChatConfigRepository
- func (margelet *Margelet) GetFileDirectURL(fileID string) (string, error)
- func (margelet *Margelet) GetRedis() *redis.Client
- func (margelet *Margelet) GetSessionRepository() SessionRepository
- func (margelet *Margelet) HandleSession(message tgbotapi.Message, command string)
- func (margelet *Margelet) IsMessageToMe(message tgbotapi.Message) bool
- func (margelet *Margelet) QuickReply(chatID, messageID int, message string) (tgbotapi.Message, error)
- func (margelet *Margelet) QuickSend(chatID int, message string) (tgbotapi.Message, error)
- func (margelet *Margelet) Run() error
- func (margelet *Margelet) Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
- func (margelet *Margelet) Stop()
- type MargeletAPI
- type MessageHandler
- type SessionHandler
- type SessionRepository
- type TGBotAPI
- type UsernameAuthorizationPolicy
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthorizationPolicy ¶
type AuthorizationPolicy interface {
Allow(message tgbotapi.Message) error
}
AuthorizationPolicy - interface, that describes authorization policy for command or session
type ChatConfigRepository ¶
type ChatConfigRepository struct {
// contains filtered or unexported fields
}
ChatConfigRepository - repository for chat configs
func (*ChatConfigRepository) Get ¶
func (chatConfig *ChatConfigRepository) Get(chatID int) string
Get - returns config for chatID
func (*ChatConfigRepository) GetWithStruct ¶
func (chatConfig *ChatConfigRepository) GetWithStruct(chatID int, obj interface{})
GetWithStruct - returns config for chatID using a struct
func (*ChatConfigRepository) Remove ¶
func (chatConfig *ChatConfigRepository) Remove(chatID int)
Remove - removes config for chatID
func (*ChatConfigRepository) Set ¶
func (chatConfig *ChatConfigRepository) Set(chatID int, JSON string)
Set - stores any config for chatID
func (*ChatConfigRepository) SetWithStruct ¶
func (chatConfig *ChatConfigRepository) SetWithStruct(chatID int, obj interface{})
SetWithStruct - stores any config for chatID using a struct
type CommandHandler ¶
type CommandHandler interface { HandleCommand(bot MargeletAPI, message tgbotapi.Message) error HelpMessage() string }
CommandHandler - interface for command handlers
type HelpHandler ¶
type HelpHandler struct {
Margelet *Margelet
}
HelpHandler Default handler for /help command. Margelet will add this automatically
func (HelpHandler) HandleCommand ¶
func (handler HelpHandler) HandleCommand(bot MargeletAPI, message tgbotapi.Message) error
HandleCommand sends default help message
func (HelpHandler) HelpMessage ¶
func (handler HelpHandler) HelpMessage() string
HelpMessage return help string for HelpHandler
type InlineHandler ¶
type InlineHandler interface {
HandleInline(bot MargeletAPI, query tgbotapi.InlineQuery) error
}
InlineHandler - interface for message handlers
type Margelet ¶
type Margelet struct { MessageHandlers []MessageHandler CommandHandlers map[string]authorizedCommandHandler SessionHandlers map[string]authorizedSessionHandler InlineHandler InlineHandler Redis *redis.Client ChatRepository *chatRepository SessionRepository SessionRepository ChatConfigRepository *ChatConfigRepository // contains filtered or unexported fields }
Margelet - main struct in package, handles all interactions
Example ¶
package main import ( "../margelet" "gopkg.in/telegram-bot-api.v2" ) type BotMock struct { Updates chan tgbotapi.Update } func (bot BotMock) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) { return tgbotapi.Message{}, nil } func (bot BotMock) AnswerInlineQuery(config tgbotapi.InlineConfig) (tgbotapi.APIResponse, error) { return tgbotapi.APIResponse{}, nil } func (bot BotMock) GetFileDirectURL(fileID string) (string, error) { return "https://example.com/test.txt", nil } func (bot BotMock) IsMessageToMe(message tgbotapi.Message) bool { return false } func (bot BotMock) GetUpdatesChan(config tgbotapi.UpdateConfig) (<-chan tgbotapi.Update, error) { return bot.Updates, nil } var ( botMock = BotMock{} ) func getMargelet() *margelet.Margelet { botMock.Updates = make(chan tgbotapi.Update, 10) m, _ := margelet.NewMargeletFromBot("test", "127.0.0.1:6379", "", 10, &botMock) m.Redis.FlushDb() return m } func main() { bot, err := margelet.NewMargelet("<your awesome bot name>", "<redis addr>", "<redis password>", 0, "your bot token", false) if err != nil { panic(err) } bot.Run() }
Output:
func NewMargelet ¶
func NewMargelet(botName string, redisAddr string, redisPassword string, redisDB int64, token string, verbose bool) (*Margelet, error)
NewMargelet creates new Margelet instance
func NewMargeletFromBot ¶
func NewMargeletFromBot(botName string, redisAddr string, redisPassword string, redisDB int64, bot TGBotAPI) (*Margelet, error)
NewMargeletFromBot creates new Margelet instance from existing TGBotAPI(tgbotapi.BotAPI)
func (*Margelet) AddCommandHandler ¶
func (margelet *Margelet) AddCommandHandler(command string, handler CommandHandler, auth ...AuthorizationPolicy)
AddCommandHandler - adds new CommandHandler to Margelet
func (*Margelet) AddMessageHandler ¶
func (margelet *Margelet) AddMessageHandler(handler MessageHandler)
AddMessageHandler - adds new MessageHandler to Margelet
func (*Margelet) AddSessionHandler ¶
func (margelet *Margelet) AddSessionHandler(command string, handler SessionHandler, auth ...AuthorizationPolicy)
AddSessionHandler - adds new SessionHandler to Margelet
func (*Margelet) AnswerInlineQuery ¶
func (margelet *Margelet) AnswerInlineQuery(config tgbotapi.InlineConfig) (tgbotapi.APIResponse, error)
AnswerInlineQuery - send answer to InlineQuery
func (*Margelet) GetConfigRepository ¶
func (margelet *Margelet) GetConfigRepository() *ChatConfigRepository
GetConfigRepository - returns chat config repository
func (*Margelet) GetFileDirectURL ¶
GetFileDirectURL - converts fileID to direct URL
func (*Margelet) GetRedis ¶
func (margelet *Margelet) GetRedis() *redis.Client
GetRedis - returns margelet's redis client
func (*Margelet) GetSessionRepository ¶
func (margelet *Margelet) GetSessionRepository() SessionRepository
GetSessionRepository - returns session repository
func (*Margelet) HandleSession ¶
HandleSession - handles any message as session message with handler
func (*Margelet) IsMessageToMe ¶
IsMessageToMe - return true if message sent to this bot
func (*Margelet) QuickReply ¶
func (margelet *Margelet) QuickReply(chatID, messageID int, message string) (tgbotapi.Message, error)
QuickReply - quick send text reply to message
type MargeletAPI ¶
type MargeletAPI interface { Send(c tgbotapi.Chattable) (tgbotapi.Message, error) AnswerInlineQuery(config tgbotapi.InlineConfig) (tgbotapi.APIResponse, error) QuickSend(chatID int, message string) (tgbotapi.Message, error) QuickReply(chatID, messageID int, message string) (tgbotapi.Message, error) GetFileDirectURL(fileID string) (string, error) IsMessageToMe(message tgbotapi.Message) bool GetConfigRepository() *ChatConfigRepository GetSessionRepository() SessionRepository GetRedis() *redis.Client HandleSession(message tgbotapi.Message, command string) }
MargeletAPI - interface, that describes margelet API
type MessageHandler ¶
type MessageHandler interface {
HandleMessage(bot MargeletAPI, message tgbotapi.Message) error
}
MessageHandler - interface for message handlers
type SessionHandler ¶
type SessionHandler interface { HandleSession(bot MargeletAPI, message tgbotapi.Message, responses []tgbotapi.Message) (bool, error) CancelSession(bot MargeletAPI, message tgbotapi.Message, responses []tgbotapi.Message) HelpMessage() string }
SessionHandler - interface for session handlers
type SessionRepository ¶
type SessionRepository interface { Create(chatID int, userID int, command string) Add(chatID int, userID int, message tgbotapi.Message) Remove(chatID int, userID int) Command(chatID int, userID int) string Dialog(chatID int, userID int) (messages []tgbotapi.Message) }
SessionRepository - public interface for session repository
type TGBotAPI ¶
type TGBotAPI interface { Send(c tgbotapi.Chattable) (tgbotapi.Message, error) AnswerInlineQuery(config tgbotapi.InlineConfig) (tgbotapi.APIResponse, error) GetFileDirectURL(fileID string) (string, error) IsMessageToMe(message tgbotapi.Message) bool GetUpdatesChan(config tgbotapi.UpdateConfig) (<-chan tgbotapi.Update, error) }
TGBotAPI - interface, that describe telegram-bot-api API
type UsernameAuthorizationPolicy ¶
type UsernameAuthorizationPolicy struct {
Usernames []string
}
UsernameAuthorizationPolicy - simple authorization policy, that checks sender's username
func (UsernameAuthorizationPolicy) Allow ¶
func (p UsernameAuthorizationPolicy) Allow(message tgbotapi.Message) error
Allow check message author's username and returns nil if it in Usernames otherwise, returns an authorization error message