From 9f32edd1beeb291c8fc0b629f5f2e14deb8077b9 Mon Sep 17 00:00:00 2001 From: mihalin Date: Fri, 17 Sep 2021 11:35:51 +0300 Subject: [PATCH] fix group chat remove, some minor fixes --- olgram/commands/menu.py | 4 ++-- olgram/utils/mix.py | 7 +++++++ server/custom.py | 8 +++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/olgram/commands/menu.py b/olgram/commands/menu.py index a90a135..d225589 100644 --- a/olgram/commands/menu.py +++ b/olgram/commands/menu.py @@ -5,7 +5,7 @@ from olgram.models.models import Bot, User from aiogram.dispatcher import FSMContext from aiogram.utils.callback_data import CallbackData from textwrap import dedent -from olgram.utils.mix import edit_or_create +from olgram.utils.mix import edit_or_create, button_text_limit from olgram.commands import bot_actions import typing as ty @@ -57,7 +57,7 @@ async def send_chats_menu(bot: Bot, call: types.CallbackQuery): for chat in chats: keyboard.insert( - types.InlineKeyboardButton(text=chat.name, + types.InlineKeyboardButton(text=button_text_limit(chat.name), callback_data=menu_callback.new(level=3, bot_id=bot.id, operation="chat", chat=chat.id)) ) diff --git a/olgram/utils/mix.py b/olgram/utils/mix.py index ba82db9..0130a56 100644 --- a/olgram/utils/mix.py +++ b/olgram/utils/mix.py @@ -20,3 +20,10 @@ async def edit_or_create(call: CallbackQuery, message: str, except TelegramAPIError: # кнопка устарела await call.bot.send_message(call.message.chat.id, text=message, reply_markup=reply_markup, parse_mode=parse_mode) + + +def button_text_limit(data: str) -> str: + max_len = 30 + if len(data) > max_len: + data = data[:max_len-4] + "..." + return data diff --git a/server/custom.py b/server/custom.py index 8d984e9..5f86a35 100644 --- a/server/custom.py +++ b/server/custom.py @@ -12,6 +12,7 @@ import typing as ty from olgram.settings import ServerSettings from olgram.models.models import Bot, GroupChat + logging.basicConfig(level=logging.INFO) _logger = logging.getLogger(__name__) @@ -72,6 +73,7 @@ async def receive_invite(message: types.Message): if member.id == message.bot.id: chat, _ = await GroupChat.get_or_create(chat_id=message.chat.id, defaults={"name": message.chat.full_name}) + chat.name = message.chat.full_name if chat not in await bot.group_chats.all(): await bot.group_chats.add(chat) await bot.save() @@ -79,19 +81,15 @@ async def receive_invite(message: types.Message): async def receive_left(message: types.Message): - _logger.info("Receive left") bot = db_bot_instance.get() if message.left_chat_member.id == message.bot.id: chat = await bot.group_chats.filter(chat_id=message.chat.id).first() - _logger.info(f"chat found {chat}") if chat: await bot.group_chats.remove(chat) bot_group_chat = await bot.group_chat - _logger.info(f"chat removed {bot_group_chat} {chat}") if bot_group_chat == chat: - _logger.info("saved") bot.group_chat = None - await bot.save(update_fields=["group_chat"]) + await bot.save() class CustomRequestHandler(WebhookRequestHandler):