55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
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,
|
||
)
|