Documentation
¶
Overview ¶
Package telegram is a notification channel on each person's own Telegram bot: it tells them what waits on them, lets them answer with buttons, and reports their runs.
It is built to do little, on purpose. A Telegram account is easier to lose than a laptop, and the agents it talks to may run code, so:
- The only thing it can change is the answer to a task that waits on the bot's owner, and only with buttons: approve or reject an approval, pick options of a form (lagrange/ask). It never starts, cancels or messages anything, and typed text is never sent anywhere. Deps is the whole of what it can reach.
- It listens to one private chat and one Telegram user, bound by a one-time pairing code; everything else gets no answer at all.
- Buttons carry random tokens that name rows here, never tasks. Closing the message is the claim that lets one answer through; the answer then goes through the same checks as the inbox.
- Approving asks twice.
- The bot token only ever goes to api.telegram.org, and never appears in errors, logs or the API.
Bots long-poll (getUpdates), so a server needs no public URL. One replica polls each bot, under a Postgres advisory lock.
Index ¶
- Constants
- Variables
- func Dur(d time.Duration) string
- func IsUserError(err error) bool
- func ValidToken(s string) bool
- type Answer
- type Button
- type CallbackQuery
- type Chat
- type Deps
- type Keyboard
- type Message
- type Run
- type Service
- func (s *Service) Connect(ctx context.Context, u *directory.User, token string) (*Status, error)
- func (s *Service) Disconnect(ctx context.Context, u *directory.User) error
- func (s *Service) Kick()
- func (s *Service) Notice(ctx context.Context, u *directory.User, nt notify.Notice) (bool, error)
- func (s *Service) Pair(ctx context.Context, u *directory.User) (*Status, error)
- func (s *Service) Run(ctx context.Context)
- func (s *Service) SetLevel(ctx context.Context, u *directory.User, level string) (*Status, error)
- func (s *Service) Status(ctx context.Context, u *directory.User) (*Status, error)
- func (s *Service) Test(ctx context.Context, u *directory.User) (bool, error)
- type Status
- type Update
- type User
- type Waiting
Constants ¶
const ( // LevelQuestions sends only what waits on the person. LevelQuestions = "questions" // LevelMilestones also sends report notices and finished runs. LevelMilestones = "milestones" )
Levels: what a bot sends besides questions.
const ButtonTTL = 7 * 24 * time.Hour
ButtonTTL is how long buttons work.
const PairTTL = 10 * time.Minute
PairTTL is how long a pairing code works.
Variables ¶
var ErrBadToken = errors.New("that isn't a working bot token: copy the whole token @BotFather sent (digits, a colon, then letters)")
ErrBadToken means the token isn't a bot token, or Telegram doesn't know it.
var ErrGone = errors.New("the task no longer waits for this answer")
ErrGone means the task no longer waits for this answer (answered elsewhere, canceled, or not the person's to answer).
Functions ¶
func IsUserError ¶
IsUserError reports whether err is the person's to fix (a bad token, a bot that is someone else's), not the server's.
func ValidToken ¶
ValidToken reports whether s looks like a bot token from @BotFather.
Types ¶
type Answer ¶
Answer is an answer from a button: a decision ("approve" or "reject") or a form's text, never typed text.
type Button ¶
type Button struct {
Text string `json:"text"`
Data string `json:"callback_data,omitempty"`
URL string `json:"url,omitempty"`
}
Button is an inline keyboard button.
type CallbackQuery ¶
type CallbackQuery struct {
ID string `json:"id"`
From User `json:"from"`
Message *Message `json:"message"`
Data string `json:"data"`
}
CallbackQuery is a button press.
type Deps ¶
type Deps struct {
// Answer answers a task waiting on handle, with the inbox's checks. It returns
// ErrGone when the task doesn't wait for this answer anymore.
Answer func(ctx context.Context, handle, taskID string, a Answer) error
// Waiting lists the tasks waiting on handle.
Waiting func(ctx context.Context, handle string) ([]Waiting, error)
// Runs lists handle's latest runs, newest first.
Runs func(ctx context.Context, handle string, limit int) ([]Run, error)
// Run returns the run rooted at taskID, if handle sees it.
Run func(ctx context.Context, handle, taskID string) (*Run, error)
}
Deps is everything the bot can reach beyond its own tables: one way to answer and three read-only queries, each on behalf of one person. Nothing here starts, cancels or sends a message, and nothing should be added that does.
type Message ¶
type Message struct {
MessageID int64 `json:"message_id"`
From *User `json:"from"`
Chat Chat `json:"chat"`
Text string `json:"text"`
}
Message is a Telegram message.
type Run ¶
type Run struct {
ID string
Title string
// State is the root task's state; Status the run's group (running, waiting, done, failed).
State, Status string
Waiting []string
Cost float64
Currency string
StartedAt time.Time
UpdatedAt time.Time
Outcome string
Report *report.Report
}
Run is a run, as Telegram shows it.
type Service ¶
type Service struct {
DB *pg.DB
Directory *directory.Directory
Activity *activity.Log
// PublicURL is the server's base URL, for links.
PublicURL string
Deps Deps
// HTTP calls the Bot API (default: a client with a 70s timeout).
HTTP *http.Client
Logger *slog.Logger
// PollTimeout is getUpdates' long-poll timeout (default 50s).
PollTimeout time.Duration
// Rescan is how often the bot list is read again (default 30s); Kick does it now.
Rescan time.Duration
// Tick is how often the activity log is followed (default 1s).
Tick time.Duration
// MinRun is how long a run without a report must have taken for its end to be
// worth a message (default 1m); failed runs and runs with a report always are.
MinRun time.Duration
// APIBase replaces https://api.telegram.org. Tests only: it is never set from
// configuration or input.
APIBase string
// Now is the clock (tests).
Now func() time.Time
// contains filtered or unexported fields
}
Service runs every person's bot.
func (*Service) Connect ¶
Connect checks a bot token with Telegram, connects the bot to u (replacing any other), and starts pairing.
func (*Service) Disconnect ¶
Disconnect removes u's bot. The bot itself stays theirs in Telegram.
func (*Service) Kick ¶
func (s *Service) Kick()
Kick reads the bot list again now (after a bot is connected or removed).
func (*Service) Notice ¶
Notice sends a notice about a waiting task to the person's bot (notify.Channel).
func (*Service) Pair ¶
Pair makes a new pairing code for u's bot and returns the link that sends it.
type Status ¶
type Status struct {
Connected bool `json:"connected"`
BotUsername string `json:"botUsername,omitempty"`
Paired bool `json:"paired"`
// ChatUsername is the Telegram user the bot is paired with.
ChatUsername string `json:"chatUsername,omitempty"`
// PairLink opens the bot with a fresh pairing code; only right after it is made.
PairLink string `json:"pairLink,omitempty"`
PairExpiresAt *time.Time `json:"pairExpiresAt,omitempty"`
Level string `json:"level,omitempty"`
MutedUntil *time.Time `json:"mutedUntil,omitempty"`
}
Status is a person's Telegram connection, as the API shows it. It never has the token.
type Update ¶
type Update struct {
UpdateID int64 `json:"update_id"`
Message *Message `json:"message"`
EditedMessage *Message `json:"edited_message"`
CallbackQuery *CallbackQuery `json:"callback_query"`
}
Update is one incoming update.