forked from TeamUltroid/Ultroid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vcstarter.py
177 lines (160 loc) · 5.97 KB
/
vcstarter.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import json
import os
from json.decoder import JSONDecodeError
from aiohttp import web
from aiohttp.http_websocket import WSMsgType
from pyUltroid import Var, vcbot, udB
from telethon import TelegramClient
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.functions.phone import (
GetGroupCallRequest,
JoinGroupCallRequest,
LeaveGroupCallRequest,
)
from telethon.tl.types import DataJSON
LOG_CHANNEL = int(udB.get("LOG_CHANNEL"))
if vcbot is not None:
bot = TelegramClient("ultroid_vc", Var.API_ID, Var.API_HASH).start(bot_token=udB.get("BOT_TOKEN"))
async def get_entity(chat):
try:
return await vcbot.get_input_entity(chat["id"])
except ValueError:
if "username" in chat:
return await vcbot.get_entity(chat["username"])
raise
async def join_call(data):
try:
chat = await get_entity(data["chat"])
except ValueError:
name = (await vcbot.get_me()).first_name
return await bot.send_message(
data["chat"]["id"], f"Please add `{name}`` in this group."
)
except Exception as ex:
return await bot.send_message(data["chat"]["id"], "`" + str(ex) + "`")
try:
full_chat = await vcbot(GetFullChannelRequest(chat))
except ValueError:
stree = (await vcbot.get_me()).first_name
return await bot.send_message(
data["chat"]["id"], f"`Please add {stree} in this group.`"
)
except Exception as ex:
return await bot.send_message(data["chat"]["id"], "`" + str(ex) + "`")
try:
call = await vcbot(GetGroupCallRequest(full_chat.full_chat.call))
except BaseException:
call = None
if not call:
return await bot.send_message(
data["chat"]["id"],
"`I can't access voice chat.`",
)
try:
result = await vcbot(
JoinGroupCallRequest(
call=call.call,
muted=False,
join_as="me",
params=DataJSON(
data=json.dumps(
{
"ufrag": data["ufrag"],
"pwd": data["pwd"],
"fingerprints": [
{
"hash": data["hash"],
"setup": data["setup"],
"fingerprint": data["fingerprint"],
},
],
"ssrc": data["source"],
},
),
),
),
)
await bot.send_message(
LOG_CHANNEL,
f"`Joined Voice Chat in {(await bot.get_entity(data['chat']['id'])).title}`",
)
except Exception as ex:
return await bot.send_message(data["chat"]["id"], "`" + str(ex) + "`")
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 leave_call(data):
try:
full_chat = await vcbot(
GetFullChannelRequest(
data["chat"]["id"],
)
)
except Exception as ex:
return await bot.send_message(
data["chat"]["id"],
"Exception in GetFullChannelRequest ```" + str(ex) + "```",
)
try:
call = full_chat.full_chat.call
except BaseException:
call = None
try:
await vcbot(
LeaveGroupCallRequest(
call=call,
source=data["source"],
),
)
await bot.send_message(
LOG_CHANNEL,
f"Left Voice Chat in `{data['chat']['title']}`",
)
except Exception as ex:
return await bot.send_message(
data["chat"]["id"],
"Exception in LeaveGroupCallRequest: `" + str(ex) + "`",
)
return {"_": "left_vc", "data": {"chat_id": data["chat"]["id"]}}
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 data["_"] == "leave":
await bot.send_message(
LOG_CHANNEL,
f"Received **Leave Request** In `{data['data']['chat']['title']}`",
)
await bot.send_message(
data["data"]["chat"]["id"],
f"Received **Leave Request** In `{data['data']['chat']['title']}`",
)
response = await leave_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=os.environ.get("PORT", 6969))
vcbot.start()
main()