Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

zerobias/telegram-mtproto

Repository files navigation

‼️Better use@mtproto/core

telegram-mtproto

npm version

Telegram Mobile Protocol(MTProto)library ines6

About MTProto..

MTProtois theTelegram Messengerprotocol "designed for access to a server API from applications running on mobile devices".

The Mobile Protocol is subdivided into three components (from the official site):

  • High-level component (API query language): defines the method whereby API queries and responses are converted to binary messages.

  • Cryptographic (authorization) layer: defines the method by which messages are encrypted prior to being transmitted through the transport protocol.

  • Transport component: defines the method for the client and the server to transmit messages over some other existing network protocol (such as, http, https, tcp, udp).

telegram-mtproto in short..

No more additional libs. Thetelegram-mtprotolibrary implements theMobile Protocoland provides all features for work with telegram protocol:

  • A high level api for server connection

  • Promise-based API

  • HTTP connectionsimplemented in the transport layer

  • Web workersupport for blazing fast crypto math works in background

  • A cipher implementation forAES and RSA encryptionin the security layer

  • Bothplain-text and encrypted messageto communicate data with the server

  • Diffie-Hellman key exchangesupported by theprime factorizationfunction implemented in the security layer

  • MTProto TL-Schemacompilation asjavascript classes and functions

  • Customasync storagesupport for saving user data between sessions

Installation

$ npm install --save telegram-mtproto@beta

Usage

importMTProtofrom'telegram-mtproto'

constphone={
num:'+9996620001',
code:'22222'
}

constapi={
layer:57,
initConnection:0x69796de9,
api_id:49631
}

constserver={
dev:true//We will connect to the test server.
}//Any empty configurations fields can just not be specified

constclient=MTProto({server,api})

asyncfunctionconnect(){
const{phone_code_hash}=awaitclient('auth.sendCode',{
phone_number:phone.num,
current_number:false,
api_id:49631,
api_hash:'fb050b8f6771e15bfda5df2409931569'
})
const{user}=awaitclient('auth.signIn',{
phone_number:phone.num,
phone_code_hash:phone_code_hash,
phone_code:phone.code
})

console.log('signed as ',user)
}

connect()

Above we used two functions from the API.

typeauth.sendCode=(phone_number:string,sms_type:int,
api_id:int,api_hash:string,lang_code:string)=>{
phone_registered:boolean,
phone_code_hash:string,
send_call_timeout:int,
is_password:boolean
}

typeauth.signIn=(phone_number:string,phone_code_hash:string,phone_code:string)=>{
expires:int,
user:User
}

Moreaboutthem,as well as about many other methods, you can read in theofficial documentation.

Additional examples can be obtained fromexamplesfolder.

Storage

You can use your own storages likelocalForagefor saving data. Module accepts the following interface

interfaceAsyncStorage{
get(key:string):Promise<any>;
set(key:string,value:any):Promise<void>;
remove(...keys:string[]):Promise<void>;
clear():Promise<void>;
}
import{MTProto}from'telegram-mtproto'
import{api}from'./config'
importCustomStoragefrom'./storage'

constclient=MTProto({
api,
app:{
storage:CustomStorage
}
})

License

The project is released under theMit License