fix group chat remove, some minor fixes

This commit is contained in:
mihalin 2021-09-17 11:35:51 +03:00
parent cb05f9d1fa
commit 9f32edd1be
3 changed files with 12 additions and 7 deletions

View File

@ -5,7 +5,7 @@ from olgram.models.models import Bot, User
from aiogram.dispatcher import FSMContext from aiogram.dispatcher import FSMContext
from aiogram.utils.callback_data import CallbackData from aiogram.utils.callback_data import CallbackData
from textwrap import dedent 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 from olgram.commands import bot_actions
import typing as ty import typing as ty
@ -57,7 +57,7 @@ async def send_chats_menu(bot: Bot, call: types.CallbackQuery):
for chat in chats: for chat in chats:
keyboard.insert( 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", callback_data=menu_callback.new(level=3, bot_id=bot.id, operation="chat",
chat=chat.id)) chat=chat.id))
) )

View File

@ -20,3 +20,10 @@ async def edit_or_create(call: CallbackQuery, message: str,
except TelegramAPIError: # кнопка устарела except TelegramAPIError: # кнопка устарела
await call.bot.send_message(call.message.chat.id, text=message, reply_markup=reply_markup, await call.bot.send_message(call.message.chat.id, text=message, reply_markup=reply_markup,
parse_mode=parse_mode) 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

View File

@ -12,6 +12,7 @@ import typing as ty
from olgram.settings import ServerSettings from olgram.settings import ServerSettings
from olgram.models.models import Bot, GroupChat from olgram.models.models import Bot, GroupChat
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -72,6 +73,7 @@ async def receive_invite(message: types.Message):
if member.id == message.bot.id: if member.id == message.bot.id:
chat, _ = await GroupChat.get_or_create(chat_id=message.chat.id, chat, _ = await GroupChat.get_or_create(chat_id=message.chat.id,
defaults={"name": message.chat.full_name}) defaults={"name": message.chat.full_name})
chat.name = message.chat.full_name
if chat not in await bot.group_chats.all(): if chat not in await bot.group_chats.all():
await bot.group_chats.add(chat) await bot.group_chats.add(chat)
await bot.save() await bot.save()
@ -79,19 +81,15 @@ async def receive_invite(message: types.Message):
async def receive_left(message: types.Message): async def receive_left(message: types.Message):
_logger.info("Receive left")
bot = db_bot_instance.get() bot = db_bot_instance.get()
if message.left_chat_member.id == message.bot.id: if message.left_chat_member.id == message.bot.id:
chat = await bot.group_chats.filter(chat_id=message.chat.id).first() chat = await bot.group_chats.filter(chat_id=message.chat.id).first()
_logger.info(f"chat found {chat}")
if chat: if chat:
await bot.group_chats.remove(chat) await bot.group_chats.remove(chat)
bot_group_chat = await bot.group_chat bot_group_chat = await bot.group_chat
_logger.info(f"chat removed {bot_group_chat} {chat}")
if bot_group_chat == chat: if bot_group_chat == chat:
_logger.info("saved")
bot.group_chat = None bot.group_chat = None
await bot.save(update_fields=["group_chat"]) await bot.save()
class CustomRequestHandler(WebhookRequestHandler): class CustomRequestHandler(WebhookRequestHandler):