zagram v0.0.2
zagram
Zagram is pure-js implementation of mtproto client library.
Node version to build: v12.13.0
Build Bundle
npm intallnpm run buildpls check
dist/directory forzagram.min.jsand `zagram.min
npm commands
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 bundleBasic usage
MTProto
MTProto(url: string, schema: Object)- is a base class to keep connection with telegram serverArguments:
url - url of telegram server
schema - object with layer that should be used
Public Methods:
init()- create authorization key and starts connection with telegram serveraddEventListener(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 telegramUpdatesobject indetailparamrequest(msg_obj: Object) -> Promise- sends rpc call to telegram server.msg_objgenerated withmethodFromSchemaandconstructorFromSchemafunctions. Returns promise with result;upload(file: File, progressCb: Function) -> { promise: Promise, cancel: Functon }- upload file to telegram server allow to track progress withprogressCbfunction, returns promise and cancel function to stop uploadingdownload(location, options: {size: Number, progressCb: Functon}) -> { promise: Promise, cancel: Function }- downloads file from telegram server by file location, allow to track downloading progress ifprogressCbpassed. returns promise of downloaded file, and returns cancel function to cancel downloading;schema
schema- current schema of 108 layermethodFromSchema
methodFromSchema(schema, methodName, params)- returns rpc call objectArguments
schema- telegram schema object that will be used to generate requestmethodName- method name that will be invoked on serverparams- object with params formethodNamemethod
method(methodName, parmas)- same asmethodFromSchemabut with predefinedschemaconstructorFromSchema
constructorFromSchema(schema, constructorName, params)- returns object build for telegramArguments
schema- telegram schema object that will be used to generate requestconstructorName- method name that will be invoked on serverparams- object with params formethodNameconstruct
construct(constructorName, params)- same asconstructorFromSchemabut with predefined schemaisMessageOf
isMessageOf(type, obj)- checks thatobjhas typetypeArguments
type- stringobj- objectisMethodOf
isMethodOf(methodName, obj)- checks thatobjis built as method withmethodNameArguments
methodName- stringobj- objectisObjectOf
isObjectOf(constructorName, obj)- checks thatobjis built with constructorconstructorNameconstructorName- stringobj- objecttlLoads
tlLoads(schema, buffer)- loads object frombufferby rules that describe inschemaArguments
schema- schema that will be usedbuffer-ArayBufferto parse datatlDumps
tlDumps(schema, obj)- dumps object toArrayBufferby schema rules.Arguments
schema- schema that will be usedobj- object to dumpExample
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**
```js
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