https://github.com/zapix/mtpylon
Library to build backend with MTProto's protocol
https://github.com/zapix/mtpylon
Last synced: 6 months ago
JSON representation
Library to build backend with MTProto's protocol
- Host: GitHub
- URL: https://github.com/zapix/mtpylon
- Owner: Zapix
- License: mit
- Created: 2020-10-22T05:34:36.000Z (almost 5 years ago)
- Default Branch: dev
- Last Pushed: 2021-08-05T20:10:38.000Z (about 4 years ago)
- Last Synced: 2024-11-06T06:43:30.428Z (11 months ago)
- Language: Python
- Size: 688 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 24
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mtpylon

[](https://codecov.io/gh/Zapix/mtpylon)Library to build backend with MTProto's protocol
## Installation
```shell
pip install mtpylon
```## Getting started
1. Generate rsa keys:
**rsa_keys.py:**
```python
from typing import List
import rsa # type: ignore
from mtpylon.crypto import KeyPair # type: ignoredef get_rsa_keys(count: int = 2) -> List[KeyPair]:
rsa_list = [
rsa.newkeys(nbits=2048)
for _ in range(count)
]return [
KeyPair(
public=public,
private=private
) for (public, private) in rsa_list
]
```2. Declare schema for mtpylon
**schema.py:**
```python
import random
from dataclasses import dataclassfrom aiohttp import web
from mtpylon import Schema
@dataclass
class Reply:
rand_id: int
content: strclass Meta:
name = 'reply'
order = ('rand_id', 'content')async def echo(request: web.Request, content: str) -> Reply:
return Reply(
rand_id=random.randint(1, 100),
content=content
)schema = Schema(constructors=[Reply], functions=[echo])
```
3. Configure aiohttp with mtpylon
**web.py:**
```python
import sys
import loggingfrom aiohttp import web
import aiohttp_corsfrom mtpylon.configuration import configure_app
from schema import schema as app_schema
from rsa_keys import get_rsa_keys# create console handler and set level to debug
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(level=logging.DEBUG)# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to ch
ch.setFormatter(formatter)logging.basicConfig(level=logging.DEBUG)
if __name__ == '__main__':
app = web.Application()
configure_app(
app,
app_schema,
{
'rsa_manager': {
'params': {
'rsa_keys': get_rsa_keys()
}
},
'pub_keys_path': '/pub-keys',
'schema_path': '/schema',
}
)cors = aiohttp_cors.setup(
app,
defaults={
'*': aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
}
)for route in list(app.router.routes()):
cors.add(route)web.run_app(app, port=8081)
```
4. Start it!
```shell
python web.py
```5. to work with backend please try https://github.com/Zapix/zagram
## Documentation
For more information visit:
https://mtpylon.readthedocs.io/en/latest/
## Example:
Echo server: https://github.com/Zapix/echo-server