From a392a037b7951e8685ae99f9ae040c6606abf7b5 Mon Sep 17 00:00:00 2001 From: Hafitz Setya <71178188+breakdowns@users.noreply.github.com> Date: Tue, 1 Jun 2021 12:08:04 +0700 Subject: [PATCH] change back --- README.md | 2 +- bot/__init__.py | 5 ++-- bot/__main__.py | 23 +++++++++---------- .../download_utils/telegram_downloader.py | 10 ++------ config_sample.env | 2 +- 5 files changed, 17 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 11fb101c905..047f99bbef2 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ Fill up rest of the fields. Meaning of each fields are discussed below: - **TELEGRAM_API**: This is to authenticate to your Telegram account for downloading Telegram files. You can get this from https://my.telegram.org DO NOT put this in quotes. - **TELEGRAM_HASH**: This is to authenticate to your Telegram account for downloading Telegram files. You can get this from https://my.telegram.org - **OWNER_ID**: The Telegram user ID (not username) of the Owner of the bot -- **DATABASE_URL**: Your Database URL. See [Generate Database](https://github.com/breakdowns/slam-mirrorbot/tree/master#generate-database) to generate database. (**NOTE**: Leave this empty if You deploying on Heroku with PostgreSQL, for using another database with Heroku just make the name of the var (DATABASE_URL), for vps You must fill database url here) +- **DATABASE_URL**: Your Database URL. See [Generate Database](https://github.com/breakdowns/slam-mirrorbot/tree/master#generate-database) to generate database. (**NOTE**: If you deploying on Heroku, no need to generate database manually, because it will automatic generate database when first deploying) - **GDRIVE_FOLDER_ID**: This is the folder ID of the Google Drive Folder to which you want to upload all the mirrors. - **DOWNLOAD_DIR**: The path to the local folder where the downloads should be downloaded to - **DOWNLOAD_STATUS_UPDATE_INTERVAL**: A short interval of time in seconds after which the Mirror progress message is updated. (I recommend to keep it `5` seconds at least) diff --git a/bot/__init__.py b/bot/__init__.py index 8ea0eebba16..109ced91311 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -93,7 +93,7 @@ def mktable(): DB_URI = getConfig('DATABASE_URL') parent_id = getConfig('GDRIVE_FOLDER_ID') DOWNLOAD_DIR = getConfig('DOWNLOAD_DIR') - if DOWNLOAD_DIR[-1] != '/' or DOWNLOAD_DIR[-1] != '\\': + if not DOWNLOAD_DIR.endswith("/"): DOWNLOAD_DIR = DOWNLOAD_DIR + '/' DOWNLOAD_STATUS_UPDATE_INTERVAL = int(getConfig('DOWNLOAD_STATUS_UPDATE_INTERVAL')) OWNER_ID = int(getConfig('OWNER_ID')) @@ -125,8 +125,7 @@ def mktable(): conn.close() LOGGER.info("Generating USER_SESSION_STRING") -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() +app = Client(':memory:', api_id=int(TELEGRAM_API), api_hash=TELEGRAM_HASH, bot_token=BOT_TOKEN) #Generate Telegraph Token sname = ''.join(random.SystemRandom().choices(string.ascii_letters, k=8)) diff --git a/bot/__main__.py b/bot/__main__.py index 1c863899d9a..f02ba3a6f2c 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,10 +1,9 @@ import shutil, psutil import signal -import pickle +import os 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 @@ -12,7 +11,7 @@ from telegram import ParseMode, BotCommand from telegram.ext import CommandHandler, run_async -from bot import dispatcher, updater, botStartTime, IMAGE_URL +from bot import bot, 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 * @@ -75,10 +74,11 @@ 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() - with open('restart.pickle', 'wb') as status: - pickle.dump(restart_message, status) - execl(executable, executable, "-m", "bot") + os.execl(executable, executable, "-m", "bot") @run_async @@ -215,12 +215,11 @@ def bot_help(update, context): def main(): fs_utils.start_cleanup() # Check if the bot is restarting - 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') + 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") 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 709cf8400b8..ad0ab71e123 100644 --- a/bot/helper/mirror_utils/download_utils/telegram_downloader.py +++ b/bot/helper/mirror_utils/download_utils/telegram_downloader.py @@ -2,10 +2,7 @@ import threading import time -from pyrogram import Client - -from bot import LOGGER, download_dict, download_dict_lock, TELEGRAM_API, \ - TELEGRAM_HASH, USER_SESSION_STRING +from bot import LOGGER, download_dict, download_dict_lock, app from .download_helper import DownloadHelper from ..status_utils.telegram_download_status import TelegramDownloadStatus @@ -23,10 +20,7 @@ def __init__(self, listener): self.__name = "" self.__gid = '' self.__start_time = time.time() - self._bot = Client(api_id=TELEGRAM_API, - api_hash=TELEGRAM_HASH, - session_name=USER_SESSION_STRING) - self._bot.start() + self._bot = app self.__is_cancelled = False @property diff --git a/config_sample.env b/config_sample.env index babf78491d6..6ec0c51c0b6 100644 --- a/config_sample.env +++ b/config_sample.env @@ -11,7 +11,7 @@ AUTO_DELETE_MESSAGE_DURATION = 20 IS_TEAM_DRIVE = "" TELEGRAM_API = TELEGRAM_HASH = "" -DATABASE_URL = "" #Leave this empty if You deploying on Heroku with PostgreSQL, for using another database with Heroku just make the name of the var (DATABASE_URL), for vps You must fill database url here +DATABASE_URL = "" # Optional config AUTHORIZED_CHATS = "" USE_SERVICE_ACCOUNTS = ""