olgram/olgram/utils/mix.py
2021-09-17 11:35:51 +03:00

30 lines
987 B
Python

from aiogram.types import Message, CallbackQuery, InlineKeyboardMarkup
from aiogram.utils.exceptions import TelegramAPIError
from typing import Optional
async def try_delete_message(message: Message):
try:
await message.delete()
except TelegramAPIError:
pass
async def edit_or_create(call: CallbackQuery, message: str,
reply_markup: Optional[InlineKeyboardMarkup] = None,
parse_mode: Optional[str] = None):
try:
await call.message.edit_text(message, parse_mode=parse_mode)
await call.message.edit_reply_markup(reply_markup)
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