|
| 1 | +# Examples |
| 2 | + |
| 3 | +Runnable bots showing the library in practice. Each is its own `main` package: |
| 4 | + |
| 5 | +```bash |
| 6 | +APP_ID=12345 APP_HASH=abcdef BOT_TOKEN=123:abc go run ./examples/echo |
| 7 | +``` |
| 8 | + |
| 9 | +Bots need an MTProto app identity (`APP_ID`/`APP_HASH`, from |
| 10 | +<https://my.telegram.org>) plus a BotFather token (`BOT_TOKEN`). |
| 11 | + |
| 12 | +## Logging |
| 13 | + |
| 14 | +The bots log structured **JSONL** via zap. The shared |
| 15 | +[`examples.NewLogger`](./logger.go) uses `zap.NewProductionConfig` but lowers the |
| 16 | +level to **Debug**, so MTProto RPC traces and the business peer diagnostics show |
| 17 | +up — handy when verifying behavior against the live API. |
| 18 | + |
| 19 | +Raw JSON is hard to read in a terminal. Pipe it through |
| 20 | +[`github.com/go-faster/pl`](https://github.com/go-faster/pl), which tails and |
| 21 | +pretty-prints exactly this `zap.NewProductionConfig` JSONL. |
| 22 | + |
| 23 | +### Install pl |
| 24 | + |
| 25 | +```bash |
| 26 | +go install github.com/go-faster/pl/cmd/pl@latest |
| 27 | +``` |
| 28 | + |
| 29 | +### Use it |
| 30 | + |
| 31 | +zap writes to **stderr**, so redirect it into `pl` with `2>&1`: |
| 32 | + |
| 33 | +```bash |
| 34 | +APP_ID=12345 APP_HASH=abcdef BOT_TOKEN=123:abc go run ./examples/business 2>&1 | pl |
| 35 | +``` |
| 36 | + |
| 37 | +Useful flags: |
| 38 | + |
| 39 | +- `--level info` — hide debug lines (keep info and above) |
| 40 | +- `--no-time` — drop timestamps |
| 41 | +- `--no-color` — disable colors (or set `NO_COLOR`) |
| 42 | +- `-f service.log` — follow a file like `tail -f` |
| 43 | + |
| 44 | +Non-JSON lines pass through untouched, so mixed output (e.g. a panic stack |
| 45 | +trace) stays readable. |
| 46 | + |
| 47 | +To capture a session and read it back later: |
| 48 | + |
| 49 | +```bash |
| 50 | +go run ./examples/business 2>session.log |
| 51 | +pl session.log # read once |
| 52 | +pl -f session.log # follow while the bot runs |
| 53 | +``` |
0 commit comments