Модульный Telegram-бот с возможностью редактирования прав доступа, как пользователям, так и группам пользователей
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.9 KiB

# -*- coding: utf8 -*-
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Сообщения для работы с документами
from bot_sys import log, config, user_access
from bot_modules import groups_utils
from template import simple_message
#import odf
def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_ErrorMessage, access_mode = user_access.AccessMode.EDIT):
async def DocFiles(a_Message):
user_id = str(a_Message.from_user.id)
user_groups= groups_utils.GetUserGroupData(a_Bot, user_id)
if not user_access.CheckAccess(a_Bot.GetRootIDs(), a_AccessFunc(), user_groups, access_mode):
return await simple_message.AccessDeniedMessage(a_Bot, a_GetButtonsFunc, user_id, a_Message, user_groups)
msg = a_CaptionMessage.GetDesc()
msg = msg.replace('@time', a_Bot.GetLog().GetTime())
files = a_FilesFunc(user_id)
if not files:
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
return
for file_path, dict_replace in files.items():
if not dict_replace:
continue
new_file = await MakeDocFile(a_Bot, file_path, dict_replace, user_id)
document = await GetFile(a_Bot, new_file)
if document is None:
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
else:
await a_Bot.SendDocument(
user_id,
document,
msg,
simple_message.ProxyGetButtonsTemplate(a_GetButtonsFunc)(a_Message, user_groups),
simple_message.ProxyGetButtonsTemplate(a_GetInlineButtonsFunc)(a_Message, user_groups)
)
return DocFiles
async def ReplaceInFile(a_Bot, a_InputFileName, a_OutFileName, a_DictReplace):
try:
filedata = ''
with open(a_InputFileName, 'r') as in_f:
filedata = in_f.read()
s = filedata
for rep_this, to_this in a_DictReplace.items():
s = s.replace(rep_this, to_this)
with open(a_OutFileName, 'w') as out_f:
out_f.write(s)
a_Bot.GetLog().Success(f'Создан файл {a_OutFileName}')
return a_OutFileName
except Exception as e:
a_Bot.GetLog().Error(f'Не удалось заменить текст в фале {a_InputFileName} и записать в {a_OutFileName}. Ошибка {str(e)}')
return None
from weasyprint import HTML, CSS
async def SaveAsPdf(a_Bot, a_InputFileName, a_OutFileName):
try:
HTML(filename = a_InputFileName).write_pdf(a_OutFileName)
a_Bot.GetLog().Success(f'Создан файл {a_OutFileName}')
return a_OutFileName
except Exception as e:
a_Bot.GetLog().Error(f'Не удалось создать пдф из фала {a_InputFileName} и записать в {a_OutFileName}. Ошибка {str(e)}')
return None
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id):
user_file_path = a_FilePath[:-5]
pdf_file_path = user_file_path
user_file_path += f"_{a_user_id}.html"
pdf_file_path += f"_{a_user_id}.pdf"
user_file_path = await ReplaceInFile(a_Bot, a_FilePath, user_file_path, a_DictReplace)
if not user_file_path:
return None
return await SaveAsPdf(a_Bot, user_file_path, pdf_file_path)
async def GetFile(a_Bot, a_Path):
if not a_Path:
return None
try:
document = open(a_Path, 'rb')
a_Bot.GetLog().Success(f'Загружен файл {a_Path}')
return document
except Exception as e:
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_Path}. Ошибка {str(e)}')
return None