Socket
Socket
Sign inDemoInstall

@mtproto/core

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mtproto/core

Telegram API (MTProto) client library for browser and nodejs


Version published
Weekly downloads
886
decreased by-34.08%
Maintainers
1
Weekly downloads
 
Created
Source

@mtproto/core

NPM Travis Telegram channel

Telegram API (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,

  // Use test servers
  test: true,
});

// 2. Get the user country code
mtproto.call('help.getNearestDc').then(result => {
  console.log(`country:`, result.country);
});

API

new MTProto({ api_id, api_hash, test }) => mtproto

Example in quick start.

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 you need to pass a constructor use _. Example for auth.sendCode:

const params = {
  // api_id and api_hash @mtproto/core will set automatically
  phone_number: '+9996621111',
  settings: {
    _: 'codeSettings',
  }
};
options.dcId: number

Specific DC id. By default, it is 2. You can change the default value using mtproto.setDefaultDc method.

options.syncAuth: boolean

By default, it is true. Tells the @mtproto/core to 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)

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.

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.

Cases

Useful references

Keywords

FAQs

Package last updated on 07 May 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc