This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Telegram/modules/inline.py

318 lines
14 KiB
Python
Raw Normal View History

2023-01-03 16:12:46 +02:00
"""Module responsible for providing answers to
all inline queries that bot receives"""
2022-10-26 15:54:55 +03:00
from datetime import datetime
from os import path, sep
2022-12-16 14:19:50 +02:00
from app import app, isAnAdmin
2023-03-09 17:25:06 +02:00
from pyrogram.types import (
InlineQueryResultArticle,
InputTextMessageContent,
InlineQuery,
InlineKeyboardMarkup,
InlineKeyboardButton,
)
2022-12-27 19:46:17 +02:00
from pyrogram.client import Client
2022-10-26 15:54:55 +03:00
from pyrogram.enums.chat_type import ChatType
from pyrogram.enums.chat_members_filter import ChatMembersFilter
from dateutil.relativedelta import relativedelta
2023-01-06 16:49:51 +02:00
from classes.errors.holo_user import UserNotFoundError, UserInvalidError
from classes.holo_user import HoloUser
2023-01-09 13:21:21 +02:00
from modules.logging import logWrite
2023-01-07 11:20:58 +02:00
from modules.utils import configGet, jsonLoad, locale
2023-01-04 20:58:54 +02:00
from modules.database import col_applications, col_spoilers
from bson.objectid import ObjectId
from bson.errors import InvalidId
2022-10-26 15:54:55 +03:00
2023-03-09 17:25:06 +02:00
2022-10-26 15:54:55 +03:00
@app.on_inline_query()
2022-12-27 19:46:17 +02:00
async def inline_answer(client: Client, inline_query: InlineQuery):
2023-01-04 20:58:54 +02:00
results = []
if configGet("allow_external", "features", "spoilers") is True:
if inline_query.query.startswith("spoiler:"):
try:
2023-03-09 17:25:06 +02:00
spoil = col_spoilers.find_one(
{"_id": ObjectId(inline_query.query.removeprefix("spoiler:"))}
)
2023-01-04 20:58:54 +02:00
if spoil is not None:
2023-03-09 17:25:06 +02:00
desc = (
locale(
"spoiler_empty", "message", locale=inline_query.from_user
).format(
locale(spoil["category"], "message", "spoiler_categories")
)
if spoil["description"] == ""
else locale(
"spoiler_described",
"message",
locale=inline_query.from_user,
).format(
locale(spoil["category"], "message", "spoiler_categories"),
spoil["description"],
)
)
2023-01-04 20:58:54 +02:00
results = [
InlineQueryResultArticle(
2023-03-09 17:25:06 +02:00
title=locale(
"title",
"inline",
"spoiler",
locale=inline_query.from_user,
),
description=locale(
"description",
"inline",
"spoiler",
locale=inline_query.from_user,
),
input_message_content=InputTextMessageContent(
desc, disable_web_page_preview=True
),
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
locale(
"spoiler_view",
"button",
locale=inline_query.from_user,
),
callback_data=f'sid_{inline_query.query.removeprefix("spoiler:")}',
)
]
]
),
)
]
2023-01-04 20:58:54 +02:00
except InvalidId:
results = []
2023-01-04 20:58:54 +02:00
2023-03-09 17:25:06 +02:00
await inline_query.answer(results=results)
2023-01-04 20:58:54 +02:00
return
2023-01-04 20:58:54 +02:00
if inline_query.chat_type in [ChatType.CHANNEL]:
2022-10-26 15:54:55 +03:00
await inline_query.answer(
2023-03-09 17:25:06 +02:00
results=[
InlineQueryResultArticle(
title=locale(
"title", "inline", "not_pm", locale=inline_query.from_user
),
input_message_content=InputTextMessageContent(
locale(
"message_content",
"inline",
"not_pm",
locale=inline_query.from_user,
)
),
description=locale(
"description", "inline", "not_pm", locale=inline_query.from_user
),
2022-10-26 15:54:55 +03:00
)
]
)
return
2023-01-07 11:20:58 +02:00
results_forbidden = [
InlineQueryResultArticle(
title=locale("title", "inline", "forbidden", locale=inline_query.from_user),
input_message_content=InputTextMessageContent(
2023-03-09 17:25:06 +02:00
locale(
"message_content",
"inline",
"forbidden",
locale=inline_query.from_user,
)
),
description=locale(
"description", "inline", "forbidden", locale=inline_query.from_user
2023-01-07 11:20:58 +02:00
),
)
]
2022-12-16 14:19:50 +02:00
try:
holo_user = HoloUser(inline_query.from_user)
except (UserNotFoundError, UserInvalidError):
2023-03-09 17:25:06 +02:00
logWrite(
f"Could not find application of {inline_query.from_user.id}, ignoring inline query",
debug=True,
2023-01-07 11:20:58 +02:00
)
2023-03-09 17:25:06 +02:00
await inline_query.answer(results=results_forbidden)
2023-01-07 11:20:58 +02:00
return
2023-03-09 17:25:06 +02:00
if path.exists(path.join(configGet("cache", "locations"), "group_members")) and (
inline_query.from_user.id
not in jsonLoad(path.join(configGet("cache", "locations"), "group_members"))
):
if path.exists(path.join(configGet("cache", "locations"), "admins")) and (
inline_query.from_user.id
not in jsonLoad(path.join(configGet("cache", "locations"), "admins"))
):
logWrite(
f"{inline_query.from_user.id} is not an admin and not in members group, ignoring inline query",
debug=True,
2023-01-09 13:21:21 +02:00
)
2023-03-09 17:25:06 +02:00
await inline_query.answer(results=results_forbidden)
2023-01-09 13:21:21 +02:00
return
2022-12-16 14:19:50 +02:00
if holo_user.application_approved() or (await isAnAdmin(holo_user.id) is True):
2023-03-09 17:25:06 +02:00
max_results = (
configGet("inline_preview_count") if inline_query.query != "" else 200
)
2022-12-18 00:12:15 +02:00
2022-10-26 15:54:55 +03:00
list_of_users = []
2023-03-09 17:25:06 +02:00
async for m in app.get_chat_members(
configGet("users", "groups"),
limit=max_results,
filter=ChatMembersFilter.SEARCH,
query=inline_query.query,
):
2022-10-26 15:54:55 +03:00
list_of_users.append(m)
for match in list_of_users:
2022-12-16 14:19:50 +02:00
application = col_applications.find_one({"user": match.user.id})
if application is None:
2022-10-27 12:40:15 +03:00
continue
2022-12-16 14:19:50 +02:00
application_content = []
i = 1
2023-03-09 17:25:06 +02:00
for question in application["application"]:
2022-12-16 14:19:50 +02:00
if i == 2:
2023-03-09 17:25:06 +02:00
age = relativedelta(datetime.now(), application["application"]["2"])
application_content.append(
f"{locale(f'question{i}', 'message', 'question_titles', locale=inline_query.from_user)} {application['application']['2'].strftime('%d.%m.%Y')} ({age.years} р.)"
)
2022-12-16 14:19:50 +02:00
elif i == 3:
2023-03-09 17:25:06 +02:00
if application["application"]["3"]["countryCode"] == "UA":
application_content.append(
f"{locale(f'question{i}', 'message', 'question_titles', locale=inline_query.from_user)} {application['application']['3']['name']}"
)
2022-12-16 14:19:50 +02:00
else:
2023-03-09 17:25:06 +02:00
application_content.append(
f"{locale(f'question{i}', 'message', 'question_titles', locale=inline_query.from_user)} {application['application']['3']['name']} ({application['application']['3']['adminName1']}, {application['application']['3']['countryName']})"
)
2022-12-16 14:19:50 +02:00
else:
2023-03-09 17:25:06 +02:00
application_content.append(
f"{locale(f'question{i}', 'message', 'question_titles', locale=inline_query.from_user)} {application['application'][question]}"
)
2022-12-16 14:19:50 +02:00
i += 1
2022-10-27 12:40:15 +03:00
if match.user.photo != None:
try:
2023-03-09 17:25:06 +02:00
if not path.exists(
f'{configGet("cache", "locations")}{sep}avatars{sep}{match.user.photo.big_file_id}'
):
print(
f'Downloaded avatar {match.user.photo.big_file_id} of {match.user.id} and uploaded to {configGet("api")}/?avatar_id={match.user.photo.big_file_id}',
flush=True,
)
await app.download_media(
match.user.photo.big_file_id,
file_name=f'{configGet("cache", "locations")}{sep}avatars{sep}{match.user.photo.big_file_id}',
)
2022-10-27 12:40:15 +03:00
results.append(
InlineQueryResultArticle(
title=str(match.user.first_name),
input_message_content=InputTextMessageContent(
2023-03-09 17:25:06 +02:00
locale(
"message_content",
"inline",
"user",
locale=inline_query.from_user,
).format(
match.user.first_name,
match.user.username,
"\n".join(application_content),
)
2022-10-27 12:40:15 +03:00
),
2023-03-09 17:25:06 +02:00
description=locale(
"description",
"inline",
"user",
locale=inline_query.from_user,
).format(match.user.first_name, match.user.username),
thumb_url=f'{configGet("api")}/?avatar_id={match.user.photo.big_file_id}',
2022-10-27 12:40:15 +03:00
)
)
except ValueError:
results.append(
InlineQueryResultArticle(
title=str(match.user.first_name),
input_message_content=InputTextMessageContent(
2023-03-09 17:25:06 +02:00
locale(
"message_content",
"inline",
"user",
locale=inline_query.from_user,
).format(
match.user.first_name,
match.user.username,
"\n".join(application_content),
)
2022-10-27 12:40:15 +03:00
),
2023-03-09 17:25:06 +02:00
description=locale(
"description",
"inline",
"user",
locale=inline_query.from_user,
).format(match.user.first_name, match.user.username),
2022-10-27 12:40:15 +03:00
)
)
except FileNotFoundError:
results.append(
InlineQueryResultArticle(
title=str(match.user.first_name),
input_message_content=InputTextMessageContent(
2023-03-09 17:25:06 +02:00
locale(
"message_content",
"inline",
"user",
locale=inline_query.from_user,
).format(
match.user.first_name,
match.user.username,
"\n".join(application_content),
)
),
2023-03-09 17:25:06 +02:00
description=locale(
"description",
"inline",
"user",
locale=inline_query.from_user,
).format(match.user.first_name, match.user.username),
)
)
2022-10-27 12:40:15 +03:00
else:
2022-10-26 15:54:55 +03:00
results.append(
InlineQueryResultArticle(
title=str(match.user.first_name),
input_message_content=InputTextMessageContent(
2023-03-09 17:25:06 +02:00
locale(
"message_content",
"inline",
"user",
locale=inline_query.from_user,
).format(
match.user.first_name,
match.user.username,
"\n".join(application_content),
)
2022-10-26 15:54:55 +03:00
),
2023-03-09 17:25:06 +02:00
description=locale(
"description",
"inline",
"user",
locale=inline_query.from_user,
).format(match.user.first_name, match.user.username),
2022-10-26 15:54:55 +03:00
)
)
2023-03-09 17:25:06 +02:00
await inline_query.answer(results=results, cache_time=10, is_personal=True)