Skip to main content

gotd v0.166.0: a bootstrap for user accounts

ยท 2 min read
Aleksandr Razumov
TeamLead @ VK

gotd v0.166.0 adds telegram/user, the user-account counterpart of telegram/bot: one call connects, signs in only when the stored session isn't authorized, and starts the updates and peer managers with their state on disk. It also updates the API docs and error constants from the Telegram documentation.

telegram/userโ€‹

A long-running user client (a userbot, an archiver, a service on a user account) needs the same wiring every time: a persistent session, a sign in that never runs twice, update state and access hashes on disk so nothing is missed across restarts, and a peer cache. telegram/user does it in one call:

d := tg.NewUpdateDispatcher()
u, err := user.Start(ctx, appID, appHash, user.Options{
Dir: "state/me",
Handler: d,
Auth: user.Phone(authenticator, auth.SendCodeOptions{}),
}, telegram.WithLifetime(ctx))
if err != nil {
return err
}
defer func() { _ = u.Close() }()

return u.Wait(context.Background())
  • An authorized session in Dir is used as is. A bot session, or another account than Options.UserID, fails with ErrSessionMismatch instead of running as it.
  • An unauthorized session is signed in with user.Phone (code and 2FA), user.QR (login token, imported on another DC if needed) or user.AuthFunc. Without Auth, Start fails with ErrNotAuthorized and sends nothing.
  • FLOOD_WAIT on sign in is waited out only where retrying can't send a second login code: before the code was requested, for QR exports, never for AuthFunc.
  • Updates sent while the client was stopped are delivered after the restart, and the access hashes of channels and users are kept, so known channels need no extra getDifference.

See User authentication and the userbot-echo example. telegram/bot now shares the same internals; its API and behaviour are unchanged.

API docs and errorsโ€‹

The generated docs and error constants come from getdoc v0.54.0. 42 error constants are new (e.g. ErrURLExpired, ErrTopicNotModified, ErrBotCreateLimitExceeded). Eight that Telegram no longer documents are removed:

ErrAnonymousReactionsDisabled, ErrEncryptionDeclineAdminFailed, ErrGroupcallAddParticipantsFailed, ErrNeedDocInvalid, ErrParticipantCallFailed, ErrPasskeyAuthRestart, ErrSavedPeerInvalid, ErrVolumeMoveInvalid.

Upgradingโ€‹

If your code uses one of the removed constants, compare with the string instead, e.g. tgerr.Is(err, "SAVED_PEER_INVALID"). Nothing else changes for existing code.

Full list of changes: gotd/td v0.166.0.