minor fixes

This commit is contained in:
mihalin 2022-02-11 21:19:30 +03:00
parent d53a574377
commit a3eb985d28
2 changed files with 19 additions and 6 deletions

View File

@ -5,7 +5,7 @@ from olgram.models.models import Bot, User, DefaultAnswer
from aiogram.dispatcher import FSMContext
from aiogram.utils.callback_data import CallbackData
from textwrap import dedent
from olgram.utils.mix import edit_or_create, button_text_limit
from olgram.utils.mix import edit_or_create, button_text_limit, wrap
from olgram.commands import bot_actions
import typing as ty
@ -237,7 +237,13 @@ async def send_bot_templates_menu(bot: Bot, call: ty.Optional[types.CallbackQuer
""")
templates = await bot.answers
templates_text = "\n".join(f"{n}. {template.text}" for n, template in enumerate(templates))
total_text_len = sum(len(t.text) for t in templates) + len(text) # примерная длина текста
max_len = 1000
if total_text_len > 4000:
max_len = 100
templates_text = "\n".join(f"{n}. {wrap(template.text, max_len)}" for n, template in enumerate(templates))
if not templates_text:
templates_text = "(нет шаблонов)"
text = text.format(bot.name, templates_text)
@ -283,6 +289,10 @@ async def template_received(message: types.Message, state: FSMContext):
await templates[number].delete()
else:
# Add template
total_templates = len(await bot.answers)
if total_templates > 30:
await message.answer("У вашего бота уже слишком много щаблонов")
else:
template = DefaultAnswer(text=message.text, bot=bot)
await template.save()

View File

@ -22,8 +22,11 @@ async def edit_or_create(call: CallbackQuery, message: str,
parse_mode=parse_mode)
def button_text_limit(data: str) -> str:
max_len = 30
def wrap(data: str, max_len: int) -> str:
if len(data) > max_len:
data = data[:max_len-4] + "..."
return data
def button_text_limit(data: str) -> str:
return wrap(data, 30)