background

command
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Command background demonstrates proactive, restart-surviving sends with github.com/gotd/botapi: messages that are NOT replies to an incoming update.

The trick is a serializable PeerRef. Sending to a chat needs its MTProto access hash; a PeerRef captures the chat id and that hash in a small JSON-serializable value. This bot persists one PeerRef per subscriber to a JSON file, so it can address them later — from a background goroutine, and even after a full restart — with no re-resolution and no live update to react to.

/subscribe    capture this chat's PeerRef and save it to disk
/unsubscribe  forget this chat

While running, a background ticker (driven by Bot.Background, not a per-update handler context) broadcasts the time to every saved subscriber every 30s. On startup the bot reloads the file, deserializes each PeerRef, and sends a "back online" message — proving the references survive a restart.

Run it with an MTProto app identity (https://my.telegram.org) and a BotFather token; SUBS_FILE overrides the store path (default ./subscribers.json):

APP_ID=12345 APP_HASH=abcdef BOT_TOKEN=123:abc go run ./examples/background

Jump to

Keyboard shortcuts

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