from aiogram import Bot, Dispatcher, Router, F
from aiogram.types import (
Message,
MessageEntity,
InlineKeyboardMarkup,
InlineKeyboardButton,
CallbackQuery,
)
from aiogram.enums import ParseMode
import asyncio
router = Router()
BOT_TOKEN = "TOKEN"
FIRE = "5368324170671202286"
STAR = "5471952986970267163"
ROCKET = "5386367538735104399"
CHECK = "5382322671028990089"
def utf16_len(s: str) -> int:
return len(s.encode("utf-16-le")) // 2
# 1. HTML orqali
@router.message(F.text == "/html")
async def cmd_html(message: Message):
text = (
f'⭐️ Salom!\n'
f'🔥 Premium emoji
HTML orqali'
)
await message.answer(text, parse_mode=ParseMode.HTML)
# 2. MarkdownV2 orqali
@router.message(F.text == "/markdown")
async def cmd_markdown(message: Message):
text = (
f" Salom\\!\n"
f" Premium emoji *MarkdownV2* orqali"
)
await message.answer(text, parse_mode=ParseMode.MARKDOWN_V2)
# 3. entities orqali (parse_mode kerak emas)
@router.message(F.text == "/entities")
async def cmd_entities(message: Message):
p1 = "⭐️"
p2 = " Salom! "
p3 = "🔥"
p4 = " Premium emoji entities orqali"
text = p1 + p2 + p3 + p4
entities = [
MessageEntity(
type="custom_emoji",
offset=0,
length=utf16_len(p1),
custom_emoji_id=STAR,
),
MessageEntity(
type="custom_emoji",
offset=utf16_len(p1 + p2),
length=utf16_len(p3),
custom_emoji_id=FIRE,
),
]
await message.answer(text, entities=entities)
# 4. inline tugmalarda premium emoji
@router.message(F.text == "/menu")
async def cmd_menu(message: Message):
text = (
f'⭐️
Asosiy menyu\n\n'
f'🔥 Tugmani tanlang:'
)
kb = InlineKeyboardMarkup(inline_keyboard=[
[
InlineKeyboardButton(
text="Profil",
callback_data="profile",
icon_custom_emoji_id=STAR,
),
InlineKeyboardButton(
text="Sozlamalar",
callback_data="settings",
icon_custom_emoji_id=FIRE,
),
],
[
InlineKeyboardButton(
text="Premium",
callback_data="premium",
icon_custom_emoji_id=ROCKET,
style="success",
),
],
[
InlineKeyboardButton(
text="Bekor qilish",
callback_data="cancel",
icon_custom_emoji_id=CHECK,
style="danger",
),
],
])
await message.answer(text, parse_mode=ParseMode.HTML, reply_markup=kb)
# Yuborilgan Premium emoji ID ni olish
@router.message(F.entities)
async def get_emoji_id(message: Message):
ids = [
e.custom_emoji_id
for e in message.entities
if e.type == "custom_emoji" and e.custom_emoji_id
]
if ids:
lines = "\n".join(f"{eid}" for eid in ids)
await message.reply(f"Emoji ID:\n{lines}", parse_mode=ParseMode.HTML)
@router.callback_query(F.data.in_({"profile", "settings", "premium", "cancel"}))
async def on_callback(callback: CallbackQuery):
await callback.answer(f"{callback.data} bosildi")
async def main():
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
dp.include_router(router)
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())
Telegram bot api'dagi ba'zi yangilanishlarni ishlatish uchun
aiogram kodi namunasi (AI orqali yozilgan, tekshirib ko'rilgan)