olgram/server/inlines.py

38 lines
1.2 KiB
Python
Raw Normal View History

2022-02-11 02:02:28 +03:00
from aiocache import cached
import hashlib
from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle
2022-02-11 04:08:14 +03:00
from olgram.models.models import Bot
2022-02-11 02:02:28 +03:00
import typing as ty
@cached(ttl=1)
2022-02-11 04:06:28 +03:00
async def get_phrases(bot: Bot) -> ty.List:
objects = await bot.answers
2022-02-11 02:02:28 +03:00
return [obj.text for obj in objects]
async def inline_handler(inline_query: InlineQuery, bot: Bot):
# Check permissions at first
# user_id = inline_query.from_user.id
# if bot.super_chat_id() != user_id:
# return await inline_query.answer([], cache_time=1) # forbidden
2022-02-11 04:06:28 +03:00
all_phrases = await get_phrases(bot)
2022-02-11 03:59:58 +03:00
print(f"All phrases {all_phrases}")
2022-02-11 02:02:28 +03:00
phrases = [phrase for phrase in all_phrases if inline_query.query.lower() in phrase.lower()]
2022-02-11 03:59:58 +03:00
print(f"phrases {phrases}")
2022-02-11 02:02:28 +03:00
items = []
for phrase in phrases:
input_content = InputTextMessageContent(phrase)
result_id: str = hashlib.md5(phrase.encode()).hexdigest()
item = InlineQueryResultArticle(
id=result_id,
title=phrase,
input_message_content=input_content,
)
items.append(item)
await inline_query.answer(results=items, cache_time=1)