Skip to main content

Installation

Requirements#

  • Node.js version >= 12 or above (which can be checked by running node -v). You can use nvm for managing multiple Node versions on a single machine installed

Install#

yarn add @mtproto/core -E
# or
npm i @mtproto/core -E

Select environment#

Depending on your environment, you need to use different import paths. Currently, two environments are supported:

  1. node โ€” if you run the code in the Node.js
  2. browser โ€” if you run the code in a browser environment

Import for node environment#

const MTProto = require('@mtproto/core');
// or
// const MTProto = require('@mtproto/core/envs/node');

Import for browser environment#

const MTProto = require('@mtproto/core/envs/browser');

Configure default storage#

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

For node environment#

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'),
},
});

For browser environment#

The window.localStorage is used for storage. You don't need to pass storageOptions

Getting api_id and api_hash#

About abuse

If you use the Telegram API for flooding, spamming, faking subscriber and view counters of channels, you will be banned forever

  1. Login into https://my.telegram.org/
  2. Go to https://my.telegram.org/apps and fill out the form
  3. Take the api_id and api_hash from the "app configuration" section

Final check#

Create simple file#

example.js
const path = require('path');
const MTProto = require('@mtproto/core');
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);
});

Run#

node example.js