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

120 lines
6.0 KiB
Python
Raw Normal View History

2022-10-26 15:54:55 +03:00
from datetime import datetime
from os import path, sep
from pyrogram.types import InlineQueryResultArticle, InputTextMessageContent
from pyrogram.enums.chat_type import ChatType
from pyrogram.enums.chat_members_filter import ChatMembersFilter
from dateutil.relativedelta import relativedelta
from app import app, isAnAdmin
from modules.utils import configGet, jsonLoad, locale
@app.on_inline_query()
async def inline_answer(client, inline_query):
if inline_query.chat_type in [ChatType.CHANNEL]:
2022-10-26 15:54:55 +03:00
await inline_query.answer(
results=[
InlineQueryResultArticle(
title=locale("title", "inline", "not_pm"),
input_message_content=InputTextMessageContent(
locale("message_content", "inline", "not_pm")
),
description=locale("description", "inline", "not_pm")
)
]
)
return
if (path.exists(f"{configGet('data', 'locations')}{sep}users{sep}{inline_query.from_user.id}.json") and jsonLoad(f"{configGet('data', 'locations')}{sep}users{sep}{inline_query.from_user.id}.json")["approved"]) or (await isAnAdmin(inline_query.from_user.id)):
list_of_users = []
async for m in app.get_chat_members(configGet("destination_group"), limit=configGet("inline_preview_count"), filter=ChatMembersFilter.SEARCH, query=inline_query.query):
2022-10-26 15:54:55 +03:00
list_of_users.append(m)
results = []
2022-12-04 12:27:59 +02:00
applications = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
2022-10-26 15:54:55 +03:00
for match in list_of_users:
try:
application_content = []
i = 1
2022-12-04 12:27:59 +02:00
for question in applications[str(match.user.id)]["application"]:
2022-10-26 15:54:55 +03:00
if i == 2:
2022-12-04 12:27:59 +02:00
age = relativedelta(datetime.now(), datetime.strptime(applications[str(match.user.id)]['application']['2'], '%d.%m.%Y'))
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {applications[str(match.user.id)]['application']['2']} ({age.years} р.)")
2022-10-26 15:54:55 +03:00
else:
2022-12-04 12:27:59 +02:00
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {applications[str(match.user.id)]['application'][question]}")
2022-10-26 15:54:55 +03:00
i += 1
2022-12-04 12:27:59 +02:00
except KeyError:
continue
2022-10-27 12:40:15 +03:00
except FileNotFoundError:
continue
except TypeError:
continue
if match.user.photo != None:
try:
2022-11-29 11:37: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(
locale("message_content", "inline", "user").format(match.user.first_name, match.user.username, "\n".join(application_content))
),
description=locale("description", "inline", "user").format(match.user.first_name, match.user.username),
2022-10-27 12:41:49 +03:00
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(
locale("message_content", "inline", "user").format(match.user.first_name, match.user.username, "\n".join(application_content))
),
description=locale("description", "inline", "user").format(match.user.first_name, match.user.username)
)
)
except FileNotFoundError:
results.append(
InlineQueryResultArticle(
title=str(match.user.first_name),
input_message_content=InputTextMessageContent(
locale("message_content", "inline", "user").format(match.user.first_name, match.user.username, "\n".join(application_content))
),
description=locale("description", "inline", "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(
locale("message_content", "inline", "user").format(match.user.first_name, match.user.username, "\n".join(application_content))
),
description=locale("description", "inline", "user").format(match.user.first_name, match.user.username)
)
)
await inline_query.answer(
results=results,
cache_time=10
)
else:
await inline_query.answer(
results=[
InlineQueryResultArticle(
title=locale("title", "inline", "forbidden"),
input_message_content=InputTextMessageContent(
locale("message_content", "inline", "forbidden")
),
description=locale("description", "inline", "forbidden")
)
]
)
return