npm package: @iam4x/teleproto
Description: NodeJS MTProto API Telegram client library,
Latest version: 1.224.0

# @iam4x/teleproto

> Fork from teleproto, updated dependencies and fixed circular dependencies

Modern Telegram MTProto client for Node.js, written in TypeScript.
`@iam4x/teleproto` is a high-performance fork of GramJS focused on clean API ergonomics, runtime reliability, and up-to-date Telegram layers.

## Features

- **MTProto-first**: Full Telegram API access through high-level client methods and raw `Api` calls.
- **TypeScript-friendly**: Strong typings across client methods, events, sessions, and TL objects.
- **Session options**: Use `StringSession` for portability or `StoreSession` for local persistence.
- **Event system**: Handle updates with builders like `NewMessage`, `EditedMessage`, `CallbackQuery`, and more.
- **Examples included**: Ready-to-run scripts in `teleproto_examples`.

## Installation

```bash
npm i @iam4x/teleproto
```

## Quick Start

1. Open https://my.telegram.org
2. Create an app in **API development tools**
3. Copy your `api_id` and `api_hash`

```ts
import { TelegramClient } from "@iam4x/teleproto";
import { StringSession } from "@iam4x/teleproto/sessions";
import readline from "readline";

const apiId = 123456;
const apiHash = "0123456789abcdef0123456789abcdef";
const session = new StringSession("");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const ask = (q: string) =>
  new Promise<string>((resolve) => rl.question(q, resolve));

async function main() {
  const client = new TelegramClient(session, apiId, apiHash, {
    connectionRetries: 5,
  });

  await client.start({
    phoneNumber: async () => await ask("Phone number: "),
    password: async () => await ask("2FA password (if enabled): "),
    phoneCode: async () => await ask("Code from Telegram: "),
    onError: (err) => console.error(err),
  });

  console.log("Connected as:", (await client.getMe())?.username || "unknown");
  console.log("String session:\n", client.session.save());

  await client.sendMessage("me", { message: "Hello from teleproto!" });
  await client.disconnect();
  rl.close();
}

main().catch(console.error);
```

## Sessions

Use `StringSession` when you want to store auth as a single string:

```ts
import { StringSession } from "@iam4x/teleproto/sessions";
const session = new StringSession("");
```

Use `StoreSession` when you want local folder-based persistence:

```ts
import { StoreSession } from "@iam4x/teleproto/sessions";
const session = new StoreSession("teleproto_session");
```

## Events

```ts
import { NewMessage } from "@iam4x/teleproto/events";

client.addEventHandler(
  async (event) => {
    const text = event.message.message || "";
    if (/^hello$/i.test(text.trim())) {
      await event.message.reply({ message: "Hi there!" });
    }
  },
  new NewMessage({})
);
```

## Raw API

```ts
import { Api } from "@iam4x/teleproto";

const result = await client.invoke(
  new Api.help.GetConfig()
);
console.log(result);
```

## Examples

Practical scripts are available in `teleproto_examples`:

- `print_updates.ts`
- `print_messages.ts`
- `replier.ts`
- `interactive_terminal.ts`

Run any example from the project root:

```bash
npx ts-node --transpile-only teleproto_examples/print_updates.ts
```

## License

MIT