Files
infinity-cats-bot/bot/bot.py
2025-10-24 03:31:59 +03:00

55 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import asyncio
import httpx
from aiogram import Bot, Dispatcher
from aiogram.types import (
InlineQuery,
InlineQueryResultPhoto,
)
from bestconfig import Config
from loguru import logger
config = Config()
ITEMS_PER_PAGE = 30
# Инициализация бота и диспетчера
bot = Bot(token=config.TOKEN)
dp = Dispatcher()
async def fetch_images(limit=10):
url = "https://cataas.com/cat?json=true"
async with httpx.AsyncClient() as client:
images = await asyncio.gather(*[client.get(url) for _ in range(limit)])
return [
{"photo": im.json()["url"], "thumb": im.json()["url"] + "?width=100"}
for im in images
]
@dp.inline_query()
async def inline_query(query: InlineQuery):
offset = int(query.offset or 0)
# Запрашиваем данные с сервера
items = await fetch_images(limit=ITEMS_PER_PAGE)
# Формируем результаты
results = [
InlineQueryResultPhoto(
id=str(offset + i),
photo_url=item["photo"],
thumbnail_url=item["thumb"],
)
for i, item in enumerate(items)
]
# Определяем следующий offset
next_offset = str(offset + ITEMS_PER_PAGE)
await query.answer(
results=results,
cache_time=60,
is_personal=True,
next_offset=next_offset,
)