ptgb
A complete Telegram Bot API client for Dart

Getting Started

ptgb requires Dart SDK ^3.5.0.

1. Install the package

dart pub add ptgb

Or add it to pubspec.yaml directly, then run dart pub get:

dependencies:
  ptgb: ^1.1.0

2. Get a bot token

  1. Open a chat with @BotFather on Telegram.
  2. Send /newbot and follow the prompts.
  3. Copy the token it gives you — it looks like 123456:ABC-your-token-here.
  4. Keep it somewhere safe and never commit it to version control.

3. Load the token

Create a .env file next to pubspec.yaml:

TOKEN=123456:ABC-your-token-here

Add .env to .gitignore. See Configuration for alternatives — passing the token directly, custom file names, and so on.

4. Write your first bot

import 'package:ptgb/ptgb.dart';

Future<void> main() async {
  final bot = Bot(); // reads TOKEN from .env automatically

  await for (final update in bot.poll()) {
    if (update.text == '/start') {
      await bot.sendMessage(update.chatId!, 'Hello from ptgb!');
    }
  }
}

bot.poll() is a long-lived Stream<Update> — the loop above runs for as long as your process does. See Polling vs. Webhooks if you’d rather run a webhook server instead.

5. Run it

dart run bin/my_bot.dart

Message your bot on Telegram with /start and you should see the reply.

Next steps

  • Wrap your handlers in try/catch before going further — see Error Handling.
  • Browse the Examples to add keyboards, media, payments, or admin features.
  • Look up any method’s exact signature in the API Reference.