ptgc
MTProto client for Dart

Core & Session

Part of the API Reference.

connect

Future<void> connect() async {

Opens the MTProto connection: reuses a saved SessionStore session if one exists, otherwise negotiates a fresh auth key. Does not log you in by itself — call auth methods afterwards if [isSignedIn] is still false.

disconnect

Future<void> disconnect() async {

Closes the socket. The session file is left in place, so a later [connect] picks up right where this left off.

isConnected

bool get isConnected => _rawClient != null;

Whether [connect] has established a connection.

isSignedIn

bool get isSignedIn => peers.selfId != null;

Whether a user is logged in on this connection (implies [isConnected]).

userId

int? get userId => peers.selfId;

The logged-in user’s ID, once known.

whoAmI

Future<PtgcUser> whoAmI() async {

Fetches your own account as a full PtgcUser — username, phone, premium status, and so on — the richer counterpart to the bare [userId] property. Throws AuthRequiredException if you’re not signed in.

events

Stream<TelegramEvent> get events => _events.stream;

A stream of chat events (new messages, participant/admin changes) — see Events & Updates for the event types. Only starts producing events after [connect].

raw

tg.Client get raw {

The underlying tg.Client. Use this (or [invoke]) for any raw API call ptgc doesn’t wrap yet — every namespace under it (.channels, .messages, .users, …) is generated straight from Telegram’s schema. Throws StateError if [connect] hasn’t been called yet. See Raw Invoke.

invoke

Future<t.TlObject> invoke(t.TlMethod method) {

The low-level escape hatch: invoke any raw t.TlMethod (from package:ptgc/raw.dart) that ptgc doesn’t have a typed wrapper for yet. Handles DC migration and error translation just like every other method in this package.

callRaw

Future<T> callRaw<T extends t.TlObject>(
  Future<t.Result<T>> Function() action,
) async {

Runs action, transparently handling Telegram’s *_MIGRATE_* redirects (retrying once against the correct data center) and translating any remaining raw RPC error into a PtgcException.

This is what every ptgc namespace method is built on. Reach for it yourself if you’re calling [raw] methods directly and want the same error handling everyone else gets.

rememberSignedInUser

Future<void> rememberSignedInUser(int id) async {

Call this right after a successful login (or after loading a session that already has [userId]) so the session file remembers who’s signed in. auth.signIn/checkPassword already call this for you.