teleproto
A modern Telegram client library written in TypeScript for Node.js, forked from GramJS with performance and size improvements.
Quick Start
Here's how to get started with teleproto:
Installation
$ npm i teleproto
Authentication Setup
- Login to your Telegram account
- Click "API development tools" and create an application
- Save your API ID and hash (never share these with anyone)
Basic Usage
import { TelegramClient } from "teleproto";
import { StringSession } from "teleproto/sessions";
import readline from "readline";
const apiId = 123456;
const apiHash = "123456abcdefg";
const stringSession = new StringSession("");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function main() {
console.log("Starting teleproto client...");
const client = new TelegramClient(stringSession, apiId, apiHash, {
connectionRetries: 5,
});
await client.start({
phoneNumber: async () =>
await new Promise(resolve => rl.question("Phone number: ", resolve)),
password: async () =>
await new Promise(resolve => rl.question("Password: ", resolve)),
phoneCode: async () =>
await new Promise(resolve => rl.question("Verification code: ", resolve)),
onError: (err) => console.error(err),
});
console.log("Connected successfully!");
console.log("Session string:", client.session.save());
await client.sendMessage("me", { message: "Hello from teleproto!" });
await client.disconnect();
rl.close();
}
main();
You can also use StoreSession
to save auth data to a folder instead of a string:
import { StoreSession } from "teleproto/sessions";
const storeSession = new StoreSession("session_folder");
const client = new TelegramClient(storeSession, apiId, apiHash, {});
API Usage
Calling Raw API Methods
await client.invoke(new Api.RequestClass({ param1: "value1" }));
Event Handling
import { NewMessage } from "teleproto/events";
client.addEventHandler(async (event) => {
console.log("New message received:", event.message.text);
if (event.message.text === "Hello") {
await event.message.reply("Hi there!");
}
}, new NewMessage({}));
Ask a question
If you have any questions or need help, feel free to join our Telegram group or open an issue on GitHub