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

Commit

Permalink
Update readme, fix bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Boldyrev <[email protected]>
  • Loading branch information
zerobias committed Mar 15, 2017
1 parent 224bda0 commit 7951d03
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 64 deletions.
120 changes: 87 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## About MTProto..

**MTProto** is the [Telegram Messenger](http://www.telegram.org ) protocol
**MTProto** is the [Telegram Messenger](http://www.telegram.org) protocol
_"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](https://core.telegram.org/mtproto#general-description)):
Expand All @@ -31,7 +31,9 @@ The **telegram-mtproto** library implements the **Mobile Protocol** and provides

- Promise-based API

- Both **TCP** and **HTTP connections** implemented in the transport layer
- **HTTP connections** implemented in the transport layer

- **Web worker** support for blazing fast crypto math works in background

- A cipher implementation for **AES and RSA encryption** in the security layer

Expand All @@ -41,60 +43,112 @@ The **telegram-mtproto** library implements the **Mobile Protocol** and provides

- **MTProto TL-Schema** compilation as **javascript classes and functions**

- Custom **async storage** support for saving user data between sessions


## Installation

```bash
$ npm install --save telegram-mtproto@beta
```

## Usage

```javascript
const { Telegram, network } = require('telegram-mtproto')
import MTProto from 'telegram-mtproto'

const fileSchema = require('./api-schema-57.json')
const phone = {
num : '+9996620001',
code: '22222'
}

const telegram = new Telegram(fileSchema)
const addKey = key => telegram.addPublicKey(key)
publicKeys.forEach(addKey)
const api = {
invokeWithLayer: 0xda9b0d0d,
layer : 57,
initConnection : 0x69796de9,
api_id : 49631
}

const connection = new network.http(server)
const client = telegram.createClient()
const server = {
dev: true //We will connect to the test server.
} //Any empty configurations fields can just not be specified

client.setConnection(connection)
connection.connect()
.then(() => console.log(`api ready for requests`))
.then(() => client.callApi('auth.sendCode', {
const client = MTProto({ server, api })

async function connect(){
const { phone_code_hash } = await client('auth.sendCode', {
phone_number : phone.num,
current_number: false,
api_id : config.id,
api_hash : config.hash
}).then(
({ phone_code_hash }) =>
client.callApi('auth.signIn', {
phone_number : phone.num,
phone_code_hash: phone_code_hash,
phone_code : phone.code
}))
api_id : 49631,
api_hash : 'fb050b8f6771e15bfda5df2409931569'
})
const { user } = await client('auth.signIn', {
phone_number : phone.num,
phone_code_hash: phone_code_hash,
phone_code : phone.code
})

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

connect()
```

## Installation
```bash
$ npm install --save telegram-mtproto
Above we used two functions from the API.
```typescript
type auth.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
}

type auth.signIn = (phone_number: string, phone_code_hash: string, phone_code: string) => {
expires: int,
user: User
}
```
[More][send-code] about [them][sign-in], as well as about many other methods, you can read in the [official documentation][docs].
## API
Additional examples can be obtained from [examples][examples] folder.
- **network**. Classes for network connection
## Storage
- **http**
You can use your own storages like [localForage][localForage] for saving data.
Module accepts the following interface
- **tcp**
```typescript
interface AsyncStorage {
get(key: string): Promise<any>;
set(obj: any): Promise<void>;
remove(...keys: string[]): Promise<void>;
clear(): Promise<void>;
}
```

- **tl**. Telegram schema api
```javascript
import { MTProto } from 'telegram-mtproto'
import { api } from './config'
import CustomStorage from './storage'

- **Telegram**. High level api.
const client = MTProto({
api,
app: {
storage: CustomStorage
}
})

- **createClient** *() => TelegramClient*
```

## License

The project is released under the [Mit License](./LICENSE)

[examples]: https://github.com/zerobias/telegram-mtproto/tree/develop/examples
[localForage]: https://github.com/localForage/localForage
[docs]: https://core.telegram.org/
[send-code]: https://core.telegram.org/method/auth.sendCode
[sign-in]: https://core.telegram.org/method/auth.signIn
[npm-url]: https://www.npmjs.org/package/telegram-mtproto
[npm-image]: https://badge.fury.io/js/telegram-mtproto.svg
4 changes: 2 additions & 2 deletions examples/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ApiManager } = require('../lib/index')
const { MTProto } = require('../lib')

const api = {
invokeWithLayer: 0xda9b0d0d,
Expand All @@ -11,6 +11,6 @@ const api = {

const server = { webogram: true, dev: true }

const telegram = new ApiManager({ api, server })
const telegram = MTProto({ api, server })

module.exports = telegram
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,55 +26,55 @@
"author": "Zero Bias",
"dependencies": {
"@goodmind/node-cryptojs-aes": "^0.5.0",
"ajv": "^4.11.3",
"ajv": "^4.11.5",
"ajv-keywords": "^1.5.1",
"axios": "^0.15.3",
"bluebird": "^3.4.7",
"debug": "^2.6.1",
"debug": "^2.6.3",
"detect-node": "^2.0.3",
"eventemitter2": "^3.0.0",
"eventemitter2": "^3.0.2",
"pako": "^1.0.4",
"ramda": "^0.23.0",
"randombytes": "^2.0.3",
"rusha": "^0.8.5",
"setimmediate": "^1.0.5",
"worker-loader": "^0.7.1"
"worker-loader": "^0.8.0"
},
"devDependencies": {
"@types/debug": "0.0.29",
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-cli": "^6.24.0",
"babel-core": "^6.24.0",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.3.2",
"babel-loader": "^6.4.0",
"babel-plugin-closure-elimination": "^1.1.14",
"babel-plugin-transform-async-to-generator": "^6.22.0",
"babel-plugin-transform-async-to-module-method": "^6.22.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.23.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"cross-env": "^3.1.4",
"eslint": "^3.15.0",
"eslint-plugin-babel": "^4.0.1",
"eslint-plugin-flowtype": "^2.30.0",
"eslint-plugin-promise": "^3.4.2",
"fast-async": "^6.2.1",
"flow-bin": "^0.40.0",
"cross-env": "^3.2.4",
"eslint": "^3.17.1",
"eslint-plugin-babel": "^4.1.1",
"eslint-plugin-flowtype": "^2.30.3",
"eslint-plugin-promise": "^3.5.0",
"flow-bin": "^0.41.0",
"hirestime": "^3.1.0",
"nightmare": "^2.9.1",
"nightmare": "^2.10.0",
"prompt": "^1.0.0",
"rimraf": "^2.5.4",
"tap": "^10.1.1",
"rimraf": "^2.6.1",
"tap": "^10.3.0",
"webpack": "^2.2.1"
},
"engines": {
"node": ">0.10.30"
"node": ">=6.0"
},
"bugs": {
"url": "https://github.com/zerobias/telegram-mtproto/issues"
},
"files": [
"lib",
"test",
"examples",
"dist",
"schema",
"source",
"es",
Expand All @@ -99,5 +99,5 @@
"type": "git",
"url": "git+https://github.com/zerobias/telegram-mtproto.git"
},
"version": "2.1.1"
"version": "2.2.0"
}
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import MTProto from './service/main/wrap'

export { CryptoWorker } from './crypto'
export { bin } from './bin'
export { ApiManager } from './service/api-manager/index'

import * as MtpTimeManager from './service/time-manager'
export { MtpTimeManager }

import MTProto from './service/main/wrap'
export default MTProto
module.exports = MTProto
export { MTProto }
export default MTProto
2 changes: 1 addition & 1 deletion src/tl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ export class Deserialization {
const compressed = this.fetchBytes(`${field}[packed_string]`)
const uncompressed = gzipUncompress(compressed)
const buffer = bytesToArrayBuffer(uncompressed)
const newDeserializer = new Deserialization(buffer)
const newDeserializer = new Deserialization(buffer, { mtproto: this.mtproto, override: this.override }, this.api, this.mtApi)

return newDeserializer.fetchObject(type, field)
}
Expand Down
4 changes: 2 additions & 2 deletions test/node.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tap').test
const MTProto = require('../lib/index')
const { MTProto } = require('../lib')

const phone = {
num : '+9996620000',
Expand All @@ -16,7 +16,7 @@ const api = {
}
const server = {
dev : true,
webogram: true
webogram: false
}

const config = {
Expand Down

0 comments on commit 7951d03

Please sign in to comment.