Documentation
¶
Overview ¶
Package telecast is a lightweight Telegram Bot API broadcast & notification engine.
Telecast is a library-first engine designed to be embedded in your Go application:
eng, err := telecast.New(telecast.Config{
BotToken: os.Getenv("BOT_TOKEN"),
StoreDSN: "/data/telecast.db",
})
if err != nil { log.Fatal(err) }
if err := eng.Start(ctx); err != nil { log.Fatal(err) }
defer eng.Shutdown(context.Background())
id, err := eng.Enqueue(ctx, telecast.Task{
ChatID: 12345, Text: "Hello!", Priority: telecast.PriorityNormal,
})
Index ¶
- type CampaignConfig
- type CampaignStats
- type Config
- type Engine
- func (e *Engine) AddRecipients(ctx context.Context, campaignID string, recipients []Recipient) error
- func (e *Engine) CampaignStats(ctx context.Context, id string) (*CampaignStats, error)
- func (e *Engine) CreateCampaign(ctx context.Context, c CampaignConfig) (string, error)
- func (e *Engine) DLQList(ctx context.Context, limit, offset int) ([]*model.Task, int, error)
- func (e *Engine) DLQRequeue(ctx context.Context, taskID string) error
- func (e *Engine) Enqueue(ctx context.Context, t Task) (string, error)
- func (e *Engine) Handler() http.Handler
- func (e *Engine) PauseCampaign(ctx context.Context, id string) error
- func (e *Engine) Shutdown(ctx context.Context) error
- func (e *Engine) Start(ctx context.Context) error
- func (e *Engine) StartCampaign(ctx context.Context, id string) error
- func (e *Engine) TaskStatus(ctx context.Context, taskID string) (*model.Task, error)
- type Priority
- type Recipient
- type Task
- type TaskKind
- type TemplateRenderer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CampaignConfig ¶
type CampaignConfig struct {
Name string
TemplateKey string
LocaleStrategy string // "per_user" (default) or "fixed"
FixedLocale string
Vars map[string]any
Priority Priority
}
CampaignConfig describes a broadcast campaign.
type CampaignStats ¶
type CampaignStats = model.CampaignStats
CampaignStats is a snapshot of campaign delivery progress.
type Config ¶
type Config struct {
// BotToken is the Telegram Bot API token (required).
BotToken string
// StoreDSN is the SQLite database path. Default: "telecast.db".
StoreDSN string
// TemplatesPath is the path to the YAML templates file.
// Leave empty if not using file-based templates.
TemplatesPath string
// TemplatesData is raw YAML templates data.
// If set, takes priority over TemplatesPath.
TemplatesData []byte
// TemplateRenderer is a custom template renderer.
// If set, TemplatesPath and TemplatesData are ignored.
TemplateRenderer TemplateRenderer
// TelegramBaseURL overrides the Telegram API base URL (for testing).
TelegramBaseURL string
// GlobalRPS is the global messages-per-second rate limit. Default: 25.
GlobalRPS float64
// PerChatRPS is the per-chat messages-per-second limit. Default: 1.
PerChatRPS float64
// MaxConcurrency is the worker pool size. Default: 8.
MaxConcurrency int
// LeaseTTL is how long a task is leased to a worker. Default: 30s.
LeaseTTL time.Duration
// PollInterval is how often the scheduler polls for tasks. Default: 500ms.
PollInterval time.Duration
// MaxRetries before a task is moved to DLQ. Default: 5.
MaxRetries int
// BaseBackoff is the initial retry delay. Default: 1s.
BaseBackoff time.Duration
// MaxBackoff caps the retry delay. Default: 5m.
MaxBackoff time.Duration
// Logger for structured logging. Default: slog.Default().
Logger *slog.Logger
// PrometheusRegisterer for metrics. Pass nil to disable. Default: nil (disabled).
PrometheusRegisterer prometheus.Registerer
}
Config configures the Telecast engine.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the Telecast message delivery engine. It is safe for concurrent use after Start returns.
func (*Engine) AddRecipients ¶
func (e *Engine) AddRecipients(ctx context.Context, campaignID string, recipients []Recipient) error
AddRecipients adds recipients to a campaign in batch.
func (*Engine) CampaignStats ¶
CampaignStats returns current campaign delivery progress.
func (*Engine) CreateCampaign ¶
CreateCampaign creates a new campaign. Returns the campaign ID.
func (*Engine) DLQRequeue ¶
DLQRequeue moves a dead-letter task back to the queue.
func (*Engine) Handler ¶
Handler returns an http.Handler for the optional REST API. Mount it on your own server; it is NOT required for core functionality.
func (*Engine) PauseCampaign ¶
PauseCampaign transitions a campaign to paused state.
func (*Engine) Shutdown ¶
Shutdown gracefully stops the engine and waits for in-flight work to complete, or until ctx expires.
func (*Engine) Start ¶
Start launches background goroutines (scheduler, workers, campaigns). It returns immediately. Call Shutdown to stop.
func (*Engine) StartCampaign ¶
StartCampaign transitions a campaign to running state.
type Task ¶
type Task struct {
ChatID int64
Kind TaskKind // default: KindSendMessage
Text string // raw text, mutually exclusive with TemplateKey
TemplateKey string
Locale string
Vars map[string]any
ParseMode string // "HTML", "MarkdownV2", ""
DisableWebPagePreview bool
DisableNotification bool
ReplyMarkup json.RawMessage
Priority Priority // default: PriorityNormal
IdempotencyKey string
NotBefore *time.Time
MessageID int64 // for KindEditMessage
}
Task is a message to be delivered via Telegram Bot API.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
telecast
command
Telecast — a lightweight Telegram Bot API broadcast & notification engine.
|
Telecast — a lightweight Telegram Bot API broadcast & notification engine. |
|
example
|
|
|
basic
command
Example basic demonstrates the simplest Telecast usage: create an engine, enqueue a message, wait for delivery.
|
Example basic demonstrates the simplest Telecast usage: create an engine, enqueue a message, wait for delivery. |
|
campaign
command
Example campaign demonstrates creating a broadcast campaign that delivers a template to multiple recipients in batches.
|
Example campaign demonstrates creating a broadcast campaign that delivers a template to multiple recipients in batches. |
|
custom_renderer
command
Example custom_renderer shows how to replace the built-in YAML template engine with your own TemplateRenderer implementation.
|
Example custom_renderer shows how to replace the built-in YAML template engine with your own TemplateRenderer implementation. |
|
internal
|
|
|
api
Package api provides the HTTP REST API for Telecast.
|
Package api provides the HTTP REST API for Telecast. |
|
config
Package config centralises application configuration.
|
Package config centralises application configuration. |
|
engine
Package engine implements the task scheduler, worker pool, and campaign processor.
|
Package engine implements the task scheduler, worker pool, and campaign processor. |
|
metrics
Package metrics exports Prometheus counters, gauges and histograms.
|
Package metrics exports Prometheus counters, gauges and histograms. |
|
storage
Package storage defines the persistence interface for Telecast.
|
Package storage defines the persistence interface for Telecast. |
|
storage/postgres
Package postgres provides a scaffold for a PostgreSQL storage driver.
|
Package postgres provides a scaffold for a PostgreSQL storage driver. |
|
storage/sqlite
Package sqlite implements storage.Store backed by SQLite with WAL mode.
|
Package sqlite implements storage.Store backed by SQLite with WAL mode. |
|
telegram
Package telegram provides a thin client for the Telegram Bot API.
|
Package telegram provides a thin client for the Telegram Bot API. |
|
testutil
Package testutil provides test helpers including a mock Telegram Bot API server.
|
Package testutil provides test helpers including a mock Telegram Bot API server. |
|
tmpl
Package tmpl provides locale-aware template rendering with YAML source.
|
Package tmpl provides locale-aware template rendering with YAML source. |