Skip to content

Commit

Permalink
first plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Mar 16, 2020
1 parent c3dff89 commit b7bf37b
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ dmypy.json

#configfile
config.ini
config.env
13 changes: 5 additions & 8 deletions Userge/__init__.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions Userge/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from Userge import userge, logging

log = logging.getLogger(__name__)


if __name__ == "__main__":
userge.run()
18 changes: 18 additions & 0 deletions Userge/client.py
Original file line number Diff line number Diff line change
@@ -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")
)
25 changes: 14 additions & 11 deletions Userge/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
log.info(f"all plugins: {plugins}")

return plugins
14 changes: 9 additions & 5 deletions Userge/plugins/ping.py
Original file line number Diff line number Diff line change
@@ -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`")
2 changes: 2 additions & 0 deletions Userge/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .config import Config
from .logger import logging
28 changes: 28 additions & 0 deletions Userge/utils/config.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions Userge/utils/logger.py
Original file line number Diff line number Diff line change
@@ -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'
)
13 changes: 13 additions & 0 deletions config.env.sample
Original file line number Diff line number Diff line change
@@ -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=""
3 changes: 0 additions & 3 deletions config.ini.sample

This file was deleted.

3 changes: 3 additions & 0 deletions genStr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python3.8 genStrSession.py
15 changes: 15 additions & 0 deletions genStrSession.py
Original file line number Diff line number Diff line change
@@ -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())
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python-dotenv
tgcrypto
https://github.com/pyrogram/pyrogram/archive/asyncio.zip
3 changes: 3 additions & 0 deletions run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python3.8 -m Userge

0 comments on commit b7bf37b

Please sign in to comment.