inlines first iteration

This commit is contained in:
mihalin 2022-02-11 02:02:28 +03:00
parent 177603606f
commit a7a08639cf
2 changed files with 43 additions and 1 deletions

View File

@ -11,7 +11,7 @@ import logging
import typing as ty
from olgram.settings import ServerSettings
from olgram.models.models import Bot, GroupChat, BannedUser
from server.inlines import inline_handler
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.INFO)
@ -136,6 +136,11 @@ async def receive_left(message: types.Message):
await bot.save()
async def receive_inline(inline_query):
bot = db_bot_instance.get()
return await inline_handler(inline_query, bot)
async def receive_migrate(message: types.Message):
bot = db_bot_instance.get()
from_id = message.chat.id
@ -175,6 +180,7 @@ class CustomRequestHandler(WebhookRequestHandler):
dp.register_message_handler(receive_left, content_types=[types.ContentType.LEFT_CHAT_MEMBER])
dp.register_message_handler(receive_migrate, content_types=[types.ContentType.MIGRATE_TO_CHAT_ID])
dp.register_message_handler(receive_group_create, content_types=[types.ContentType.GROUP_CHAT_CREATED])
dp.register_inline_handler(receive_inline)
return dp

36
server/inlines.py Normal file
View File

@ -0,0 +1,36 @@
from aiocache import cached
import hashlib
from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle
from olgram.models.models import Bot, DefaultAnswer
import typing as ty
@cached(ttl=1)
async def get_phrases(bot: int) -> ty.List:
objects = await DefaultAnswer.filter(bot_id=bot)
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
all_phrases = await get_phrases(bot.id)
phrases = [phrase for phrase in all_phrases if inline_query.query.lower() in phrase.lower()]
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)