
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@mtproto/core
Advanced tools
Telegram API JS (MTProto) client library for browser and nodejs
yarn add @mtproto/core -E
# or
npm i @mtproto/core -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 { MTProto } = require('@mtproto/core');
const api_id = 'YOU_API_ID';
const api_hash = 'YOU_API_HASH';
// 1. Create an instance
const mtproto = new MTProto({
api_id,
api_hash,
});
// 2. Provide params for initConnection method (optional)
mtproto.updateInitConnectionParams({
app_version: '10.0.0',
});
// 3. Get the user country code
mtproto.call('help.getNearestDc').then(result => {
console.log(`country:`, result.country);
});
new MTProto({ api_id, api_hash, test, customLocalStorage }) => mtproto
api_id: number
and api_hash: string
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.
test: boolean
Default: false
. Use test data centers. On test servers, you can use test phone numbers.
customLocalStorage: localStorage
Default for browser: window.localStorage
. Default for nodejs: node-localstorage
. Custom storage for save auth data. Your localStorage must follow this API:
class MyAsyncLocalStorage {
setItem(key: string, value: string): Promise<void>;
getItem(key: string): Promise<string|null>;
}
We have ready-made storage:
Example:
const { tempLocalStorage } = require('@mtproto/core/src/storage/temp');
const mtproto = new MTProto({
customLocalStorage: tempLocalStorage,
});
mtproto.call(method, params, options) => Promise
method: string
Method name from methods list.
params: object
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',
},
};
options.dcId: number
Specific DC id. By default, it is 2
. You can change the default value using mtproto.setDefaultDc method.
options.syncAuth: boolean
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);
});
mtproto.updates.on(updates, listener)
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}`);
}
});
mtproto.setDefaultDc(dcId) => Promise
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.
mtproto.updateInitConnectionParams(params)
Provide params for initConnection method. I recommend running this function immediately after creating an instance of MTProto.
See the example in the quick start.
getSRPParams({ g, p, salt1, salt2, gB, password }) => { A, M1 }
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.
FAQs
Telegram API JS (MTProto) client library for Node.js and browser
The npm package @mtproto/core receives a total of 919 weekly downloads. As such, @mtproto/core popularity was classified as not popular.
We found that @mtproto/core demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.