-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_BotInlineQuery.py
98 lines (91 loc) · 5.44 KB
/
_BotInlineQuery.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
#********************************Inline Query (This will run when user type: @botusername <query>)****************************************************
from telegram import Update
from telegram.ext import ContextTypes
from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineQueryResultPhoto #, InlineQueryResultDocument
from _reqOpenAI import send_req_openai_chat, send_req_openai_image
#? Inline Query Handler
from uuid import uuid4
async def inline_query_initial(update: Update, context: ContextTypes.DEFAULT_TYPE): #? inline_query_initial -- with end of the query model ("\\")
query = update.inline_query.query
if query != "":
if (len(query) <= 230): # This is the no. of characters that user can write in inline query
query = query.strip()
if query.endswith('\\\\'):
if query.startswith('i-'):
# update.inline_query.answer(results=[InlineQueryResultArticle(id=str(uuid4()),title="ChatAI",description="Generating!🔄, Please wait.....",input_message_content=InputTextMessageContent("Not Valid!"))])
await inline_query_image(update, query.lstrip("i-").rstrip(('\\\\')))
else:
# update.inline_query.answer(results=[InlineQueryResultArticle(id=str(uuid4()),title="ChatAI",description="Generating!🔄, Please wait.....",input_message_content=InputTextMessageContent("Not Valid!"))])
await inline_query_text(update, query.rstrip("\\\\"))
else:
await update.inline_query.answer(results=[InlineQueryResultArticle(id=str(uuid4()),title="ChatAI",description="💡Put \'\\\\\' at the end to start Generarting.",input_message_content=InputTextMessageContent("Invalid Input!"))])
else:
await update.inline_query.answer(results=[InlineQueryResultArticle(id=str(uuid4()),title="ChatAI",description=f"⚠ Query must not exceed 230 characters.",input_message_content=InputTextMessageContent("Invalid Input!"))])
else:
await update.inline_query.answer(results=[InlineQueryResultArticle(id=str(uuid4()),title="ChatAI",description="💡Start writing to Generate.\n• End your query with \'\\\\\'\n• Image generation: start query with 'i-' flag.",input_message_content=InputTextMessageContent("Invalid Input!"))])
#* This code is Efficient to provide Real Time "Text Completion",
#* though becuase to avoid unnessary (tons) of OpenAI API requests,
#* and also as it produces high amount of machine load (which if not be handled correctly by the machine, it can produce unexpected results),
#* hence to avoid this I made a wrapper function inline_query_initial(),
#* which will allow this function to run only if the user put '\\' at the end of its query.
async def inline_query_text(update: Update, query) -> None:
#* Send OpenAI Request
chat_id = str(update.inline_query.from_user.id)
try:
response, prob_file, num_openai_req = await send_req_openai_chat(update, query, chat_id, True)
response = query + '\n\n' + response
results = [
InlineQueryResultArticle( #* https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinebot.py
id = str(uuid4()),
title = "ChatAI",
description = f"🟢Generated! Tap me. \n({query[:50]}..)",
input_message_content = InputTextMessageContent(response)
# reply_markup = InlineKeyboardMarkup(inline_keyboard) # You can generate an inline keyboard too
)
]
except:
results = [
InlineQueryResultArticle(
id = str(uuid4()),
title = "ChatAI",
description = "⚠ OOPS! ERROR OCCURED.",
input_message_content = InputTextMessageContent("⚠ Oops! Error Occured.😶\nTry again!")
)
]
#* Display Pop-up
await update.inline_query.answer(results=results)
async def inline_query_image (update: Update, query) -> None:
#* Send OpenAI Request
chat_id = str(update.inline_query.from_user.id)
try:
response, limit = await send_req_openai_image(update, query, chat_id, True)
if (not limit):
results = [
InlineQueryResultPhoto( #* https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinebot.py
id = str(uuid4()),
title = "ChatAI",
description = f"🟢Generated! Tap me. \n({query[:50]}..)",
photo_url = response,
thumb_url = response
)
]
else:
results = [
InlineQueryResultArticle(
id = str(uuid4()),
title = "ChatAI",
description = "⚠ YOUR MAX LIMIT EXHUASTED!",
input_message_content = InputTextMessageContent("⚠ Your Max Limit Exhuasted!\nTry again Tommorrow")
)
]
except:
results = [
InlineQueryResultArticle(
id = str(uuid4()),
title = "ChatAI",
description = "⚠ OOPS! ERROR OCCURED.",
input_message_content = InputTextMessageContent("⚠ Oops! Error Occured.😶\nTry again!")
)
]
#* Display Pop-up
await update.inline_query.answer(results=results)