forked from CyniteOfficial/Auto-Filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline.py
103 lines (88 loc) · 3.85 KB
/
inline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import logging
from pyrogram import Client, emoji, filters
from pyrogram.errors.exceptions.bad_request_400 import QueryIdInvalid
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultCachedDocument
from database.ia_filterdb import get_search_results
from utils import is_subscribed, get_size
from info import CACHE_TIME, AUTH_USERS, AUTH_CHANNEL, CUSTOM_FILE_CAPTION
logger = logging.getLogger(__name__)
cache_time = 0 if AUTH_USERS or AUTH_CHANNEL else CACHE_TIME
@Client.on_inline_query(filters.user(AUTH_USERS) if AUTH_USERS else None)
async def answer(bot, query):
"""Show search results for given inline query"""
if AUTH_CHANNEL and not await is_subscribed(bot, query):
await query.answer(results=[],
cache_time=0,
switch_pm_text='You have to subscribe my channel to use the bot',
switch_pm_parameter="subscribe")
return
results = []
if '|' in query.query:
string, file_type = query.query.split('|', maxsplit=1)
string = string.strip()
file_type = file_type.strip().lower()
else:
string = query.query.strip()
file_type = None
offset = int(query.offset or 0)
reply_markup = get_reply_markup(query=string)
files, next_offset, total = await get_search_results(string,
file_type=file_type,
max_results=10,
offset=offset)
for file in files:
title=file.file_name
size=get_size(file.file_size)
f_caption=file.caption
if CUSTOM_FILE_CAPTION:
try:
f_caption=CUSTOM_FILE_CAPTION.format(file_name=title, file_size=size, file_caption=f_caption)
except Exception as e:
logger.exception(e)
f_caption=f_caption
if f_caption is None:
f_caption = f"{file.file_name}"
results.append(
InlineQueryResultCachedDocument(
title=file.file_name,
file_id=file.file_id,
caption=f_caption,
description=f'Size: {get_size(file.file_size)}\nType: {file.file_type}',
reply_markup=reply_markup))
if results:
switch_pm_text = f"{emoji.FILE_FOLDER} Results - {total}"
if string:
switch_pm_text += f" for {string}"
try:
await query.answer(results=results,
is_personal = True,
cache_time=cache_time,
switch_pm_text=switch_pm_text,
switch_pm_parameter="start",
next_offset=str(next_offset))
except QueryIdInvalid:
pass
except Exception as e:
logging.exception(str(e))
await query.answer(results=[], is_personal=True,
cache_time=cache_time,
switch_pm_text=str(e)[:63],
switch_pm_parameter="error")
else:
switch_pm_text = f'{emoji.CROSS_MARK} No results'
if string:
switch_pm_text += f' for "{string}"'
await query.answer(results=[],
is_personal = True,
cache_time=cache_time,
switch_pm_text=switch_pm_text,
switch_pm_parameter="okay")
def get_reply_markup(query):
buttons = [
[
InlineKeyboardButton('♻️ 𝗦𝗲𝗮𝗿𝗰𝗵 𝗔𝗴𝗮𝗶𝗻 ♻️', switch_inline_query_current_chat=query)
],[
InlineKeyboardButton('♥️ 𝗖𝗵𝗮𝗻𝗻𝗲𝗹 ♥️', url='https://t.me/+veUIdIW2CQ5mOGU5')
]
]
return InlineKeyboardMarkup(buttons)