diff --git a/.gitignore b/.gitignore index 88bbaa475..e567dcdf4 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,4 @@ dmypy.json #configfile config.ini +config.env \ No newline at end of file diff --git a/Userge/__init__.py b/Userge/__init__.py index 684696fd1..3bb4d0ac9 100644 --- a/Userge/__init__.py +++ b/Userge/__init__.py @@ -1,11 +1,8 @@ -import logging +from .utils import logging +log = logging.getLogger(__name__) -logging.basicConfig( - level=logging.INFO, - format='[%(asctime)s - %(levelname)s] - %(name)s - %(message)s', - datefmt='%d-%b-%y %H:%M:%S' -) -log = logging.getLogger(__name__) -userge = None # userge is the client name +from .client import Userge + +userge = Userge() # userge is the client name diff --git a/Userge/__main__.py b/Userge/__main__.py index e69de29bb..04b2e2a54 100644 --- a/Userge/__main__.py +++ b/Userge/__main__.py @@ -0,0 +1,7 @@ +from Userge import userge, logging + +log = logging.getLogger(__name__) + + +if __name__ == "__main__": + userge.run() \ No newline at end of file diff --git a/Userge/client.py b/Userge/client.py index e69de29bb..cb8b3a3b0 100644 --- a/Userge/client.py +++ b/Userge/client.py @@ -0,0 +1,18 @@ +from .utils import Config, logging + +log = logging.getLogger(__name__) + + +from pyrogram import Client + +logging.getLogger("pyrogram").setLevel(logging.WARNING) + + +class Userge(Client): + def __init__(self): + super().__init__( + Config.HU_STRING_SESSION, + api_id=Config.API_ID, + api_hash=Config.API_HASH, + plugins = dict(root="Userge/plugins") + ) \ No newline at end of file diff --git a/Userge/plugins/__init__.py b/Userge/plugins/__init__.py index aea5c959d..e19d0a2ff 100644 --- a/Userge/plugins/__init__.py +++ b/Userge/plugins/__init__.py @@ -1,16 +1,19 @@ -from Userge import log -from os.path import dirname, basename, isfile -import glob +from Userge import logging +log = logging.getLogger(__name__) -plugins = sorted( - [ - basename(f)[:-3] for f in glob.glob(dirname(__file__) + "/*.py") - if isfile(f) and f.endswith(".py") and not f.endswith("__init__.py") - ] -) +async def get_all_plugins(): + from os.path import dirname, basename, isfile + import glob -log.info(f"plugins to load: {plugins}") + plugins = sorted( + [ + basename(f)[:-3] for f in glob.glob(dirname(__file__) + "/*.py") + if isfile(f) and f.endswith(".py") and not f.endswith("__init__.py") + ] + ) -__all__ = plugins + ["plugins"] \ No newline at end of file + log.info(f"all plugins: {plugins}") + + return plugins \ No newline at end of file diff --git a/Userge/plugins/ping.py b/Userge/plugins/ping.py index eac512da6..4386c73ea 100644 --- a/Userge/plugins/ping.py +++ b/Userge/plugins/ping.py @@ -1,12 +1,16 @@ -from datetime import datetime -from Userge import userge +from Userge import userge, logging + +log = logging.getLogger(__name__) + + from pyrogram import Filters, Message +from datetime import datetime @userge.on_message(Filters.command("ping", ".") & Filters.me) -def pingme(_, message: Message): +async def pingme(_, message: Message): start = datetime.now() - message.edit('`Pong!`') + await message.edit('`Pong!`') end = datetime.now() ms = (end - start).microseconds / 1000 - message.edit(f"**Pong!**\n`{ms} ms`") + await message.edit(f"**Pong!**\n`{ms} ms`") diff --git a/Userge/utils/__init__.py b/Userge/utils/__init__.py index e69de29bb..c3e41229c 100644 --- a/Userge/utils/__init__.py +++ b/Userge/utils/__init__.py @@ -0,0 +1,2 @@ +from .config import Config +from .logger import logging \ No newline at end of file diff --git a/Userge/utils/config.py b/Userge/utils/config.py index e69de29bb..73663b931 100644 --- a/Userge/utils/config.py +++ b/Userge/utils/config.py @@ -0,0 +1,28 @@ +import os +from dotenv import load_dotenv +from .logger import logging + +log = logging.getLogger(__name__) + +config_file = "config.env" + +if not os.path.isfile(config_file): + log.error(f"{config_file} Not Found!") + quit(1) + +load_dotenv(config_file) + +if os.environ.get("_____REMOVE_____THIS_____LINE_____", None): + log.error("Please remove the line mentioned in the first hashtag from the config.env file") + quit(1) + + +class Config: + + API_ID = int(os.environ.get("API_ID", 12345)) + + API_HASH = os.environ.get("API_HASH", None) + + HU_STRING_SESSION = os.environ.get("HU_STRING_SESSION", None) + + DB_URI = os.environ.get("DATABASE_URL", None) \ No newline at end of file diff --git a/Userge/utils/logger.py b/Userge/utils/logger.py new file mode 100644 index 000000000..716bfc07b --- /dev/null +++ b/Userge/utils/logger.py @@ -0,0 +1,7 @@ +import logging + +logging.basicConfig( + level=logging.INFO, + format='[%(asctime)s - %(levelname)s] - %(name)s - %(message)s', + datefmt='%d-%b-%y %H:%M:%S' +) \ No newline at end of file diff --git a/config.env.sample b/config.env.sample new file mode 100644 index 000000000..2cf798c00 --- /dev/null +++ b/config.env.sample @@ -0,0 +1,13 @@ +# Remove this line first before doing anything else +_____REMOVE_____THIS_____LINE_____=True + +# +# Get from https://my.telegram.org/ +# +API_ID="" +API_HASH="" + +# +# Get by running `bash genStr` command +# +HU_STRING_SESSION="" \ No newline at end of file diff --git a/config.ini.sample b/config.ini.sample deleted file mode 100644 index 762a24f90..000000000 --- a/config.ini.sample +++ /dev/null @@ -1,3 +0,0 @@ -[pyrogram] -api_id = -api_hash = \ No newline at end of file diff --git a/genStr b/genStr new file mode 100644 index 000000000..75f8e95a2 --- /dev/null +++ b/genStr @@ -0,0 +1,3 @@ +#!/bin/bash + +python3.8 genStrSession.py \ No newline at end of file diff --git a/genStrSession.py b/genStrSession.py new file mode 100644 index 000000000..1bd56bdb4 --- /dev/null +++ b/genStrSession.py @@ -0,0 +1,15 @@ +from pyrogram import Client +import asyncio + + +async def genStrSession(): + async with Client( + "Userge", + api_id=int(input("enter Telegram APP ID: ")), + api_hash=input("enter Telegram API HASH: ") + ) as Userge: + print(Userge.export_session_string()) + + +if __name__ == "__main__": + asyncio.run(genStrSession()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e69de29bb..f3ae3227b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-dotenv +tgcrypto +https://github.com/pyrogram/pyrogram/archive/asyncio.zip \ No newline at end of file diff --git a/run b/run new file mode 100644 index 000000000..0beb3a4a0 --- /dev/null +++ b/run @@ -0,0 +1,3 @@ +#!/bin/bash + +python3.8 -m Userge \ No newline at end of file