Change sended message

Add deleting logs before command, change .sh file
This commit is contained in:
Dima YaFlay
2022-05-21 00:26:36 +03:00
committed by GitHub
parent 47451b64a3
commit 3ea8e473bf

26
main.py
View File

@@ -1,18 +1,23 @@
# in your server config socketInput should be default (localhost) # in your server config socketInput should be default (localhost)
from datetime import datetime
from datetime import datetime
import os import os
import sys import sys
import logging import logging
from aiogram import * from aiogram import *
from aiogram.types import ContentType from aiogram.types import ContentType
BOT_TOKEN = 'UR TOKEN'
BOT_TOKEN = ''
bot = Bot(token=BOT_TOKEN, parse_mode="HTML") bot = Bot(token=BOT_TOKEN, parse_mode="HTML")
dp = Dispatcher(bot) dp = Dispatcher(bot)
main_path = __file__.replace(os.path.basename(__file__), '') main_path = __file__.replace(os.path.basename(__file__), '')
with open(f'{main_path}/run_server.sh', 'r+') as file: fileShReplace = file.read(); file.close()
with open(f'{main_path}/run_server.sh', 'w+') as file: file.close()
with open(f'{main_path}/run_server.sh', 'r+') as file: file.write(fileShReplace.replace('echo "config socketInput true" | ', ''))
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] %(message)s") logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] %(message)s")
rootLogger = logging.getLogger() rootLogger = logging.getLogger()
@@ -28,19 +33,29 @@ def server_connect(text):
@dp.message_handler(content_types= ContentType.TEXT) @dp.message_handler(content_types= ContentType.TEXT)
async def command(message: types.message): async def command(message: types.message):
if not os.path.isfile(f'{main_path}/config/logs/copy-log.log'): file = open(f'{main_path}/config/logs/copy-log.log', 'x'); file.close()
with open(f'{main_path}/config/logs/log-0.txt', 'r') as file:
with open(f'{main_path}/config/logs/copy-log.log', 'r+') as file2:
file2.write(file.read())
file2.close()
file.close()
with open(f'{main_path}/config/logs/log-0.txt', 'w+') as file:
file.close()
now = datetime.now() now = datetime.now()
date_time = now.strftime("%m-%d-%Y %H:%M:%S") date_time = now.strftime("%m-%d-%Y %H:%M:%S")
server_connect(message.text) server_connect(message.text)
rootLogger.info(message.text) rootLogger.info(message.text)
await message.reply('Command entried!') await message.reply('Command entried!')
with open(f'{main_path}/config/logs/log-0.txt', mode='r') as file: with open(f'{main_path}/config/logs/log-0.txt', mode='r') as file:
text = file.read().replace("<", '').replace(">", '').replace(f"[{date_time}]", '').replace('Received command socket connection: localhost/127.0.0.1:6859', '').replace('Lost command socket connection: localhost/127.0.0.1:6859', '').replace('[I]', '').encode('utf8') text = file.read().replace("<", '(').replace(">", ')').replace(f"[{date_time}]", '').replace('Received command socket connection: localhost/127.0.0.1:6859', '').replace('Lost command socket connection: localhost/127.0.0.1:6859', '').replace('[I]', '').encode('utf8')
if len(text) > 4096: if len(text) > 4096:
for x in range(0, len(text), 4096): for x in range(0, len(text), 4096):
await message.answer(text[x:x+4096].decode('utf8')) await message.answer(text[x:x+4096].decode('utf8'))
else: else:
try:
await message.answer(text.decode('utf8')) await message.answer(text.decode('utf8'))
# await message.answer(f'''output: {text}''') except:
await message.answer(f'Server don`t reply.')
with open(f'{main_path}/config/logs/log-0.txt', 'w+') as file: with open(f'{main_path}/config/logs/log-0.txt', 'w+') as file:
file.close() file.close()
file.close() file.close()
@@ -53,4 +68,3 @@ try:
executor.start_polling(dp, skip_updates=False) executor.start_polling(dp, skip_updates=False)
except Exception: except Exception:
sys.exit() sys.exit()