UNPKG

4.29 kBMarkdownView Raw
1# telegram-mtproto
2
3[![npm version][npm-image]][npm-url]
4
5**Telegram Mobile Protocol** [(MTProto)](https://core.telegram.org/mtproto) library in **es6**
6
7## About MTProto..
8
9**MTProto** is the [Telegram Messenger](http://www.telegram.org) protocol
10_"designed for access to a server API from applications running on mobile devices"_.
11
12The Mobile Protocol is subdivided into three components ([from the official site](https://core.telegram.org/mtproto#general-description)):
13
14 - High-level component (API query language): defines the method whereby API
15 queries and responses are converted to binary messages.
16
17 - Cryptographic (authorization) layer: defines the method by which messages
18 are encrypted prior to being transmitted through the transport protocol.
19
20 - Transport component: defines the method for the client and the server to transmit
21 messages over some other existing network protocol (such as, http, https, tcp, udp).
22
23
24
25## telegram-mtproto in short..
26
27No more additional libs.
28The **telegram-mtproto** library implements the **Mobile Protocol** and provides all features for work with telegram protocol:
29
30 - A high level api for server connection
31
32 - Promise-based API
33
34 - **HTTP connections** implemented in the transport layer
35
36 - **Web worker** support for blazing fast crypto math works in background
37
38 - A cipher implementation for **AES and RSA encryption** in the security layer
39
40 - Both **plain-text and encrypted message** to communicate data with the server
41
42 - **Diffie-Hellman key exchange** supported by the **prime factorization** function implemented in the security layer
43
44 - **MTProto TL-Schema** compilation as **javascript classes and functions**
45
46 - Custom **async storage** support for saving user data between sessions
47
48
49## Installation
50
51```bash
52$ npm install --save telegram-mtproto@beta
53```
54
55## Usage
56
57```javascript
58import MTProto from 'telegram-mtproto'
59
60const phone = {
61 num : '+9996620001',
62 code: '22222'
63}
64
65const api = {
66 layer : 57,
67 initConnection : 0x69796de9,
68 api_id : 49631
69}
70
71const server = {
72 dev: true //We will connect to the test server.
73} //Any empty configurations fields can just not be specified
74
75const client = MTProto({ server, api })
76
77async function connect(){
78 const { phone_code_hash } = await client('auth.sendCode', {
79 phone_number : phone.num,
80 current_number: false,
81 api_id : 49631,
82 api_hash : 'fb050b8f6771e15bfda5df2409931569'
83 })
84 const { user } = await client('auth.signIn', {
85 phone_number : phone.num,
86 phone_code_hash: phone_code_hash,
87 phone_code : phone.code
88 })
89
90 console.log('signed as ', user)
91}
92
93connect()
94```
95
96Above we used two functions from the API.
97```typescript
98type auth.sendCode = (phone_number: string, sms_type: int,
99 api_id: int, api_hash: string, lang_code: string) => {
100 phone_registered: boolean,
101 phone_code_hash: string,
102 send_call_timeout: int,
103 is_password: boolean
104 }
105
106type auth.signIn = (phone_number: string, phone_code_hash: string, phone_code: string) => {
107 expires: int,
108 user: User
109}
110```
111[More][send-code] about [them][sign-in], as well as about many other methods, you can read in the [official documentation][docs].
112
113Additional examples can be obtained from [examples][examples] folder.
114
115## Storage
116
117You can use your own storages like [localForage][localForage] for saving data.
118Module accepts the following interface
119
120```typescript
121interface AsyncStorage {
122 get(key: string): Promise<any>;
123 set(obj: any): Promise<void>;
124 remove(...keys: string[]): Promise<void>;
125 clear(): Promise<void>;
126}
127```
128
129```javascript
130import { MTProto } from 'telegram-mtproto'
131import { api } from './config'
132import CustomStorage from './storage'
133
134const client = MTProto({
135 api,
136 app: {
137 storage: CustomStorage
138 }
139})
140
141```
142
143## License
144
145The project is released under the [Mit License](./LICENSE)
146
147[examples]: https://github.com/zerobias/telegram-mtproto/tree/develop/examples
148[localForage]: https://github.com/localForage/localForage
149[docs]: https://core.telegram.org/
150[send-code]: https://core.telegram.org/method/auth.sendCode
151[sign-in]: https://core.telegram.org/method/auth.signIn
152[npm-url]: https://www.npmjs.org/package/telegram-mtproto
153[npm-image]: https://badge.fury.io/js/telegram-mtproto.svg
\No newline at end of file