npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mtproton

v6.0.0

Published

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

Downloads

14

Readme

mtproton

NPM Downloads

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

Install

npm i mtproton -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 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);
});

Guides

API

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

api_id: number and api_hash: string and storageOptions: { path: string, instance: object }

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.

storageOptions: { instance?: customLocalStorage, path?: 'path to json storage file' }

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

Custom Storage

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:

  1. tempStorage only stores data while the script is running

Example:

const tempStorage = require('mtproton/core/src/storage/temp');

new MTProto({
  storageOptions: {
    instance: tempStorage,
  },
});

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

  • API methods — https://core.telegram.org/methods
  • API schema — https://core.telegram.org/schema