Upload chat.py
Browse files- chatbot/plugins/chat.py +52 -36
chatbot/plugins/chat.py
CHANGED
@@ -113,6 +113,55 @@ async def cancel_(client, callback_query):
|
|
113 |
else:
|
114 |
await callback_query.edit_message_text("⚠️ No active task to cancel.")
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
async def handle_video(client, message, model_):
|
117 |
chat_id = message.chat.id
|
118 |
async def process_video():
|
@@ -129,7 +178,8 @@ async def handle_video(client, message, model_):
|
|
129 |
video_file_name = await message.download(file_name="newvideo.mp4")
|
130 |
caption = message.caption or "What's this?"
|
131 |
model = genai.GenerativeModel(model_name=model_)
|
132 |
-
|
|
|
133 |
ai_reply = await message.reply_text(
|
134 |
"Uploading file..",
|
135 |
reply_markup=InlineKeyboardMarkup(buttons)
|
@@ -285,41 +335,7 @@ async def chatbot_talk(client: Client, message: Message):
|
|
285 |
if message.reply_to_message.from_user.id != client.me.id:
|
286 |
return
|
287 |
if message.photo:
|
288 |
-
await client
|
289 |
-
await asyncio.sleep(1.5)
|
290 |
-
file_path = await message.download()
|
291 |
-
caption = message.caption or "What's this?"
|
292 |
-
x = GeminiLatest(api_keys=GOOGLE_API_KEY)
|
293 |
-
if client.me.is_premium:
|
294 |
-
ai_reply = await message.reply_text(f"{custom_loading}Processing...")
|
295 |
-
else:
|
296 |
-
ai_reply = await message.reply_text(f"Processing...")
|
297 |
-
try:
|
298 |
-
await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
|
299 |
-
await asyncio.sleep(1.5)
|
300 |
-
backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
|
301 |
-
backup_chat.append({"role": "user", "parts": [{"text": caption}]})
|
302 |
-
response_reads = x.get_response_image(caption, file_path)
|
303 |
-
if len(response_reads) > 4096:
|
304 |
-
with open("chat.txt", "w+", encoding="utf8") as out_file:
|
305 |
-
out_file.write(response_reads)
|
306 |
-
await message.reply_document(
|
307 |
-
document="chat.txt",
|
308 |
-
disable_notification=True
|
309 |
-
)
|
310 |
-
await ai_reply.delete()
|
311 |
-
os.remove("chat.txt")
|
312 |
-
else:
|
313 |
-
await ai_reply.edit_text(response_reads)
|
314 |
-
backup_chat.append({"role": "model", "parts": [{"text": response_reads}]})
|
315 |
-
await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
|
316 |
-
await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
|
317 |
-
os.remove(file_path)
|
318 |
-
return
|
319 |
-
except InvalidArgument as e:
|
320 |
-
return await ai_reply.edit_text(f"Error: {e}")
|
321 |
-
except Exception as e:
|
322 |
-
return await ai_reply.edit_text(f"Error: {e}")
|
323 |
if message.audio or message.voice:
|
324 |
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_AUDIO)
|
325 |
await asyncio.sleep(1.5)
|
|
|
113 |
else:
|
114 |
await callback_query.edit_message_text("⚠️ No active task to cancel.")
|
115 |
|
116 |
+
async def handle_photo(client, message):
|
117 |
+
chat_id = message.chat.id
|
118 |
+
async def process_photo():
|
119 |
+
buttons = [
|
120 |
+
[
|
121 |
+
InlineKeyboardButton(
|
122 |
+
text="Cancel",
|
123 |
+
callback_data="cancels"
|
124 |
+
)
|
125 |
+
],
|
126 |
+
]
|
127 |
+
try:
|
128 |
+
spam_chats.append(chat_id)
|
129 |
+
file_path = await message.download()
|
130 |
+
caption = message.caption or "What's this?"
|
131 |
+
x = GeminiLatest(api_keys=GOOGLE_API_KEY)
|
132 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
|
133 |
+
await asyncio.sleep(1.5)
|
134 |
+
ai_reply = await message.reply_text(
|
135 |
+
"Uploading file..",
|
136 |
+
reply_markup=InlineKeyboardMarkup(buttons)
|
137 |
+
)
|
138 |
+
backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
|
139 |
+
backup_chat.append({"role": "user", "parts": [{"text": caption}]})
|
140 |
+
response_reads = x.get_response_image(caption, file_path)
|
141 |
+
if len(response_reads) > 4096:
|
142 |
+
with open("chat.txt", "w+", encoding="utf8") as out_file:
|
143 |
+
out_file.write(response_reads)
|
144 |
+
await message.reply_document(
|
145 |
+
document="chat.txt",
|
146 |
+
disable_notification=True
|
147 |
+
)
|
148 |
+
await ai_reply.delete()
|
149 |
+
os.remove("chat.txt")
|
150 |
+
else:
|
151 |
+
await ai_reply.edit_text(response_reads)
|
152 |
+
backup_chat.append({"role": "model", "parts": [{"text": response_reads}]})
|
153 |
+
await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
|
154 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
|
155 |
+
os.remove(file_path)
|
156 |
+
return
|
157 |
+
except asyncio.CancelledError:
|
158 |
+
await ai_reply.edit_text("⚠️ Photo processing was canceled.")
|
159 |
+
finally:
|
160 |
+
spam_chats.remove(chat_id)
|
161 |
+
task_manager.cancel_task(chat_id)
|
162 |
+
|
163 |
+
await task_manager.add_task(chat_id, process_photo())
|
164 |
+
|
165 |
async def handle_video(client, message, model_):
|
166 |
chat_id = message.chat.id
|
167 |
async def process_video():
|
|
|
178 |
video_file_name = await message.download(file_name="newvideo.mp4")
|
179 |
caption = message.caption or "What's this?"
|
180 |
model = genai.GenerativeModel(model_name=model_)
|
181 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
|
182 |
+
await asyncio.sleep(1.5)
|
183 |
ai_reply = await message.reply_text(
|
184 |
"Uploading file..",
|
185 |
reply_markup=InlineKeyboardMarkup(buttons)
|
|
|
335 |
if message.reply_to_message.from_user.id != client.me.id:
|
336 |
return
|
337 |
if message.photo:
|
338 |
+
await handle_photo(client, message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
if message.audio or message.voice:
|
340 |
await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_AUDIO)
|
341 |
await asyncio.sleep(1.5)
|