waitnew

package
v0.1.3 Latest Latest
Warning

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

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

Documentation

Overview

Package waitnew finds incoming messages newer than a per-chat watermark. One Poll is one connection's worth of work; the caller loops, sleeps and releases the Telegram lock between polls.

Index

Constants

View Source
const (
	// PerChat caps messages returned per chat per poll (the newest ones).
	PerChat = 100
)

Variables

This section is empty.

Functions

func Init

func Init(ctx context.Context, t tgclient.Telegram, f Filter, since time.Time) (*State, *Result, error)

Init builds the watermark from every dialog. With since set it also returns the wanted messages newer than since.

func Poll

func Poll(ctx context.Context, t tgclient.Telegram, st *State, f Filter) (*State, *Result, error)

Poll returns the wanted incoming messages past the watermark and the advanced state. Only chats whose top message moved are fetched; st is not modified.

func Prepare

func Prepare(path string, st *State, now time.Time) (commit func() error, err error)

Prepare writes the state to a temp file next to path; commit renames it into place. Callers print their output between the two, so a crash re-delivers messages rather than losing them.

func Save

func Save(path string, st *State, now time.Time) error

Save writes the state file atomically (0600).

Types

type Filter

type Filter struct {
	Chats    map[int64]bool // empty: every chat
	DMs      bool           // 1:1 chats (people and bots)
	Groups   bool           // groups, supergroups, forums
	Mentions bool           // group messages only when they mention or reply to you
}

Filter selects chats and messages.

type Result

type Result struct {
	Messages  []tgclient.Message
	Truncated []int64 // chats with more than PerChat new messages
}

Result is one poll's (or the first run's) output.

type State

type State struct {
	Version int           `json:"version"`
	Chats   map[int64]int `json:"chats"` // chat id → highest message id seen
	// Newest is the newest top-message date seen (server clock); dialogs older
	// than it (minus slack) cannot hold new messages, so the scan stops there.
	Newest  time.Time `json:"newest,omitzero"`
	Updated time.Time `json:"updated,omitzero"`
}

State is the watermark file.

func Load

func Load(path string) (st *State, ok bool, err error)

Load reads the state file; ok is false when it does not exist.

Jump to

Keyboard shortcuts

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