Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
to report changes in users and some early commands
- Loading branch information
Showing
10 changed files
with
229 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,6 @@ def get_current(cls, | |
) | ||
|
||
payload = get_admin_payload(token) | ||
print(payload) | ||
if not payload: | ||
raise exc | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import glob | ||
import importlib.util | ||
from os.path import basename, dirname, join | ||
from threading import Thread | ||
from config import TELEGRAM_API_TOKEN, TELEGRAM_PROXY_URL | ||
from app import app | ||
from telebot import TeleBot, apihelper | ||
|
||
|
||
bot = None | ||
if TELEGRAM_API_TOKEN: | ||
apihelper.proxy = {'http': TELEGRAM_PROXY_URL, 'https': TELEGRAM_PROXY_URL} | ||
bot = TeleBot(TELEGRAM_API_TOKEN) | ||
|
||
|
||
@app.on_event("startup") | ||
def start_bot(): | ||
if bot: | ||
handler = glob.glob(join(dirname(__file__), "*.py")) | ||
for file in handler: | ||
name = basename(file).replace('.py', '') | ||
if name.startswith('_'): | ||
continue | ||
spec = importlib.util.spec_from_file_location(name, file) | ||
spec.loader.exec_module(importlib.util.module_from_spec(spec)) | ||
|
||
thread = Thread(target=bot.infinity_polling, daemon=True) | ||
thread.start() | ||
|
||
|
||
from .report import ( # noqa | ||
report, | ||
report_new_user, | ||
report_user_modification, | ||
report_user_deletion, | ||
report_status_change | ||
) | ||
|
||
__all__ = [ | ||
"bot", | ||
"report", | ||
"report_new_user", | ||
"report_user_modification", | ||
"report_user_deletion", | ||
"report_status_change" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from app import xray | ||
import math | ||
from app.db import GetDB, crud | ||
from app.models.user import UserStatus | ||
from app.telegram import bot | ||
from app.utils.system import cpu_usage, memory_usage | ||
from config import TELEGRAM_ADMIN_ID | ||
from telebot.custom_filters import ChatFilter | ||
|
||
bot.add_custom_filter(ChatFilter()) | ||
|
||
|
||
commands_text = """🚀 Marzban's bot commands: | ||
/system | ||
_\- Get system info_ | ||
/restart | ||
_\- Restart Xray core_ | ||
""" | ||
|
||
system_text = """⚙️ System statistics | ||
*CPU Cores*: `{cpu_cores}` | ||
*CPU Usage*: `{cpu_percent}%` | ||
*Total Memory*: `{total_memory}` | ||
*In Use Memory*: `{used_memory}` | ||
*Free Memory*: `{free_memory}` | ||
*Total Bandwidth Usage*: `{total_bandwidth}` | ||
*Upload Bandwidth Usage*: `{up_bandwidth}` | ||
*Download Bandwidth Usage*: `{down_bandwidth}` | ||
*Total Users*: `{total_users}` | ||
*Active Users*: `{active_users}` | ||
*Deactive Users*: `{deactive_users}` | ||
""" | ||
|
||
|
||
def readable_size(size_bytes): | ||
if size_bytes == 0: | ||
return "0B" | ||
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | ||
i = int(math.floor(math.log(size_bytes, 1024))) | ||
p = math.pow(1024, i) | ||
s = round(size_bytes / p, 2) | ||
return f'{s} {size_name[i]}' | ||
|
||
|
||
@bot.message_handler(commands=['start', 'help'], chat_id=[TELEGRAM_ADMIN_ID]) | ||
def help_command(message): | ||
return bot.reply_to(message, commands_text, parse_mode="MarkdownV2") | ||
|
||
|
||
@bot.message_handler(commands=['system'], chat_id=[TELEGRAM_ADMIN_ID]) | ||
def system_command(message): | ||
mem = memory_usage() | ||
cpu = cpu_usage() | ||
with GetDB() as db: | ||
bandwidth = crud.get_system_usage(db) | ||
total_users = crud.get_users_count(db) | ||
users_active = crud.get_users_count(db, UserStatus.active) | ||
|
||
text = system_text.format(cpu_cores=cpu.cores, | ||
cpu_percent=cpu.percent, | ||
total_memory=readable_size(mem.total), | ||
used_memory=readable_size(mem.used), | ||
free_memory=readable_size(mem.free), | ||
total_bandwidth=readable_size(bandwidth.uplink + bandwidth.downlink), | ||
up_bandwidth=readable_size(bandwidth.uplink), | ||
down_bandwidth=readable_size(bandwidth.downlink), | ||
total_users=total_users, | ||
active_users=users_active, | ||
deactive_users=total_users - users_active) | ||
|
||
return bot.reply_to(message, text, parse_mode='MarkdownV2') | ||
|
||
|
||
@bot.message_handler(commands=['restart'], chat_id=[TELEGRAM_ADMIN_ID]) | ||
def restart_command(message): | ||
m = bot.reply_to(message, '🔄 Restarting...') | ||
xray.core.restart() | ||
bot.edit_message_text('✅ Xray core restarted.', m.chat.id, m.message_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from app import logger | ||
from app.telegram import bot | ||
from telebot.apihelper import ApiTelegramException | ||
from config import TELEGRAM_ADMIN_ID | ||
from telebot.formatting import escape_markdown | ||
|
||
|
||
def report(message: str, parse_mode="MarkdownV2"): | ||
if bot and TELEGRAM_ADMIN_ID: | ||
try: | ||
bot.send_message(TELEGRAM_ADMIN_ID, message, parse_mode=parse_mode) | ||
except ApiTelegramException as e: | ||
logger.error(e) | ||
|
||
|
||
def report_new_user(username: str, by: str): | ||
return report(f"New user *{escape_markdown(username)}* added by *{escape_markdown(by)}*") | ||
|
||
|
||
def report_user_modification(username: str, by: str): | ||
return report(f"User *{escape_markdown(username)}* modified by *{escape_markdown(by)}*") | ||
|
||
|
||
def report_user_deletion(username: str, by: str): | ||
return report(f"User *{escape_markdown(username)}* deleted by *{escape_markdown(by)}*") | ||
|
||
|
||
def report_status_change(username: str, status: str): | ||
return report(f"User *{escape_markdown(username)}*'s status has changed to _{status}_") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters