This repository was archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathmain.py
106 lines (87 loc) · 2.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import json
import os
from json.decoder import JSONDecodeError
from aiohttp import web
from aiohttp.http_websocket import WSMsgType
from dotenv import load_dotenv
from telethon import TelegramClient
from telethon.sessions import StringSession
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.functions.phone import GetGroupCallRequest
from telethon.tl.functions.phone import JoinGroupCallRequest
from telethon.tl.types import DataJSON
load_dotenv()
SESSION = os.getenv("SESSION")
API_ID = int(os.getenv("API_ID", 12345))
API_HASH = os.getenv("API_HASH")
PORT = int(os.getenv("PORT", 5000))
client = TelegramClient(
StringSession(SESSION),
API_ID,
API_HASH
)
client.start()
async def get_entity(chat):
try:
return await client.get_input_entity(chat['id'])
except ValueError:
if 'username' in chat:
return await client.get_entity(chat['username'])
raise
async def join_call(data):
chat = await get_entity(data['chat'])
full_chat = await client(GetFullChannelRequest(chat))
call = await client(GetGroupCallRequest(full_chat.full_chat.call))
result = await client(
JoinGroupCallRequest(
call=call.call,
muted=False,
params=DataJSON(
data=json.dumps({
'ufrag': data['ufrag'],
'pwd': data['pwd'],
'fingerprints': [{
'hash': data['hash'],
'setup': data['setup'],
'fingerprint': data['fingerprint'],
}],
'ssrc': data['source'],
}),
),
),
)
transport = json.loads(result.updates[0].call.params.data)['transport']
return {
'_': 'get_join',
'data': {
'chat_id': data['chat']['id'],
'transport': {
'ufrag': transport['ufrag'],
'pwd': transport['pwd'],
'fingerprints': transport['fingerprints'],
'candidates': transport['candidates'],
},
},
}
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == WSMsgType.TEXT:
try:
data = json.loads(msg.data)
except JSONDecodeError:
await ws.close()
break
response = None
if data['_'] == 'join':
response = await join_call(data['data'])
if response is not None:
await ws.send_json(response)
return ws
def main():
app = web.Application()
app.router.add_route('GET', '/', websocket_handler)
web.run_app(app, port=PORT)
if __name__ == '__main__':
main()