bot

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package bot runs the companion Telegram bot. It talks only to the owner, renders plugin settings as button panels and hosts plugin pages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Disabled

func Disabled() bool

Disabled reports whether the env asks to skip the bot (tests, CI).

func Login

func Login(ctx context.Context, client *telegram.Client, token string) (*tg.User, error)

Login authorizes client as the bot behind token. A session that belongs to another bot is logged out first.

func NewClient

func NewClient(opt Options, h telegram.UpdateHandler) *telegram.Client

NewClient builds the gotd client for the bot account.

func SelfErrorHook added in v0.1.3

func SelfErrorHook(log plugin.Logger) func(context.Context, error) error

SelfErrorHook returns a telegram.Options.OnSelfError handler, shared by the userbot and bot clients. gotd calls Self() after every (re)connect to subscribe the fresh connection for updates, then swallows the error unless the handler acts: one transient failure would leave the client connected (pings flow, presence looks online) while receiving nothing at all.

Returning the error here makes gotd drop the connection and reconnect with backoff, retrying the subscription. Authorization errors are passed through: before login they are routine, and gotd already treats them as permanent connection errors.

func TokenBotID

func TokenBotID(token string) int64

TokenBotID is the numeric bot id at the start of a token, 0 if malformed.

Types

type Catalog

type Catalog interface {
	Builtins() []PluginInfo
	// Externals lists loaded external plugins and files that failed.
	Externals() []PluginInfo
	Repo(ctx context.Context, fresh bool) ([]RepoEntry, error)
	Install(ctx context.Context, name string) error
	// InstallFile installs an external plugin from a local .so path.
	InstallFile(ctx context.Context, path string) error
	Remove(ctx context.Context, name string) error
	// Reload reloads a loaded plugin or retries a failed one.
	Reload(ctx context.Context, name string) error
	// Explain turns an action error into a reason (zh, en).
	Explain(err error) (string, string)
}

Catalog is what the bot needs to list and manage plugins.

type Options

type Options struct {
	APIID       int
	APIHash     string
	Token       string
	SessionFile string
	PeersFile   string
	Device      telegram.DeviceConfig
	// Version is shown in the menu header.
	Version string
}

Options configures the bot.

type PluginInfo

type PluginInfo struct {
	Name     string
	Desc     string
	DescEN   string
	Version  string
	Author   string
	Commands []string // with prefix, aliases included
	// Failed is the load error of an external plugin file that did not
	// load; such a plugin has no commands, settings or page.
	Failed string
}

PluginInfo describes one plugin for the panels.

type RepoEntry

type RepoEntry struct {
	Name      string
	Desc      string
	DescEN    string
	Version   string
	Installed bool
}

RepoEntry is one plugin in the external repository.

type Service

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

Service is the companion bot.

func New

func New(opt Options, reg *settings.Registry, i18nMgr *i18n.Manager) *Service

New builds the service. Without a token Run returns at once and the bot stays unavailable, but settings still work for plugins.

func (*Service) Enabled

func (s *Service) Enabled() bool

Enabled reports whether a token is configured.

func (*Service) For

func (s *Service) For(name string) plugin.Bot

For returns the bot as seen by one plugin.

func (*Service) KnowsOwner

func (s *Service) KnowsOwner() bool

KnowsOwner reports whether the bot can already message the owner. Bots cannot open a chat; the owner has to message it once.

func (*Service) Ready

func (s *Service) Ready() bool

Ready reports whether the bot is logged in.

func (*Service) RemovePlugin

func (s *Service) RemovePlugin(name string)

RemovePlugin drops a plugin's page and settings (on unload).

func (*Service) Run

func (s *Service) Run(ctx context.Context) error

Run connects the bot and blocks until ctx ends. Connection problems are logged, never fatal: the userbot keeps working without its bot.

func (*Service) SetCatalog

func (s *Service) SetCatalog(c Catalog)

SetCatalog wires plugin listing and management.

func (*Service) SetOwner

func (s *Service) SetOwner(id int64)

SetOwner sets the only user the bot answers.

func (*Service) Settings

func (s *Service) Settings(spec *plugin.SettingsSpec) (plugin.Settings, error)

Settings registers a plugin's settings panel.

func (*Service) Username

func (s *Service) Username() string

Username is the bot's username, "" before login.

func (*Service) WaitReady

func (s *Service) WaitReady(ctx context.Context) bool

WaitReady blocks until the bot is online or ctx ends.

type View

type View = plugin.View

View aliases plugin.View inside this package.

Jump to

Keyboard shortcuts

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