Televerseteleverse.

RawAPI Class

Direct access to the Telegram Bot API.

The RawAPI class provides a low-level, type-safe interface to the Telegram Bot API. It mirrors the official API methods and handles the HTTP requests, JSON serialization, and response parsing for you. While the Bot class is great for handling updates, RawAPI is your tool for sending actions back to Telegram.

Initialization

You can create a standalone instance of RawAPI if you just need to send requests without running a bot listener. This is useful for scripts, admin tools, or serverless functions.

main.dart
import 'package:televerse/televerse.dart';

void main() {
  // Initialize with your bot token
  final api = RawAPI('YOUR_BOT_TOKEN');
}

Access from Bot

If you are using the Bot class, you don't need to create a separate RawAPI instance. The bot instance already has one ready for you at bot.api.

main.dart
final bot = Bot('YOUR_BOT_TOKEN');

// Access API through bot instance
await bot.api.sendMessage(ChatID(123456789), 'Hello!');

Making Requests

The RawAPI class contains methods for almost every action available in the Telegram Bot API. These methods are named exactly as they appear in the official documentation.

Sending Messages

main.dart
// Send a text message
await api.sendMessage(
  ChatID(123456789),
  'Hello World!',
  parseMode: ParseMode.html,
);

Sending Media

Televerse makes it easy to send files. You can use the InputFile class to send files from your file system, URLs, or raw bytes.

main.dart
import 'dart:io';

// Send a photo from a local file
final photo = File('path/to/photo.jpg');
await api.sendPhoto(
  ChatID(123456789),
  InputFile.fromFile(photo),
  caption: 'Check out this photo!',
);

Advanced Usage

For advanced use cases, you can use the generic call method in the RawAPI class which makes its instances callable.

main.dart
// Type-safe generic call
final message = await api<Map<String, dynamic>>(
  APIMethod.sendMessage,
  Payload({
    'chat_id': 123456789,
    'text': 'Hello generic!',
  }),
);

See Also