bf

package module
v0.0.0-...-d979894 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 10 Imported by: 0

README

golhu

BF - bot framework

Simple go tg bot framework Go to example folder for usage.

DO NOT USE NOW - too raw

License

MIT License

TODO

  • Add tests
  • Add examples
  • Add docs
  • Check todo
  • Check inspect code
  • Add CI
  • Add CI badges
  • Add coverage

Documentation

Index

Constants

View Source
const (
	EventKindText         eventType = "text"
	EventKindInlineButton eventType = "buttonInline"
	EventKindCommand      eventType = "command"
	EventKindVoice        eventType = "audio"
)

Variables

View Source
var ErrUnparsedEvent = errors.New("unparsed event")

Functions

This section is empty.

Types

type AudioHandler

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

type BotOption

type BotOption func(bot *ChatBotImpl)

func WithDebug

func WithDebug() BotOption

WithDebug enables debug mode.

func WithLogger

func WithLogger(logger Logger) BotOption

WithLogger inject your logger inside framework.

func WithParseMode

func WithParseMode(parseMode string) BotOption

WithParseMode sets parse mode for TG for all messages. default is ModeHTML Possible values:

  • ModeMarkdown
  • ModeMarkdownV2
  • ModeHTML

type ChatBot

type ChatBot interface {
	// Start main loop of the bot. Start after register all non-dynamic handlers.
	Start(ctx context.Context) error
	// SendMsg sends a message to the chat. Buttons register one-time handlers.
	SendMsg(chatID int64, layer *HandlerLayer) error
	// SendText sends a short text message to the chat. Doesn't wipe layers.
	SendText(chatID int64, text string) error

	// RegisterDefaultHandler registers a handler that will be called if no other handler is found.
	RegisterDefaultHandler(handler HandlerFunc)
	// RegisterCommand registers a handler for a command.
	RegisterCommand(command string, handler HandlerFunc)
	// RegisterIButton registers a new inline button with the given text and handler function.
	RegisterIButton(btn string, handler HandlerFunc)

	// RegisterMiddleware middlewares before any handler that matches the filter function.
	// If the filter function returns true, the middleware will be applied.
	// If filterFunc is nil, the middleware will be applied to all handlers.
	RegisterMiddleware(middleware MiddlewareFunc)

	// NewLayer creates a new Layer of handlers necessary for SendMsg.
	NewLayer(msgText ...any) *HandlerLayer

	// RetryLastLayer SendMsg with the same layer as the last one.
	RetryLastLayer(event Event, newText string) error

	// RegisterErrorHandler sets the default handler if error happens on handler.
	RegisterErrorHandler(handler ErrorHandlerFunc)
	// SelfUserName returns the username of the bot.
	SelfUserName() string

	// LoaderButton short loader button cancel by ctx.
	LoaderButton(chatID int64, loadScreen []string) context.CancelFunc

	GetFileURL(fileID string) (string, error)
}

type ChatBotImpl

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

func NewBot

func NewBot(apikey string, opts ...BotOption) (*ChatBotImpl, error)

NewBot creates new bot instance, base layer and starts cleaner. apikey - telegram bot api key generated by BotFather. opts - options for bot BotOption.

func (*ChatBotImpl) GetFileURL

func (b *ChatBotImpl) GetFileURL(fileID string) (string, error)

GetFileURL returns direct url to telegram file.

func (*ChatBotImpl) LoaderButton

func (b *ChatBotImpl) LoaderButton(chatID int64, loadScreen []string) context.CancelFunc

LoaderButton creates loader button. Loader button is a button that will be shown while some long operation is in progress by editing last message with texts from loadScreen. !!! WARNING !!! Don't forget to call cancel() when operation is finished.

func (*ChatBotImpl) NewLayer

func (b *ChatBotImpl) NewLayer(msgText ...any) *HandlerLayer

NewLayer creates new layer. Layer is a set of handlers for different types of events. msgText - text that will be sent to user when layer is activated via `SendMsg` method.

func (*ChatBotImpl) RegisterAudio

func (b *ChatBotImpl) RegisterAudio(handler HandlerFunc)

RegisterAudio registers a handler for Voice msg. Voice will be added as file to Event.

func (*ChatBotImpl) RegisterButton

func (b *ChatBotImpl) RegisterButton(btn string, handler HandlerFunc)

RegisterButton registers a new button with the given text and handler function.

func (*ChatBotImpl) RegisterCommand

func (b *ChatBotImpl) RegisterCommand(command string, handler HandlerFunc)

RegisterCommand registers a new command with the given name and handler function.

func (*ChatBotImpl) RegisterDefaultHandler

func (b *ChatBotImpl) RegisterDefaultHandler(handler HandlerFunc)

RegisterDefaultHandler registers a new default handler.

func (*ChatBotImpl) RegisterErrorHandler

