ptgc
MTProto client for Dart

ptgc

A typed MTProto client for logging in as a real Telegram user account and controlling chats and channels — ban, kick, restrict, promote, invite, and list members — plus contacts, dialogs, and messaging. Where ptgb (the companion package) talks to Telegram as a bot, ptgc talks to Telegram as a user, which is the only way to do the things a bot API token simply can’t.

import 'package:ptgc/ptgc.dart';

Future<void> main() async {
  final client = TelegramClient.fromEnv(); // reads API_ID / API_HASH from .env
  await client.connect();

  if (!client.isSignedIn) {
    final sent = await client.auth.sendCode('+15551234567');
    final result = await client.auth.signIn(code: '12345', phoneCodeHash: sent.phoneCodeHash);
    if (result.status == SignInStatus.passwordRequired) {
      await client.auth.checkPassword('your 2FA password');
    }
  }

  final me = await client.contacts.resolveUsername('someone');
  if (me != null) await client.members.ban(chatId, me.id);

  await client.disconnect();
}

Why ptgc

  • Real user-account control — ban, kick, restrict, promote, and invite members the way only a logged-in account (or an admin bot) can, via Members.
  • A typed session, not raw MTProtoTelegramClient.fromEnv() handles the handshake, data-center migration, and session persistence; you work with client.auth, client.chats, client.members, client.contacts, and client.messages.
  • Automatic peer resolution — every call feeds the users/chats it sees into a PeerCache, so you can address a chat or user by plain integer ID afterwards without threading access hashes through your own code.
  • Typed permissionsAdminRights and BannedRights replace Telegram’s boolean-flag grab bags with named, documented fields.
  • A low-level escape hatch (client.invoke, client.raw) for any MTProto method that doesn’t have a typed wrapper yet — the same error handling and DC-migration logic every namespace method gets.
  • A live event stream (client.events) for new messages and membership/admin changes, without hand-rolling update parsing.

Install

dart pub add ptgc