GramJS is a JavaScript-based client library for interacting with the Telegram MTProto API, designed to function across both Node.js environments and web browsers. It is currently in active development, with version 2.26.22 being the latest stable release at the time of writing, showing a pattern of frequent updates. Key differentiators include its core architecture, which is based on the popular Python Telethon library, providing a robust and feature-rich foundation. It allows developers to build userbots and custom Telegram applications by directly accessing the MTProto API, handling session management, and offering mechanisms for sending messages and invoking raw API methods. The library supports persistent sessions, either via string-based or file-based storage, to avoid repeated logins. It also provides dedicated guidance for browser integration, typically requiring webpack for bundling.
npm install telegramVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to initialize the `TelegramClient`, authenticate using phone number/password/code (or a saved session string from `TELEGRAM_SESSION` environment variable), and send a message to 'me'. It requires `TELEGRAM_API_ID` and `TELEGRAM_API_HASH` environment variables.
Use `import` statements for all GramJS modules. If using Node.js without a bundler, ensure your project's `package.json` has `"type": "module"`.
Always use environment variables (e.g., `process.env.TELEGRAM_API_ID`), a secure configuration management system, or dedicated secrets managers for these credentials.
Refer to the 'Running GramJS inside browsers' section in the official documentation, which typically involves using `webpack` or a similar bundler to create a browser-compatible build.
Always refer to the official documentation at `gram.js.org` or the beta documentation at `gram.js.org/beta` for the most current and accurate information.
Consult resources like the provided Gist 'My ISP blocks Telegram. How can I still use GramJS?' for potential solutions, which often involve using proxies or VPNs, configurable via the `connection` option in `TelegramClient`.
Change `const telegram = require('telegram');` to `import { TelegramClient } from 'telegram';` and ensure your `package.json` has `"type": "module"` or use a bundler like Webpack/Rollup.Ensure you are running the correct build for your target environment. For Node.js, ensure you're not using browser-specific configurations. For browsers, use a bundler like Webpack as described in the GramJS documentation.
Double-check your `TELEGRAM_API_ID` and `TELEGRAM_API_HASH` values obtained from my.telegram.org. If using a session string (`TELEGRAM_SESSION`), ensure it's valid and not corrupted. Try logging in fresh if issues persist.
Ensure the `TelegramClient` object is correctly instantiated and accessible within the scope where `start()` is invoked. Verify that `await` is used correctly with async functions and that the client object exists.
No dependency data recorded yet.