Zagram is pure-js implementation of mtproto client library.
Node version to build: v12.13.0
npm intall
npm run build
- pls check
dist/
directory forzagram.min.js
and `zagram.min
npm run test
- run tests for mtproto with jestnpm run testw
- run tests in watch modenpm run lint
- check code with eslintnpm run lintw
- eslint in watch modenpm run lint-fix
- fix lint errorsnpm run build
- build bundle
MTProto(url: string, schema: Object)
- is a base class to keep connection with telegram server
Arguments:
- url - url of telegram server
- schema - object with layer that should be used
Public Methods:
init()
- create authorization key and starts connection with telegram server
addEventListener(handler: Function)
- added event listener for telegram event.
Emited Events:
statusChanged
- emits when auth key has been crated or creation has been failedtelegramUpdate
- emits when update from telegram received. Event contains telegramUpdates
object indetail
param
request(msg_obj: Object) -> Promise
- sends rpc call to telegram server. msg_obj
generated
with methodFromSchema
and constructorFromSchema
functions. Returns promise with result;
upload(file: File, progressCb: Function) -> { promise: Promise, cancel: Functon }
- upload file to telegram server
allow to track progress with progressCb
function, returns promise and cancel function to stop uploading
download(location, options: {size: Number, progressCb: Functon}) -> { promise: Promise, cancel: Function }
- downloads
file from telegram server by file location, allow to track downloading progress if progressCb
passed.
returns promise of downloaded file, and returns cancel function to cancel downloading;
schema
- current schema of 108 layer
methodFromSchema(schema, methodName, params)
- returns rpc call object
Arguments
schema
- telegram schema object that will be used to generate requestmethodName
- method name that will be invoked on serverparams
- object with params formethodName
method(methodName, parmas)
- same as methodFromSchema
but with predefined schema
constructorFromSchema(schema, constructorName, params)
- returns object build for telegram
Arguments
schema
- telegram schema object that will be used to generate requestconstructorName
- method name that will be invoked on serverparams
- object with params formethodName
construct(constructorName, params)
- same as constructorFromSchema
but with predefined schema
isMessageOf(type, obj)
- checks that obj
has type type
Arguments
type
- stringobj
- object
isMethodOf(methodName, obj)
- checks that obj
is built as method with methodName
Arguments
methodName
- stringobj
- object
isObjectOf(constructorName, obj)
- checks that obj
is built with constructor constructorName
constructorName
- stringobj
- object
tlLoads(schema, buffer)
- loads object from buffer
by rules that describe in schema
Arguments
schema
- schema that will be usedbuffer
-ArayBuffer
to parse data
tlDumps(schema, obj)
- dumps object to ArrayBuffer
by schema rules.
Arguments
schema
- schema that will be usedobj
- object to dump
Connect to telegram server:
const { MTProto, schema, pems, methodFromSchema } = zagram;
const url = 'ws://149.154.167.40/apiws';
const API_ID = 1005944;
const API_HASH = 'dfbf8ed1e37d1cd1ad370e7431ed8a87';
const connection = new MTProto({url: url, protocols: ['binary']}, schema, pems);
connection.addEventListener('statusChanged', (e) => {
if (e.status === 'AUTH_KEY_CREATED') {
const obj = methodFromSchema(
schema,
'invokeWithLayer',
{
layer: 108,
query: methodFromSchema(
schema,
'initConnection',
{
api_id: API_ID,
device_model: navigator.userAgent,
system_version: navigator.platform,
app_version: '0.0.1',
system_lang_code: navigator.language,
lang_pack: '',
lang_code: 'ru-ru',
query: methodFromSchema(schema, 'help.getConfig'),
},
),
},
);
connection.request(obj).then(console.log);
}
});
connection.init();
Connect to mtpylon server
const { MTProto, methodFromSchema } = zagram;
const WS_URL = 'ws://localhost:8081/ws';
const PUB_KEYS_URL = 'http://localhost:8081/pub-keys';
const SCHEMA_URL = 'http://localhost:8081/schema';
function initConnection(schema, pems) {
return new Promise((resolve, reject) => {
const connection = new MTProto(WS_URL, schema, pems);
connection.addEventListener('statusChanged', (e) => {
if (e.status === 'AUTH_KEY_CREATED') {
resolve([connection, schema]);
} else {
reject(e.status);
}
});
connection.init();
});
}
Promise
.all([
fetch(SCHEMA_URL).then(r => r.json()),
fetch(PUB_KEYS_URL).then(r => r.json()),
])
.then(([schema, pems]) => initConnection(schema, pems))
.then(([connection, schema]) => {
const rpc = methodFromSchema(schema, 'echo', {'content': 'hello world'});
return connection.request(rpc);
})
.then(console.log);
Application example: https://github.com/Zapix/echo-server