Fixed reports ignorance

This commit is contained in:
Profitroll 2022-08-09 10:01:02 +02:00
parent 13f6286fd0
commit e65d98deca

34
main.py
View File

@ -8,9 +8,11 @@ import time
import traceback import traceback
import schedule import schedule
pid = os.getpid()
try: try:
from pyrogram import Client, filters, idle # type: ignore from pyrogram import Client, filters, idle # type: ignore
from pyrogram.types import ChatPermissions, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton # type: ignore from pyrogram.types import ChatPermissions, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton, BotCommand, BotCommandScopeChat # type: ignore
except ModuleNotFoundError: except ModuleNotFoundError:
print(f"Module pyrogram is not installed. Run 'pip3 install -r requirements.txt' and restart the program.", flush=True) print(f"Module pyrogram is not installed. Run 'pip3 install -r requirements.txt' and restart the program.", flush=True)
sys.exit() sys.exit()
@ -57,6 +59,7 @@ def send_content():
candidate = configGet("queue", "locations")+os.sep+candidate_file candidate = configGet("queue", "locations")+os.sep+candidate_file
else: else:
logWrite(f"Could not send content due to queue folder empty with allowed extensions") logWrite(f"Could not send content due to queue folder empty with allowed extensions")
if configGet("error", "reports"):
app.send_message(configGet("admin", "reports"), f"Could not send content: `Queue folder is empty or contains only unsupported or already sent files.`") # type: ignore app.send_message(configGet("admin", "reports"), f"Could not send content: `Queue folder is empty or contains only unsupported or already sent files.`") # type: ignore
return return
@ -86,15 +89,25 @@ def send_content():
if configGet("move_sent", "posting"): if configGet("move_sent", "posting"):
shutil.move(candidate, configGet("sent", "locations")+os.sep+candidate_file) shutil.move(candidate, configGet("sent", "locations")+os.sep+candidate_file)
if configGet("sent", "reports"):
app.send_message(configGet("admin", "reports"), f"Posted `{candidate_file}`", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup([ app.send_message(configGet("admin", "reports"), f"Posted `{candidate_file}`", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton("View in channel", url=sent.link)] # type: ignore [InlineKeyboardButton("View in channel", url=sent.link)] # type: ignore
])) # type: ignore ])) # type: ignore
except Exception as exp: except Exception as exp:
logWrite(f"Could not send content due to {exp}") logWrite(f"Could not send content due to {exp}")
if configGet("error", "reports"):
app.send_message(configGet("admin", "reports"), f"Could not send content due to `{exp}`\n\nTraceback:\n```{traceback.format_exc()}```") # type: ignore app.send_message(configGet("admin", "reports"), f"Could not send content due to `{exp}`\n\nTraceback:\n```{traceback.format_exc()}```") # type: ignore
@app.on_message(~ filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"]))
def kill(app, msg):
if msg.from_user.id == configGet("admin", "reports"):
msg.reply_text(f"Shutting down bot with pid `{pid}`")
os.system('kill -9 '+str(pid))
for entry in configGet("time", "posting"): for entry in configGet("time", "posting"):
schedule.every().day.at(entry).do(send_content) schedule.every().day.at(entry).do(send_content)
@ -111,22 +124,27 @@ def background_task():
logWrite(f"Exception {exp} happened on task execution") logWrite(f"Exception {exp} happened on task execution")
except KeyboardInterrupt: except KeyboardInterrupt:
logWrite('\nShutting down...') logWrite('\nShutting down...')
app.send_message(configGet("admin", "reports"), f"Shutting down with PID `{os.getpid()}`") # type: ignore app.send_message(configGet("admin", "reports"), f"Shutting down with PID `{pid}`") # type: ignore
os.system('kill -9 '+str(os.getpid())) os.system('kill -9 '+str(pid))
if __name__ == "__main__": if __name__ == "__main__":
logWrite(f'Starting with PID {os.getpid()}') logWrite(f'Starting with PID {pid}')
app.start() # type: ignore app.start() # type: ignore
app.send_message(configGet("admin", "reports"), f"Starting with pid `{os.getpid()}`") # type: ignore app.send_message(configGet("admin", "reports"), f"Starting with pid `{pid}`") # type: ignore
t = Thread(target=background_task) t = Thread(target=background_task)
t.start() t.start()
app.set_bot_commands([ # type: ignore
BotCommand("reboot", "Reboot the bot"),
],
scope=BotCommandScopeChat(chat_id=configGet("admin", "reports")))
idle() idle()
app.send_message(configGet("admin", "reports"), f"Shutting down with pid `{os.getpid()}`") # type: ignore app.send_message(configGet("admin", "reports"), f"Shutting down with pid `{pid}`") # type: ignore
logWrite(f'Shutting down with PID {os.getpid()}') logWrite(f'Shutting down with PID {pid}')
subprocess.call(f'kill -9 {os.getpid()}', shell=True) # type: ignore subprocess.call(f'kill -9 {pid}', shell=True) # type: ignore