randydev commited on
Commit
5fdfcce
·
verified ·
1 Parent(s): 19f6e8f
Files changed (1) hide show
  1. chatbot/plugins/chat.py +70 -84
chatbot/plugins/chat.py CHANGED
@@ -7,46 +7,36 @@
7
  # This project is proprietary software. Unauthorized redistribution is prohibited.
8
 
9
  import asyncio
10
- import random
11
- import os
12
  import base64
 
 
 
13
  import re
14
  import uuid
15
- import requests
16
- import json
17
- import cohere # type: ignore
18
  from datetime import datetime as dt, timedelta
19
-
20
- from gpytranslate import SyncTranslator
21
  from io import BytesIO
22
  from typing import List
23
 
 
 
 
 
 
 
 
24
  from PIL import Image
25
  from pyrogram import *
26
- from pyrogram import enums
27
- from pyrogram import Client, filters
28
  from pyrogram.errors import *
 
 
 
 
 
29
  from config import *
30
- from database import users_collection, db
31
  from logger import LOGS
32
 
33
- from pyrogram.types import (
34
- InlineKeyboardButton,
35
- InlineKeyboardMarkup,
36
- InlineQueryResultArticle,
37
- InputTextMessageContent,
38
- InputMediaPhoto,
39
- InlineQuery,
40
- Message,
41
- InlineQueryResultPhoto
42
- )
43
-
44
- from akenoai import *
45
- from akenoai.types import DifferentAPIDefault
46
-
47
- from google import genai
48
- from google.genai import types as ty
49
-
50
  trans = SyncTranslator()
51
  js = AkenoXJs(DifferentAPIDefault()).connect()
52
 
@@ -269,72 +259,63 @@ async def coheresearch(client, callback):
269
  ])
270
  await callback.edit_message_reply_markup(reply_markup=InlineKeyboardMarkup(keyboard))
271
 
272
-
273
- def inline_error(inline_text, **args):
274
- answers = [
275
  InlineQueryResultArticle(
276
- title="Error!",
277
- thumb_url="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg",
278
  input_message_content=InputTextMessageContent(
279
- message_text=inline_text,
280
- disable_web_page_preview=True
281
- ),
282
- **args
283
  )
284
  ]
285
- return answers
286
 
287
- @Client.on_inline_query()
288
- async def inline_search(client, inline_query: InlineQuery):
 
289
  search_query = inline_query.query.strip()
290
  if not search_query:
291
  return
292
- if search_query:
293
- try:
294
- response = requests.get(
295
- 'https://www.googleapis.com/customsearch/v1',
296
- params={
297
- "key": str(GOOGLE_API_SEARCH),
298
- "cx": "8386c816e6fff4b2a",
299
- "q": search_query,
300
- "searchType": "image"
301
- }
302
- )
303
- response_json = response.json()
304
- if 'items' in response_json:
305
- results = []
306
- for i, item in enumerate(response_json['items']):
307
- title = item['title']
308
- description = item['image']['contextLink']
309
- url = item['link']
310
- result = InlineQueryResultPhoto(
311
- photo_url=url,
312
- id=str(i),
313
- title=title,
314
- description=description,
315
- thumb_url=url,
316
- input_message_content=InputTextMessageContent(
317
- message_text=f"**Title:** {title}\n\n**Description:** {description}"
318
- ),
319
- reply_markup=InlineKeyboardMarkup(
320
- [[InlineKeyboardButton(text="Open Link", url=url)]]
321
- ),
322
- )
323
- results.append(result)
324
- await client.answer_inline_query(inline_query.id, results=results, cache_time=10)
325
- else:
326
- no_found = inline_error("No found result.")
327
- await client.answer_inline_query(
328
- inline_query.id,
329
- results=no_found
330
  )
331
- except Exception as e:
332
- LOGS.error(f"Error Inline: {type(e).__name__}")
333
- error_ = inline_error(f"Error try again.")
334
- await client.answer_inline_query(
335
- inline_query.id,
336
- results=error_
337
- )
 
 
338
 
339
  @Client.on_callback_query(filters.regex("^deeps_(on|off)_([a-f0-9]{8})$"))
340
  async def deepsearchmode(client, callback):
@@ -1409,6 +1390,7 @@ async def chatbot_talk(client: Client, message: Message):
1409
  {"$set": {"prompt_image": user_result["prompt"]}},
1410
  upsert=True
1411
  )
 
1412
  await message.reply_photo(
1413
  photo="loading.jpg",
1414
  caption="Are you sure you want to prompt this Image generate?",
@@ -1501,6 +1483,8 @@ async def chatbot_talk(client: Client, message: Message):
1501
  )
1502
  backup_chat.append({"role": "model", "parts": [{"text": results}]})
1503
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
 
 
1504
  return
1505
 
1506
  if check_is_system.get("is_system", False):
@@ -1583,6 +1567,7 @@ async def chatbot_talk(client: Client, message: Message):
1583
  )
1584
  backup_chat.append({"role": "model", "parts": [{"text": test_result}]})
1585
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
 
1586
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
1587
  return
1588
 
@@ -1661,6 +1646,7 @@ async def chatbot_talk(client: Client, message: Message):
1661
  )
