add answer for user

This commit is contained in:
Andrey
2024-07-21 13:24:18 +03:00
parent 0704e6b3fe
commit 33fa84943d
9 changed files with 296 additions and 176 deletions

View File

@@ -26,9 +26,7 @@ def get_text_message(post_text: str, first_name: str, username: str):
username: Юзернейм автора поста
Returns:
Кортеж из двух элементов:
- Сформированный текст сообщения.
- Флаг, указывающий, является ли пост анонимным (True - анонимный, False - не анонимный)
str: - Сформированный текст сообщения.
"""
if "неанон" in post_text or "не анон" in post_text:
return f'Пост из ТГ:\n{post_text}\n\nАвтор поста: {first_name} @{username}'
@@ -54,6 +52,9 @@ async def download_file(message: types.Message, file_id: str):
os.makedirs("files", exist_ok=True)
os.makedirs("files/photos", exist_ok=True)
os.makedirs("files/videos", exist_ok=True)
os.makedirs("files/music", exist_ok=True)
os.makedirs("files/voice", exist_ok=True)
os.makedirs("files/video_notes", exist_ok=True)
file = await message.bot.get_file(file_id)
file_path = os.path.join("files", file.file_path)
await message.bot.download_file(file_path=file.file_path, destination=file_path)
@@ -109,7 +110,7 @@ async def prepare_media_group_from_middlewares(album, post_caption: str = ''):
return media_group # Возвращаем MediaGroup
async def add_in_db_media(sent_message):
async def add_in_db_media_mediagroup(sent_message):
"""
Идентификатор медиа-группы
@@ -134,26 +135,34 @@ async def add_in_db_media(sent_message):
continue
async def add_in_db_media_old(sent_message):
async def add_in_db_media(sent_message):
"""
Идентификатор медиа-группы
Args:
sent_message: sent_message объект из Telegram API
Returns:
Список InputMediaPhoto.
Список InputFile (FSInputFile).
"""
media_group_message_id = sent_message[-1].message_id # Получаем идентификатор медиа-группы
for i, message in enumerate(sent_message):
file_id = message.photo[-1].file_id
file_path = await download_file(message, file_id=file_id)
if i == 0:
BotDB.add_post_content_in_db(media_group_message_id, message.message_id, file_path)
elif i == len(sent_message) - 1:
BotDB.add_post_content_in_db(media_group_message_id, message.message_id, file_path)
else:
BotDB.add_post_content_in_db(media_group_message_id, message.message_id, file_path)
if sent_message.photo:
file_id = sent_message.photo[-1].file_id
file_path = await download_file(sent_message, file_id=file_id)
BotDB.add_post_content_in_db(sent_message.message_id, sent_message.message_id, file_path, 'photo')
elif sent_message.video:
file_id = sent_message.video.file_id
file_path = await download_file(sent_message, file_id=file_id)
BotDB.add_post_content_in_db(sent_message.message_id, sent_message.message_id, file_path, 'video')
elif sent_message.voice:
file_id = sent_message.voice.file_id
file_path = await download_file(sent_message, file_id=file_id)
BotDB.add_post_content_in_db(sent_message.message_id, sent_message.message_id, file_path, 'voice')
elif sent_message.audio:
file_id = sent_message.audio.file_id
file_path = await download_file(sent_message, file_id=file_id)
BotDB.add_post_content_in_db(sent_message.message_id, sent_message.message_id, file_path, 'audio')
elif sent_message.video_note:
file_id = sent_message.video_note.file_id
file_path = await download_file(sent_message, file_id=file_id)
BotDB.add_post_content_in_db(sent_message.message_id, sent_message.message_id, file_path, 'video_note')
async def send_media_group_message_to_private_chat(chat_id: int, message: types.Message,
@@ -163,7 +172,7 @@ async def send_media_group_message_to_private_chat(chat_id: int, message: types.
media=media_group,
)
BotDB.add_post_in_db(sent_message[-1].message_id, sent_message[-1].caption, message.from_user.id)
await add_in_db_media(sent_message)
await add_in_db_media_mediagroup(sent_message)
message_id = sent_message[-1].message_id
return message_id
@@ -219,82 +228,87 @@ async def send_text_message(chat_id, message: types.Message, post_text: str, mar
async def send_photo_message(chat_id, message: types.Message, photo: str, post_text: str,
markup: types.ReplyKeyboardMarkup = None):
if markup is None:
await message.bot.send_photo(
sent_message = await message.bot.send_photo(
chat_id=chat_id,
caption=post_text,
photo=photo
)
else:
await message.bot.send_photo(
sent_message = await message.bot.send_photo(
chat_id=chat_id,
caption=post_text,
photo=photo,
reply_markup=markup
)
return sent_message
async def send_video_message(chat_id, message: types.Message, video: str, post_text: str = "",
markup: types.ReplyKeyboardMarkup = None):
if markup is None:
await message.bot.send_video(
sent_message = await message.bot.send_video(
chat_id=chat_id,
caption=post_text,
video=video
)
else:
await message.bot.send_video(
sent_message = await message.bot.send_video(
chat_id=chat_id,
caption=post_text,
video=video,
reply_markup=markup
)
return sent_message
async def send_video_note_message(chat_id, message: types.Message, video_note: str,
markup: types.ReplyKeyboardMarkup = None):
if markup is None:
await message.bot.send_video_note(
sent_message = await message.bot.send_video_note(
chat_id=chat_id,
video_note=video_note
)
else:
await message.bot.send_video_note(
sent_message = await message.bot.send_video_note(
chat_id=chat_id,
video_note=video_note,
reply_markup=markup
)
return sent_message
async def send_audio_message(chat_id, message: types.Message, audio: str, post_text: str,
markup: types.ReplyKeyboardMarkup = None):
if markup is None:
await message.bot.send_audio(
sent_message = await message.bot.send_audio(
chat_id=chat_id,
caption=post_text,
audio=audio
)
else:
await message.bot.send_audio(
sent_message = await message.bot.send_audio(
chat_id=chat_id,
caption=post_text,
audio=audio,
reply_markup=markup
)
return sent_message
async def send_voice_message(chat_id, message: types.Message, voice: str,
markup: types.ReplyKeyboardMarkup = None):
if markup is None:
await message.bot.send_voice(
sent_message = await message.bot.send_voice(
chat_id=chat_id,
voice=voice
)
else:
await message.bot.send_voice(
sent_message = await message.bot.send_voice(
chat_id=chat_id,
voice=voice,
reply_markup=markup
)
return sent_message
def check_access(user_id: int):