diff --git a/TelegramBot/__init__.py b/TelegramBot/__init__.py new file mode 100644 index 0000000..acce3fc --- /dev/null +++ b/TelegramBot/__init__.py @@ -0,0 +1,84 @@ +import sys, os, shutil +import time +from asyncio import get_event_loop, new_event_loop, set_event_loop +import uvloop +import httpx + +from pyrogram import Client +from TelegramBot import config +from TelegramBot.database.MongoDb import check_mongo_uri +from TelegramBot.database.sqdb import delete_old_entries_by_hours +from TelegramBot.logging import LOGGER +from apscheduler.schedulers.asyncio import AsyncIOScheduler +from TelegramBot.helpers.odesli.Odesli import Odesli +import spotipy +from spotipy.oauth2 import SpotifyClientCredentials + +uvloop.install() +LOGGER(__name__).info("Starting TelegramBot....") +BotStartTime = time.time() + + +if sys.version_info[0] < 3 or sys.version_info[1] < 7: + LOGGER(__name__).critical(""" +============================================================= +You MUST need to be on python 3.7 or above, shutting down the bot... +============================================================= +""") + sys.exit(1) + + +LOGGER(__name__).info("setting up event loop....") +try: + loop = get_event_loop() +except RuntimeError: + set_event_loop(new_event_loop()) + loop = get_event_loop() + + +LOGGER(__name__).info( + r""" +____________________________________________________________________ +| _______ _ ____ _ | +| |__ __| | | | _ \ | | | +| | | ___| | ___ __ _ _ __ __ _ _ __ ___ | |_) | ___ | |_ | +| | |/ _ \ |/ _ \/ _` | '__/ _` | '_ ` _ \| _ < / _ \| __| | +| | | __/ | __/ (_| | | | (_| | | | | | | |_) | (_) | |_ | +| |_|\___|_|\___|\__, |_| \__,_|_| |_| |_|____/ \___/ \__| | +| __/ | | +|__________________________________________________________________| +""") +# https://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20 + + +LOGGER(__name__).info("initiating the client....") +LOGGER(__name__).info("checking MongoDb URI....") +loop.run_until_complete(check_mongo_uri(config.MONGO_URI)) + + +if not os.path.exists("resource"): + os.mkdir("resource") + +async def clear(): + await delete_old_entries_by_hours("mytable", 12) + shutil.rmtree("resource") + os.mkdir("resource") + +scheduler = AsyncIOScheduler() +scheduler.add_job(clear, "interval", minutes=1440) +scheduler.start() + +# intitatioing odesli, sporipy +httpx_client= httpx.AsyncClient(follow_redirects=True) +odesli= Odesli() +spotify= spotipy.Spotify(requests_session=True, client_credentials_manager=SpotifyClientCredentials(client_id=config.CLIENT_ID, client_secret=config.CLIENT_SECRET)) +LOGGER(__name__).info("initiated the Odesli & Spotipy client....") + +# https://docs.pyrogram.org/topics/smart-plugins +plugins = dict(root="TelegramBot/plugins") +bot = Client( + "TelegramBot", + api_id=config.API_ID, + api_hash=config.API_HASH, + bot_token=config.BOT_TOKEN, + plugins=plugins) diff --git a/TelegramBot/__main__.py b/TelegramBot/__main__.py new file mode 100644 index 0000000..3a08c8d --- /dev/null +++ b/TelegramBot/__main__.py @@ -0,0 +1,6 @@ +from TelegramBot import bot +from TelegramBot.logging import LOGGER + +LOGGER(__name__).info("client successfully initiated....") +if __name__ == "__main__": + bot.run() diff --git a/TelegramBot/config.py b/TelegramBot/config.py new file mode 100644 index 0000000..584d74e --- /dev/null +++ b/TelegramBot/config.py @@ -0,0 +1,19 @@ +import json +from os import getenv +from dotenv import load_dotenv + +load_dotenv("config.env") + +API_ID = int(getenv("API_ID")) +API_HASH = getenv("API_HASH") +BOT_TOKEN = getenv("BOT_TOKEN") + +OWNER_USERID = json.loads(getenv("OWNER_USERID")) +SUDO_USERID = OWNER_USERID +try:SUDO_USERID += json.loads(getenv("SUDO_USERID")) +except:pass +SUDO_USERID = list(set(SUDO_USERID)) + +MONGO_URI = getenv("MONGO_URI") +CLIENT_ID = getenv("CLIENT_ID") +CLIENT_SECRET = getenv("CLIENT_SECRET") diff --git a/TelegramBot/logging.py b/TelegramBot/logging.py new file mode 100644 index 0000000..a8b8ec4 --- /dev/null +++ b/TelegramBot/logging.py @@ -0,0 +1,20 @@ +import logging +import os +from logging.handlers import RotatingFileHandler + +# removing old logs file if they exist. +try: os.remove("logs.txt") +except: pass + +logging.basicConfig( + level=logging.INFO, + format="[%(asctime)s - %(levelname)s] - %(name)s - %(message)s", + datefmt="%d-%b-%y %H:%M:%S", + handlers=[ + RotatingFileHandler("logs.txt", mode="w+", maxBytes=5000000, backupCount=10), + logging.StreamHandler()]) + +logging.getLogger("pyrogram").setLevel(logging.ERROR) + +def LOGGER(name: str) -> logging.Logger: + return logging.getLogger(name) diff --git a/TelegramBot/version.py b/TelegramBot/version.py new file mode 100644 index 0000000..956d5b7 --- /dev/null +++ b/TelegramBot/version.py @@ -0,0 +1,15 @@ +from sys import version_info +from pyrogram import __version__ as __pyro_version__ + +__major__ = 3 +__minor__ = 9 +__micro__ = 0 + + +def get_version() -> str: + return f"{__major__}.{__minor__}.{__micro__}" + +__python_version__ = f"{version_info[0]}.{version_info[1]}.{version_info[2]}" +__version__ = get_version() +__license__ = "[MIT](https://github.com/sanjit-sinha/TelegramBot-Boilerplate/blob/main/LICENSE)" +__pyrogram_version__ = __pyro_version__