diff --git a/README.md b/README.md index 42a3009f440..11fb101c905 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ ![GitHub commit activity](https://img.shields.io/github/commit-activity/m/breakdowns/slam-mirrorbot) [![Slam Mirror Support](https://img.shields.io/badge/slam%20mirror%20bot-support%20group-blue)](https://t.me/SlamMirrorSupport) -**Slam Mirror Bot** is a Telegram Bot writen in Python for mirroring files on the Internet to our beloved Google Drive. +**Slam Mirror Bot** is a multipurpose Telegram Bot writen in Python for mirroring files on the Internet to our beloved Google Drive. # Features supported: @@ -26,7 +26,7 @@ - Multiple Trackers support - Check Heroku dynos stats - Custom image support -- Counting file/folders +- Counting file/folder - Racaty.net support - Shell and Executor - Stickers module diff --git a/bot/__init__.py b/bot/__init__.py index 3e5d3b12e2b..8ea0eebba16 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -90,10 +90,10 @@ def mktable(): try: BOT_TOKEN = getConfig('BOT_TOKEN') - DB_URI = os.environ.get("DATABASE_URL") + DB_URI = getConfig('DATABASE_URL') parent_id = getConfig('GDRIVE_FOLDER_ID') DOWNLOAD_DIR = getConfig('DOWNLOAD_DIR') - if not DOWNLOAD_DIR.endswith("/"): + if DOWNLOAD_DIR[-1] != '/' or DOWNLOAD_DIR[-1] != '\\': DOWNLOAD_DIR = DOWNLOAD_DIR + '/' DOWNLOAD_STATUS_UPDATE_INTERVAL = int(getConfig('DOWNLOAD_STATUS_UPDATE_INTERVAL')) OWNER_ID = int(getConfig('OWNER_ID')) @@ -125,7 +125,8 @@ def mktable(): conn.close() LOGGER.info("Generating USER_SESSION_STRING") -app = Client(':memory:', api_id=int(TELEGRAM_API), api_hash=TELEGRAM_HASH, bot_token=BOT_TOKEN) +with Client(':memory:', api_id=int(TELEGRAM_API), api_hash=TELEGRAM_HASH, bot_token=BOT_TOKEN) as app: + USER_SESSION_STRING = app.export_session_string() #Generate Telegraph Token sname = ''.join(random.SystemRandom().choices(string.ascii_letters, k=8)) diff --git a/bot/__main__.py b/bot/__main__.py index ace1d75dd4b..1c863899d9a 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,9 +1,10 @@ import shutil, psutil import signal -import os +import pickle from pyrogram import idle from bot import app +from os import execl, kill, path, remove from sys import executable from datetime import datetime import pytz @@ -11,7 +12,7 @@ from telegram import ParseMode, BotCommand from telegram.ext import CommandHandler, run_async -from bot import bot, dispatcher, updater, botStartTime, IMAGE_URL +from bot import dispatcher, updater, botStartTime, IMAGE_URL from bot.helper.ext_utils import fs_utils from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.message_utils import * @@ -74,11 +75,10 @@ def restart(update, context): restart_message = sendMessage("Restarting, Please wait!", context.bot, update) LOGGER.info(f'Restarting the Bot...') # Save restart message object in order to reply to it after restarting - with open(".restartmsg", "w") as f: - f.truncate(0) - f.write(f"{restart_message.chat.id}\n{restart_message.message_id}\n") fs_utils.clean_all() - os.execl(executable, executable, "-m", "bot") + with open('restart.pickle', 'wb') as status: + pickle.dump(restart_message, status) + execl(executable, executable, "-m", "bot") @run_async @@ -197,29 +197,30 @@ def bot_help(update, context): BotCommand(f'{BotCommands.TarMirrorCommand}','Upload tar (zipped) file'), BotCommand(f'{BotCommands.UnzipMirrorCommand}','Extract files'), BotCommand(f'{BotCommands.CloneCommand}','Copy file/folder to Drive'), -BotCommand(f'{BotCommands.CountCommand}','Count file/folder of Drive Links'), +BotCommand(f'{BotCommands.CountCommand}','Count file/folder of Drive link'), BotCommand(f'{BotCommands.WatchCommand}','Mirror YT-DL support link'), BotCommand(f'{BotCommands.TarWatchCommand}','Mirror Youtube playlist link as tar'), BotCommand(f'{BotCommands.CancelMirror}','Cancel a task'), BotCommand(f'{BotCommands.CancelAllCommand}','Cancel all tasks'), BotCommand(f'{BotCommands.DeleteCommand}','Delete file from Drive'), -BotCommand(f'{BotCommands.ListCommand}',' [query] Searches files in G-Drive'), +BotCommand(f'{BotCommands.ListCommand}',' [query] Searches files in Drive'), BotCommand(f'{BotCommands.StatusCommand}','Get Mirror Status message'), BotCommand(f'{BotCommands.StatsCommand}','Bot Usage Stats'), BotCommand(f'{BotCommands.HelpCommand}','Get Detailed Help'), BotCommand(f'{BotCommands.SpeedCommand}','Check Speed of the host'), -BotCommand(f'{BotCommands.LogCommand}','Bot Log [owner only]'), -BotCommand(f'{BotCommands.RestartCommand}','Restart bot [owner only]')] +BotCommand(f'{BotCommands.LogCommand}','Bot Log [owner/sudo only]'), +BotCommand(f'{BotCommands.RestartCommand}','Restart bot [owner/sudo only]')] def main(): fs_utils.start_cleanup() # Check if the bot is restarting - if os.path.isfile(".restartmsg"): - with open(".restartmsg") as f: - chat_id, msg_id = map(int, f) - bot.edit_message_text("Restarted successfully!", chat_id, msg_id) - os.remove(".restartmsg") + if path.exists('restart.pickle'): + with open('restart.pickle', 'rb') as status: + restart_message = pickle.load(status) + restart_message.edit_text("Restarted Successfully!") + LOGGER.info('Restarted Successfully!') + remove('restart.pickle') bot.set_my_commands(botcmds) diff --git a/bot/helper/mirror_utils/download_utils/telegram_downloader.py b/bot/helper/mirror_utils/download_utils/telegram_downloader.py index ad0ab71e123..709cf8400b8 100644 --- a/bot/helper/mirror_utils/download_utils/telegram_downloader.py +++ b/bot/helper/mirror_utils/download_utils/telegram_downloader.py @@ -2,7 +2,10 @@ import threading import time -from bot import LOGGER, download_dict, download_dict_lock, app +from pyrogram import Client + +from bot import LOGGER, download_dict, download_dict_lock, TELEGRAM_API, \ + TELEGRAM_HASH, USER_SESSION_STRING from .download_helper import DownloadHelper from ..status_utils.telegram_download_status import TelegramDownloadStatus @@ -20,7 +23,10 @@ def __init__(self, listener): self.__name = "" self.__gid = '' self.__start_time = time.time() - self._bot = app + self._bot = Client(api_id=TELEGRAM_API, + api_hash=TELEGRAM_HASH, + session_name=USER_SESSION_STRING) + self._bot.start() self.__is_cancelled = False @property