Update voice bot functionality and clean up project structure
- Added voice message handling capabilities, including saving and deleting audio messages via callback queries. - Refactored audio record management in the database to remove unnecessary fields and streamline operations. - Introduced new keyboard options for voice interactions in the bot. - Updated `.gitignore` to include voice user files for better project organization. - Removed obsolete voice bot handler files to simplify the codebase.
This commit is contained in:
95
helper_bot/handlers/voice/utils.py
Normal file
95
helper_bot/handlers/voice/utils.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import time
|
||||
import html
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from helper_bot.handlers.voice.exceptions import DatabaseError
|
||||
from logs.custom_logger import logger
|
||||
|
||||
|
||||
def format_time_ago(date_from_db: str) -> Optional[str]:
|
||||
"""Форматировать время с момента последней записи"""
|
||||
try:
|
||||
if date_from_db is None:
|
||||
return None
|
||||
|
||||
parse_date = datetime.strptime(date_from_db, "%Y-%m-%d %H:%M:%S")
|
||||
last_voice_time_timestamp = time.mktime(parse_date.timetuple())
|
||||
time_now_timestamp = time.time()
|
||||
date_difference = time_now_timestamp - last_voice_time_timestamp
|
||||
|
||||
# Считаем минуты, часы, дни
|
||||
much_minutes_ago = round(date_difference / 60, 0)
|
||||
much_hour_ago = round(date_difference / 3600, 0)
|
||||
much_days_ago = int(round(much_hour_ago / 24, 0))
|
||||
|
||||
message_with_date = ''
|
||||
if much_minutes_ago <= 60:
|
||||
word_minute = plural_time(1, much_minutes_ago)
|
||||
# Экранируем потенциально проблемные символы
|
||||
word_minute_escaped = html.escape(word_minute)
|
||||
message_with_date = f'<b>Последнее сообщение было записано {word_minute_escaped} назад</b>'
|
||||
elif much_minutes_ago > 60 and much_hour_ago <= 24:
|
||||
word_hour = plural_time(2, much_hour_ago)
|
||||
# Экранируем потенциально проблемные символы
|
||||
word_hour_escaped = html.escape(word_hour)
|
||||
message_with_date = f'<b>Последнее сообщение было записано {word_hour_escaped} назад</b>'
|
||||
elif much_hour_ago > 24:
|
||||
word_day = plural_time(3, much_days_ago)
|
||||
# Экранируем потенциально проблемные символы
|
||||
word_day_escaped = html.escape(word_day)
|
||||
message_with_date = f'<b>Последнее сообщение было записано {word_day_escaped} назад</b>'
|
||||
|
||||
return message_with_date
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при форматировании времени: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def plural_time(type: int, n: float) -> str:
|
||||
"""Форматировать множественное число для времени"""
|
||||
word = []
|
||||
if type == 1:
|
||||
word = ['минуту', 'минуты', 'минут']
|
||||
elif type == 2:
|
||||
word = ['час', 'часа', 'часов']
|
||||
elif type == 3:
|
||||
word = ['день', 'дня', 'дней']
|
||||
else:
|
||||
return str(int(n))
|
||||
|
||||
if n % 10 == 1 and n % 100 != 11:
|
||||
p = 0
|
||||
elif 2 <= n % 10 <= 4 and (n % 100 < 10 or n % 100 >= 20):
|
||||
p = 1
|
||||
else:
|
||||
p = 2
|
||||
|
||||
new_number = int(n)
|
||||
return str(new_number) + ' ' + word[p]
|
||||
|
||||
|
||||
def get_last_message_text(bot_db) -> Optional[str]:
|
||||
"""Получить текст сообщения о времени последней записи"""
|
||||
try:
|
||||
date_from_db = bot_db.last_date_audio()
|
||||
return format_time_ago(date_from_db)
|
||||
except Exception as e:
|
||||
logger.error(f"Не удалось получить дату последнего сообщения - {e}")
|
||||
return None
|
||||
|
||||
|
||||
def validate_voice_message(message) -> bool:
|
||||
"""Проверить валидность голосового сообщения"""
|
||||
return message.content_type == 'voice'
|
||||
|
||||
|
||||
def get_user_emoji_safe(bot_db, user_id: int) -> str:
|
||||
"""Безопасно получить эмодзи пользователя"""
|
||||
try:
|
||||
user_emoji = bot_db.check_emoji_for_user(user_id)
|
||||
return user_emoji if user_emoji else "😊"
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при получении эмодзи пользователя {user_id}: {e}")
|
||||
return "😊"
|
||||
Reference in New Issue
Block a user