1662
  backup_chat.append({"role": "model", "parts": [{"text": output}]})
1663
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
 
1664
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
1665
  return
1666
 
 
7
  # This project is proprietary software. Unauthorized redistribution is prohibited.
8
 
9
  import asyncio
 
 
10
  import base64
11
+ import json
12
+ import os
13
+ import random
14
  import re
15
  import uuid
 
 
 
16
  from datetime import datetime as dt, timedelta
 
 
17
  from io import BytesIO
18
  from typing import List
19
 
20
+ import cohere
21
+ import requests
22
+ from akenoai import *
23
+ from akenoai.types import DifferentAPIDefault
24
+ from google import genai
25
+ from google.genai import types as ty
26
+ from gpytranslate import SyncTranslator
27
  from PIL import Image
28
  from pyrogram import *
29
+ from pyrogram import Client, enums, filters
 
30
  from pyrogram.errors import *
31
+ from pyrogram.types import (InlineKeyboardButton, InlineKeyboardMarkup,
32
+ InlineQuery, InlineQueryResultArticle,
33
+ InlineQueryResultPhoto, InputMediaPhoto,
34
+ InputTextMessageContent, Message)
35
+
36
  from config import *
37
+ from database import db, users_collection
38
  from logger import LOGS
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  trans = SyncTranslator()
41
  js = AkenoXJs(DifferentAPIDefault()).connect()
42
 
 
259
  ])
260
  await callback.edit_message_reply_markup(reply_markup=InlineKeyboardMarkup(keyboard))
261
 
262
+ def inline_error(text: str):
263
+ return [
 
264
  InlineQueryResultArticle(
265
+ title="Error",
 
266
  input_message_content=InputTextMessageContent(
267
+ message_text=f"⚠️ {text}"
268
+ )
 
 
269
  )
270
  ]
 
271
 
272
+ @Client.on_inline_query(filters.regex(".*"))
273
+ async def inline_search(client: Client, inline_query: InlineQuery):
274
+ GOOGLE_CX = "8386c816e6fff4b2a"
275
  search_query = inline_query.query.strip()
276
  if not search_query:
277
  return
278
+ try:
279
+ response = requests.get(
280
+ "https://www.googleapis.com/customsearch/v1",
281
+ params={
282
+ "key": GOOGLE_API_SEARCH,
283
+ "cx": GOOGLE_CX,
284
+ "q": search_query,
285
+ "searchType": "image"
286
+ }
287
+ )
288
+ response_json = response.json()
289
+
290
+ if "items" in response_json:
291
+ results = []
292
+ for i, item in enumerate(response_json["items"]):
293
+ title = item.get("title", "No Title")
294
+ description = item["image"].get("contextLink", "No Link")
295
+ url = item.get("link")
296
+
297
+ result = InlineQueryResultPhoto(
298
+ photo_url=url,
299
+ id=str(i),
300
+ title=title,
301
+ description=description,
302
+ thumb_url=url,
303
+ input_message_content=InputTextMessageContent(
304
+ message_text=f"**Title:** {title}\n\n**Source:** {description}"
305
+ ),
306
+ reply_markup=InlineKeyboardMarkup(
307
+ [[InlineKeyboardButton("🔗 Open Link", url=url)]]
308
+ ),
 
 
 
 
 
 
 
309
  )
310
+ results.append(result)
311
+
312
+ await inline_query.answer(results, cache_time=10)
313
+
314
+ else:
315
+ await inline_query.answer(inline_error("No result found."), cache_time=1)
316
+ except Exception as e:
317
+ await inline_query.answer(inline_error("Internal error. Try again later."), cache_time=1)
318
+ print(f"[INLINE ERROR] {type(e).__name__}: {e}")
319
 
320
  @Client.on_callback_query(filters.regex("^deeps_(on|off)_([a-f0-9]{8})$"))
321
  async def deepsearchmode(client, callback):
 
1390
  {"$set": {"prompt_image": user_result["prompt"]}},
1391
  upsert=True
1392
  )
1393
+ LOGS.info(f"Response Image Generate: {message.from_user.first_name} | {message.from_user.id}")
1394
  await message.reply_photo(
1395
  photo="loading.jpg",
1396
  caption="Are you sure you want to prompt this Image generate?",
 
1483
  )
1484
  backup_chat.append({"role": "model", "parts": [{"text": results}]})
1485
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
1486
+ LOGS.info(f"Response Deep Search: {message.from_user.first_name} | {message.from_user.id}")
1487
+ await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
1488
  return
1489
 
1490
  if check_is_system.get("is_system", False):
 
1567
  )
1568
  backup_chat.append({"role": "model", "parts": [{"text": test_result}]})
1569
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
1570
+ LOGS.info(f"Response Gemini System: {message.from_user.first_name} | {message.from_user.id}")
1571
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
1572
  return
1573
 
 
1646
  )
1647
  backup_chat.append({"role": "model", "parts": [{"text": output}]})
1648
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
1649
+ LOGS.info(f"Response Gemini: {message.from_user.first_name} | {message.from_user.id}")
1650
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
1651
  return
1652