func (b *ChatBotImpl) RegisterErrorHandler(handler ErrorHandlerFunc)

RegisterErrorHandler registers a new error handler.

func (*ChatBotImpl) RegisterIButton

func (b *ChatBotImpl) RegisterIButton(btn string, handler HandlerFunc)

RegisterIButton registers a new inline button with the given text and handler function. btn - text to be displayed on the button. handler - function to be called when the button is pressed.

func (*ChatBotImpl) RegisterMiddleware

func (b *ChatBotImpl) RegisterMiddleware(middleware MiddlewareFunc)

RegisterMiddleware registers a new middleware.

func (*ChatBotImpl) RetryLastLayer

func (b *ChatBotImpl) RetryLastLayer(event Event, newText string) error

RetryLastLayer sends previous layer to chat with given ID. If newText is not empty, it will be used instead of previous layer text.

func (*ChatBotImpl) SelfUserName

func (b *ChatBotImpl) SelfUserName() string

SelfUserName returns bot username.

func (*ChatBotImpl) SendMsg

func (b *ChatBotImpl) SendMsg(chatID int64, layer *HandlerLayer) error

SendMsg sends layer to chat with given ID. Text and buttons will be sent, and layer will be set as current layer for chatID.

func (*ChatBotImpl) SendText

func (b *ChatBotImpl) SendText(chatID int64, text string) error

SendText sends simple text to chat. Doesn't affect any layers.

func (*ChatBotImpl) Start

func (b *ChatBotImpl) Start(ctx context.Context) error

Start starts bot.

type CommandHandler

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

type ErrorHandlerFunc

type ErrorHandlerFunc func(context.Context, Event, error)

type Event

type Event struct {
	Kind             eventType `json:"kind"`
	Text             string    `json:"text"`
	Command          string    `json:"command"`
	Button           string    `json:"button"`
	ButtonText       string    `json:"buttonText"`
	ChatID           int64     `json:"chatID"`
	UserTGID         int64     `json:"userTGID"`
	FirstName        string    `json:"firstName"`
	LastName         string    `json:"lastName"`
	UserTgUsername   string    `json:"userTgUsername"`
	CommandArguments string    `json:"commandArguments"`
	Username         string    `json:"username"`

	Voice *tgbotapi.Voice `json:"-"`
	// contains filtered or unexported fields
}

Event is a struct that represents event from telegram.

func (*Event) FullName

func (e *Event) FullName() string

func (*Event) String

func (e *Event) String() string

type FilterFunc

type FilterFunc func(ctx context.Context, event Event) bool

type HandlerFunc

type HandlerFunc func(ctx context.Context, event Event) error

type HandlerLayer

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

HandlerLayer handlers for events from 1 chat. There is 2 most common usages: 1. Create layer and in use in SendMsg. That way your layer will be used for next 1 user message and wipe after. 2. Default layer. That layer process all messages that doesn't have any chat-specific one-time layers.

func (*HandlerLayer) AddText

func (hl *HandlerLayer) AddText(text string)

func (*HandlerLayer) Handler

func (hl *HandlerLayer) Handler(event Event) HandlerFunc

Handler get a HandlerFunc for event.

func (*HandlerLayer) IsEmpty

func (hl *HandlerLayer) IsEmpty() bool

func (*HandlerLayer) IsExpired

func (hl *HandlerLayer) IsExpired() bool

func (*HandlerLayer) RegisterButton

func (hl *HandlerLayer) RegisterButton(text string, handler HandlerFunc)

func (*HandlerLayer) RegisterCommand

func (hl *HandlerLayer) RegisterCommand(command string, handler HandlerFunc)

func (*HandlerLayer) RegisterIButton

func (hl *HandlerLayer) RegisterIButton(text string, handler HandlerFunc)

func (*HandlerLayer) RegisterIButtonSwitch

func (hl *HandlerLayer) RegisterIButtonSwitch(text, link string, handler HandlerFunc)

func (*HandlerLayer) RegisterIButtonURL

func (hl *HandlerLayer) RegisterIButtonURL(text, url string)

func (*HandlerLayer) RegisterText

func (hl *HandlerLayer) RegisterText(text string, handler HandlerFunc)

func (*HandlerLayer) RegisterVoice

func (hl *HandlerLayer) RegisterVoice(handler HandlerFunc)

func (*HandlerLayer) SetIButtonRowMode

func (hl *HandlerLayer) SetIButtonRowMode()

type InlineButtonHandler

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

type Logger

type Logger interface {
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
}

type MiddlewareFunc

type MiddlewareFunc func(handlerFunc HandlerFunc) HandlerFunc

type TextHandler

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

type TextHandlerKind

type TextHandlerKind string
const (
	TextHandlerKindText   TextHandlerKind = "text"
	TextHandlerKindButton TextHandlerKind = "button"
	AnyText                               = "*"
)

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL