Awesome Open Source
Awesome Open Source

@mtproto/core

NPM GitHub Actions Downloads Telegram channel

Telegram API JS (MTProto) client library for browser and nodejs

Install

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

Quick start

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

Guides

API

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:

  1. tempLocalStorage only stores data while the script is running

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.

  1. If the method needs the flags parameter, flags is calculated automatically 🙃

  2. 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.

Example:

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.

Useful references


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (67,535) 
api (1,593) 
library (1,261) 
browser (425) 
client (370) 
telegram (312) 
messenger (106) 
telegram-api (35) 
mtproto (24) 
lib (23) 
tdlib (14) 

Find Open Source By Browsing 7,000 Topics Across 59 Categories

Advertising 📦 10
All Projects
Application Programming Interfaces 📦 124
Applications 📦 192
Artificial Intelligence 📦 78
Blockchain 📦 73
Build Tools 📦 113
Cloud Computing 📦 80
Code Quality 📦 28
Collaboration 📦 32
Command Line Interface 📦 49
Community 📦 83
Companies 📦 60
Compilers 📦 63
Computer Science 📦 80
Configuration Management 📦 42
Content Management 📦 175
Control Flow 📦 213
Data Formats 📦 78
Data Processing 📦 276
Data Storage 📦 135
Economics 📦 64
Frameworks 📦 215
Games 📦 129
Graphics 📦 110
Hardware 📦 152
Integrated Development Environments 📦 49
Learning Resources 📦 166
Legal 📦 29
Libraries 📦 129
Lists Of Projects 📦 22
Machine Learning 📦 347
Mapping 📦 64
Marketing 📦 15
Mathematics 📦 55
Media 📦 239
Messaging 📦 98
Networking 📦 315
Operating Systems 📦 89
Operations 📦 121
Package Managers 📦 55
Programming Languages 📦 245
Runtime Environments 📦 100
Science 📦 42
Security 📦 396
Social Media 📦 27
Software Architecture 📦 72
Software Development 📦 72
Software Performance 📦 58
Software Quality 📦 133
Text Editors 📦 49
Text Processing 📦 136
User Interface 📦 330
User Interface Components 📦 514
Version Control 📦 30
Virtualization 📦 71
Web Browsers 📦 42
Web Servers 📦 26
Web User Interface 📦 210