iambot

package
v1.7.3 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package iambot is the Telegram operator bot: it watches pending access requests through the IAM admin API and lets allowlisted Telegram users approve/deny them (or mint invites) without opening the admin panel.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("iambot: access request not found")

ErrNotFound marks a decision on a request that is gone (already decided by someone else, or deleted).

Functions

This section is empty.

Types

type AccessRequest

type AccessRequest struct {
	ID     string
	Email  string
	Reason string
	Status string
}

AccessRequest is the bot-facing view of a pending request.

type Bot

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

Bot wires the Telegram bot, the IAM client and the seen-state together.

func New

func New(cfg Config, iam *IAM, state *State, log *xlog.Logger) (*Bot, error)

New builds the Telegram bot with its handlers registered.

func (*Bot) Announce

func (b *Bot) Announce(ctx context.Context, req *AccessRequest) error

Announce pushes a new request card to every allowed operator.

func (*Bot) Start

func (b *Bot) Start(ctx context.Context)

Start runs the Telegram long polling until ctx is cancelled.

type Config

type Config struct {
	// TelegramToken is the bot token from @BotFather.
	TelegramToken string
	// BaseURL is the IAM service base URL (e.g. https://iam.example.com).
	BaseURL string
	// ProjectID scopes every admin call to one project.
	ProjectID string
	// AdminToken is a project-admin bearer token for the admin API.
	AdminToken string
	// Environment is the optional X-Environment scope (live/test/…).
	Environment string
	// AllowedUsers is the allowlist of Telegram user ids the bot talks to.
	AllowedUsers []int64
	// StatePath is where the seen-request state is persisted.
	StatePath string
	// PollInterval is how often pending access requests are re-listed.
	PollInterval time.Duration
	// HealthAddr is the liveness/readiness listener (":8081"); empty disables.
	HealthAddr string
	// LogFormat is "json" (default) or "text".
	LogFormat string
	// LogLevel is the xlog level (default "info").
	LogLevel string
}

Config is the bot's environment-driven configuration.

func FromEnv

func FromEnv() (Config, error)

FromEnv builds the Config from IAM_BOT_* environment variables.

func (Config) Allows

func (c Config) Allows(userID int64) bool

Allows reports whether a Telegram user id may operate the bot.

type IAM

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

IAM is the thin admin-API surface the bot needs.

func NewIAM

func NewIAM(baseURL, adminToken, projectID, env string) (*IAM, error)

NewIAM builds the admin-API client for one project.

func (*IAM) Approve

func (c *IAM) Approve(ctx context.Context, id string) error

Approve approves a pending request. IAM mints the email-bound invite and sends the decision email with the magic link as part of the same call.

func (*IAM) CreateInvite

func (c *IAM) CreateInvite(ctx context.Context, email string) (string, error)

CreateInvite mints an email-bound invitation; the raw token is returned exactly once (and IAM emails it to the invitee as well).

func (*IAM) Deny

func (c *IAM) Deny(ctx context.Context, id, reason string) error

Deny denies a pending request with an optional reason (surfaced in the denial email when set).

func (*IAM) ListPending

func (c *IAM) ListPending(ctx context.Context) ([]AccessRequest, error)

ListPending returns every pending access request, following the keyset pagination until the listing is exhausted.

type Poller

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

Poller watches the pending list and announces requests the state has not seen yet. See State for how "new" is defined (seen-set diff, not time).

func NewPoller

func NewPoller(bot *Bot, iam *IAM, state *State) *Poller

NewPoller builds the backlog watcher.

func (*Poller) HealthCheck

func (p *Poller) HealthCheck(context.Context) error

HealthCheck reports readiness: at least one pass ran, the last pass did not error, and the last success is not older than a few intervals.

func (*Poller) Run

func (p *Poller) Run(ctx context.Context, interval time.Duration) error

Run polls until ctx is cancelled. The first successful pass on a fresh state silently absorbs the existing backlog (a brand-new bot must not spam the whole pending history); everything pending stays reachable via /pending.

type State

type State struct {
	// Seen holds the ids of pending requests already pushed to the operators.
	// Ids leave the set when they leave the pending list (approved/denied), so
	// it stays the size of the current pending backlog.
	Seen map[string]struct{} `json:"seen"`

	// FirstRunDone marks that the cold-start backlog was silently absorbed.
	FirstRunDone bool `json:"first_run_done"`
}

State is the bot's durable memory of which access requests were already announced. The admin list endpoint exposes no created_at and its ordering is id-lexicographic (not temporal), so "new" is defined exactly as "pending id not in the seen set" — immune to clocks, ordering, pagination and restarts.

func LoadState

func LoadState(path string) (*State, error)

LoadState reads the state file; a missing file yields a fresh empty state (and firstRunDone=false so the caller can absorb the backlog quietly).

func (*State) Diff

func (s *State) Diff(ids []string) []string

Diff returns the ids from ids that were not seen yet, preserving order.

func (*State) MarkSeen

func (s *State) MarkSeen(ids ...string)

MarkSeen records ids as announced.

func (*State) Prune

func (s *State) Prune(live map[string]struct{})

Prune drops seen ids that are no longer pending, keeping the set bounded by the live backlog.

func (*State) Save

func (s *State) Save(path string) error

Save persists the state atomically (write to a temp file, then rename).

Jump to

Keyboard shortcuts

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