-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (84 loc) · 2.63 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
import json
import os
from typing import Optional
import telethon
from telethon import TelegramClient, events
from telethon.tl.functions.channels import (
CreateChannelRequest,
InviteToChannelRequest,
EditAdminRequest,
)
from telethon.tl.types import ChatAdminRights
API_ID = os.environ.get("API_ID", 0)
API_HASH = os.environ.get("API_HASH", "")
ALLOWED_CHATS = [int(c) for c in os.environ.get("ALLOWED_CHATS", "").split("|")]
client = TelegramClient("telegram", API_ID, API_HASH)
def decode_packet(text: str) -> Optional[dict]:
try:
return json.loads(text)
except json.decoder.JSONDecodeError:
return None
async def create_group(packet: dict) -> dict:
group = packet.get("group", {})
title = group.get("title", None)
description = group.get("description", None)
bot = group.get("bot_username", None)
assert title is not None
assert description is not None
assert bot is not None
mtbot = await client.get_input_entity(bot)
response = await client(CreateChannelRequest(
title=title,
about=description,
broadcast=False,
megagroup=True,
))
if len(response.chats) < 1:
return {"ok": False}
mtgroup = response.chats[0]
await client(InviteToChannelRequest(
channel=mtgroup,
users=[mtbot, ]
))
await client(EditAdminRequest(
channel=mtgroup,
user_id=mtbot.user_id,
admin_rights=ChatAdminRights(
change_info=True,
delete_messages=True,
ban_users=True,
invite_users=True,
pin_messages=True,
add_admins=True,
manage_call=True,
other=True,
),
rank="Bot",
))
return {
"ok": True,
"group_id": int(f"-100{mtgroup.id}"),
}
@client.on(events.NewMessage)
async def new_message(event):
message = event.message
if not message or message.out or not message.raw_text:
return
chat_id = event.chat_id
if chat_id not in ALLOWED_CHATS:
return
packet = decode_packet(message.raw_text)
if type(packet) != dict:
return
command = packet.get("command")
res = {"ok": False, "error": "Unrecognized command"}
if command == "create_group":
try:
res = await create_group(packet)
except (telethon.errors.FloodError, telethon.errors.FloodWaitError):
res = {"ok": False, "error": "FLOOD_WAIT"}
except telethon.errors.RPCError as e:
res = {"ok": False, "error": "RPC_ERROR", "message": e.message}
await event.reply(json.dumps(res))
client.start()
client.run_until_disconnected()