2020-09-13 13:50:36 -04:00
|
|
|
import sys
|
|
|
|
from logging import INFO, basicConfig, getLogger
|
2023-05-07 17:19:42 -04:00
|
|
|
from time import time
|
2020-09-13 13:50:36 -04:00
|
|
|
|
2021-02-13 12:06:21 -05:00
|
|
|
import telethon
|
2020-09-17 17:31:47 -04:00
|
|
|
from telethon.errors.rpcerrorlist import (AccessTokenExpiredError,
|
|
|
|
AccessTokenInvalidError,
|
|
|
|
TokenInvalidError)
|
2020-09-13 13:50:36 -04:00
|
|
|
from telethon.network.connection.tcpabridged import \
|
|
|
|
ConnectionTcpAbridged as CTA
|
|
|
|
|
2021-02-13 12:06:21 -05:00
|
|
|
from .custom import ExtendedEvent
|
2020-09-13 13:50:36 -04:00
|
|
|
from .loader import Loader
|
|
|
|
from .settings import Settings
|
|
|
|
|
|
|
|
if sys.version_info.major < 3 or (sys.version_info.major == 3 and sys.version_info.minor < 6):
|
|
|
|
print("This program requires at least Python 3.6.0 to work correctly, exiting.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2023-05-07 17:19:42 -04:00
|
|
|
startup_time = time()
|
|
|
|
|
2020-09-13 13:50:36 -04:00
|
|
|
|
|
|
|
class MicroBot():
|
|
|
|
client = None
|
|
|
|
settings = Settings()
|
|
|
|
logger = None
|
|
|
|
loader = None
|
|
|
|
|
|
|
|
def __init__(self):
|
2021-06-22 10:20:22 -04:00
|
|
|
basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=INFO)
|
|
|
|
self.logger = getLogger(__name__)
|
2020-09-13 13:50:36 -04:00
|
|
|
self.start_client()
|
2021-06-22 10:20:22 -04:00
|
|
|
self.loader = Loader(self.client, self.logger, self.settings)
|
2020-09-13 13:50:36 -04:00
|
|
|
|
2023-05-07 17:08:49 -04:00
|
|
|
async def run_until_done(self):
|
2020-09-13 13:50:36 -04:00
|
|
|
self.loader.load_all_modules()
|
2023-05-29 10:53:04 -04:00
|
|
|
self.logger.info("Bot successfully started.")
|
2023-05-07 17:08:49 -04:00
|
|
|
await self.client.run_until_disconnected()
|
2023-05-29 10:53:04 -04:00
|
|
|
await self.stop_client(reason="Bot disconnected.")
|
2020-09-13 13:50:36 -04:00
|
|
|
|
|
|
|
def _check_config(self):
|
|
|
|
api_key = self.settings.get_config("api_key")
|
|
|
|
api_hash = self.settings.get_config("api_hash")
|
2020-09-17 17:31:47 -04:00
|
|
|
bot_token = self.settings.get_config("bot_token")
|
2020-09-13 13:50:36 -04:00
|
|
|
|
|
|
|
while not api_key:
|
|
|
|
api_key = input("Enter your API key: ")
|
|
|
|
|
|
|
|
self.settings.set_config("api_key", api_key)
|
|
|
|
|
|
|
|
while not api_hash:
|
|
|
|
api_hash = input("Enter your API hash: ")
|
|
|
|
|
|
|
|
self.settings.set_config("api_hash", api_hash)
|
|
|
|
|
2020-09-17 17:31:47 -04:00
|
|
|
while not bot_token:
|
|
|
|
bot_token = input("Enter your bot token: ")
|
2020-09-13 13:50:36 -04:00
|
|
|
|
2020-09-17 17:31:47 -04:00
|
|
|
self.settings.set_config("bot_token", bot_token)
|
|
|
|
|
|
|
|
return api_key, api_hash, bot_token
|
2020-09-13 13:50:36 -04:00
|
|
|
|
|
|
|
def start_client(self):
|
2020-09-17 17:31:47 -04:00
|
|
|
api_key, api_hash, bot_token = self._check_config()
|
2020-09-13 13:50:36 -04:00
|
|
|
|
|
|
|
try:
|
2021-06-22 10:20:22 -04:00
|
|
|
self.client = telethon.TelegramClient(self.settings.get_config("session_name", "bot0"), api_key, api_hash, connection=CTA).start(bot_token=bot_token)
|
2020-09-17 17:31:47 -04:00
|
|
|
except (TokenInvalidError, AccessTokenExpiredError, AccessTokenInvalidError):
|
|
|
|
self.logger.error("The bot token provided is invalid, exiting.")
|
2023-05-07 17:08:49 -04:00
|
|
|
sys.exit(1)
|
2020-09-13 13:50:36 -04:00
|
|
|
|
2023-05-07 17:08:49 -04:00
|
|
|
async def stop_client(self, reason=None, exit_code=None):
|
2020-09-13 13:50:36 -04:00
|
|
|
if reason:
|
2023-05-07 17:08:49 -04:00
|
|
|
self.logger.info("Stopping bot for reason: %s", reason)
|
2020-09-13 13:50:36 -04:00
|
|
|
else:
|
2023-05-07 17:08:49 -04:00
|
|
|
self.logger.info("Stopping bot.")
|
2020-09-13 13:50:36 -04:00
|
|
|
|
|
|
|
await self.loader.aioclient.close()
|
2023-05-07 17:08:49 -04:00
|
|
|
|
|
|
|
if exit_code is not None:
|
|
|
|
self.logger.info("Exiting with exit code: %i", exit_code)
|
|
|
|
sys.exit(exit_code)
|
|
|
|
else:
|
|
|
|
sys.exit(0)
|
2020-09-13 13:50:36 -04:00
|
|
|
|
|
|
|
|
2021-02-13 12:06:21 -05:00
|
|
|
telethon.events.NewMessage.Event = ExtendedEvent
|
|
|
|
|
2020-09-13 13:50:36 -04:00
|
|
|
micro_bot = MicroBot()
|
|
|
|
ldr = micro_bot.loader
|
2020-10-03 13:52:25 -04:00
|
|
|
client = micro_bot.client
|
|
|
|
logger = micro_bot.logger
|
2020-09-13 13:50:36 -04:00
|
|
|
|
2023-05-07 17:08:49 -04:00
|
|
|
client.loop.run_until_complete(micro_bot.run_until_done())
|