inline permissions

This commit is contained in:
mihalin 2022-02-11 04:24:11 +03:00
parent 0fbfa9bd1e
commit a5a6d5beac

View File

@ -1,6 +1,7 @@
from aiocache import cached from aiocache import cached
import hashlib import hashlib
from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle
from aiogram.bot import Bot as AioBot
from olgram.models.models import Bot from olgram.models.models import Bot
import typing as ty import typing as ty
@ -12,17 +13,35 @@ async def get_phrases(bot: Bot) -> ty.List:
return [obj.text for obj in objects] return [obj.text for obj in objects]
@cached(ttl=1)
async def check_chat_member(chat_id: int, user_id: int, bot: AioBot) -> bool:
member = await bot.get_chat_member(chat_id, user_id)
return member.is_chat_member()
@cached(ttl=1)
async def check_permissions(inline_query: InlineQuery, bot: Bot):
user_id = inline_query.from_user.id
super_chat_id = await bot.super_chat_id()
if super_chat_id == user_id:
return True
if super_chat_id < 0: # Group chat
is_member = await check_chat_member(super_chat_id, user_id, inline_query.bot)
return is_member
return False
async def inline_handler(inline_query: InlineQuery, bot: Bot): async def inline_handler(inline_query: InlineQuery, bot: Bot):
# Check permissions at first # Check permissions at first
# user_id = inline_query.from_user.id allow = await check_permissions(inline_query, bot)
# if bot.super_chat_id() != user_id: if not allow:
# return await inline_query.answer([], cache_time=1) # forbidden return await inline_query.answer([], cache_time=1) # forbidden
all_phrases = await get_phrases(bot) all_phrases = await get_phrases(bot)
print(f"All phrases {all_phrases}")
phrases = [phrase for phrase in all_phrases if inline_query.query.lower() in phrase.lower()] phrases = [phrase for phrase in all_phrases if inline_query.query.lower() in phrase.lower()]
print(f"phrases {phrases}")
items = []
for phrase in phrases: for phrase in phrases:
input_content = InputTextMessageContent(phrase) input_content = InputTextMessageContent(phrase)