Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/core/telegram_manager.py
1628 views
1
from pyrogram import Client, enums
2
from pyrogram.types import LinkPreviewOptions
3
from asyncio import Lock
4
5
from .. import LOGGER
6
from .config_manager import Config
7
8
9
class TgClient:
10
_lock = Lock()
11
bot = None
12
user = None
13
NAME = ""
14
ID = 0
15
IS_PREMIUM_USER = False
16
MAX_SPLIT_SIZE = 2097152000
17
18
@classmethod
19
async def start_bot(cls):
20
LOGGER.info("Creating client from BOT_TOKEN")
21
cls.ID = Config.BOT_TOKEN.split(":", 1)[0]
22
cls.bot = Client(
23
cls.ID,
24
Config.TELEGRAM_API,
25
Config.TELEGRAM_HASH,
26
proxy=Config.TG_PROXY,
27
bot_token=Config.BOT_TOKEN,
28
workdir="/app",
29
parse_mode=enums.ParseMode.HTML,
30
max_concurrent_transmissions=10,
31
max_message_cache_size=15000,
32
max_topic_cache_size=15000,
33
sleep_threshold=0,
34
link_preview_options=LinkPreviewOptions(is_disabled=True),
35
)
36
await cls.bot.start()
37
cls.NAME = cls.bot.me.username
38
39
@classmethod
40
async def start_user(cls):
41
if Config.USER_SESSION_STRING:
42
LOGGER.info("Creating client from USER_SESSION_STRING")
43
try:
44
cls.user = Client(
45
"user",
46
Config.TELEGRAM_API,
47
Config.TELEGRAM_HASH,
48
proxy=Config.TG_PROXY,
49
session_string=Config.USER_SESSION_STRING,
50
workdir="/app",
51
parse_mode=enums.ParseMode.HTML,
52
sleep_threshold=60,
53
max_concurrent_transmissions=10,
54
max_message_cache_size=15000,
55
max_topic_cache_size=15000,
56
link_preview_options=LinkPreviewOptions(is_disabled=True),
57
)
58
await cls.user.start()
59
cls.IS_PREMIUM_USER = cls.user.me.is_premium
60
if cls.IS_PREMIUM_USER:
61
cls.MAX_SPLIT_SIZE = 4194304000
62
except Exception as e:
63
LOGGER.error(f"Failed to start client from USER_SESSION_STRING. {e}")
64
cls.IS_PREMIUM_USER = False
65
cls.user = None
66
67
@classmethod
68
async def stop(cls):
69
async with cls._lock:
70
if cls.bot:
71
await cls.bot.stop()
72
if cls.user:
73
await cls.user.stop()
74
LOGGER.info("Client(s) stopped")
75
76
@classmethod
77
async def reload(cls):
78
async with cls._lock:
79
await cls.bot.restart()
80
if cls.user:
81
await cls.user.restart()
82
LOGGER.info("Client(s) restarted")
83
84