Telegram API JS (MTProto) client library for browser and nodejs
- Actual. 133 layer in the API scheme
- Fast. For the Node.js, it uses the TCP and crypto module. For the browser, it uses WebSocket and window.crypto
- Easy. Cryptography and bytes is hidden. Just make requests to the API
- Smart. Automatically sync authorization on all DC's
- Events. Subscribe to updates via the EventEmitter API
- 2FA. Use the library's built-in function to calculate 2FA parameters
- Secure. Complies with Telegram security guidelines
npm i mtproton -E
You need api_id and api_hash. If you do not have them yet, then get them according to the official instructions: creating your Telegram application.
const path = require('path');
const MTProto = require('mtproton');
const api_id = YOU_API_ID;
const api_hash = YOU_API_HASH;
// 1. Create instance
const mtproto = new MTProto({
api_id,
api_hash,
storageOptions: {
path: path.resolve(__dirname, './data/1.json'),
},
});
// 2. Print the user country code
mtproto.call('help.getNearestDc').then(result => {
console.log('country:', result.country);
});
api_id and api_hash are required. If you do not have them yet, then get them according to the official instructions: creating your Telegram application.
Default: false
. Use test data centers. On test servers, you can use test phone numbers.
We have default storages. The storage is used to store the session (authentication keys, server salts and time offset). Depending on the environment, you need to pass different parameters for the storage. But you can also use custom storage
In the storageOptions.path, pass the absolute path to the json file through the constructor
new MTProto({
storageOptions: {
path: path.resolve(__dirname, './data/1.json'),
},
});
The window.localStorage is used for storage. You don't need to pass storageOptions
class CustomStorage {
set(key: string, value: string): Promise<void>;
get(key: string): Promise<string|null>;
}
const instance = new CustomStorage();
new MTProto({
storageOptions: {
instance,
},
});
We have ready-made storage:
tempStorage
only stores data while the script is running
Example:
const tempStorage = require('mtproton/core/src/storage/temp');
new MTProto({
storageOptions: {
instance: tempStorage,
},
});
Method name from methods list.
Parameters for method
from https://core.telegram.org/method/{method}#parameters
.
-
If the method needs the
flags
parameter,flags
is calculated automatically π -
If you need to pass a constructor use
_
. Example for users.getFullUser:
const params = {
id: {
_: 'inputUserSelf',
},
};
Specific DC id. By default, it is 2
. You can change the default value using mtproto.setDefaultDc method.
Default: true
. Copy authorization to all DC if the response contains auth.authorization
.
mtproto.call('help.getNearestDc', {}, {
dcId: 1
}).then(result => {
console.log('result:', result);
// { _: 'nearestDc', country: 'RU', this_dc: 1, nearest_dc: 2 }
}).catch(error => {
console.log('error.error_code:', error.error_code);
console.log('error.error_message:', error.error_message);
});
Method for handles updates.
Example of handling a updateShort with updateUserStatus:
mtproto.updates.on('updateShort', message => {
const { update } = message;
if (update._ === 'updateUserStatus') {
const { user_id, status } = update;
console.log(`User with id ${user_id} change status to ${status}`);
}
});
If a migration error occurs, you can use this function to change the default data center. You can also use options.dcId.
See the example in the authentication.
Provide params for initConnection method. I recommend running this function immediately after creating an instance of MTProto.
See the example in the quick start.
Function to calculate parameters for 2FA (Two-factor authentication). For more information about parameters, see the article on the Telegram website.
See the example in the authentication.
- API methods β https://core.telegram.org/methods
- API schema β https://core.telegram.org/schema