AlexXanderGrib / node-tdlib

TDLib cross platform API
MIT License
4 stars 0 forks source link
tdlib telegram typescript

TDLib Native

Test Status Downloads last commit codecov GitHub tdlib-native Known Vulnerabilities Quality npm license MIT Size

Why use this package?

📦 Installation

⚙️ Usage

This is raw wrapper of TDLib

import { Client, Authenticator } from "tdlib-native";
import { TDLibAddon } from "tdlib-native/addon";

async function init() {
  // Loading addon
  const adapter = await TDLibAddon.create();

  // Make TDLib shut up. Immediately
  Client.disableLogs(adapter);

  const client = new Client(adapter);
  const authenticator = Authenticator.create(client)
    .tdlibParameters({
      /* options */
    })
    .token(process.env.TELEGRAM_BOT_TOKEN);

  // Start polling responses from TDLib
  client.start();
  await authenticator.authenticate();
  // client authorized as bot

  // Call any tdlib method
  await client.api.getOption({ name: "version" });
  // => Promise { _: "optionValueString", value: "1.8.22" }

  // or use a wrapper
  await client.tdlibOptions.get("version");
  // => Promise "1.8.22"

  // Subscribe to updates
  client.updates.subscribe(console.log);

  // Pause receiving updates. Will freeze method all running API calls
  // client.pause();
  // Resume pause
  // client.start();

  // Destroy
  await client.api.close({});
  client.destroy();
}

Usage with RxJS

// Observable will complete after client.destroy() call
const updates = new Observable(client.updates.toRxObserver());