first commit
This commit is contained in:
54
bot/bot.py
Normal file
54
bot/bot.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import asyncio
|
||||
|
||||
import httpx
|
||||
from aiogram import Bot, Dispatcher
|
||||
from aiogram.types import (
|
||||
InlineQuery,
|
||||
InlineQueryResultPhoto,
|
||||
)
|
||||
from bestconfig import Config
|
||||
|
||||
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,
|
||||
)
|
||||
Reference in New Issue
Block a user