Create chat.py
Browse files- chatbot/plugins/chat.py +125 -0
chatbot/plugins/chat.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
# Copyright 2020-2024 (c) Randy W @xtdevs, @xtsea
|
4 |
+
#
|
5 |
+
# from : https://github.com/TeamKillerX
|
6 |
+
# Channel : @RendyProjects
|
7 |
+
# This program is free software: you can redistribute it and/or modify
|
8 |
+
# it under the terms of the GNU Affero General Public License as published by
|
9 |
+
# the Free Software Foundation, either version 3 of the License, or
|
10 |
+
# (at your option) any later version.
|
11 |
+
#
|
12 |
+
# This program is distributed in the hope that it will be useful,
|
13 |
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
+
# GNU Affero General Public License for more details.
|
16 |
+
#
|
17 |
+
# You should have received a copy of the GNU Affero General Public License
|
18 |
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
19 |
+
|
20 |
+
import requests
|
21 |
+
import time
|
22 |
+
import json
|
23 |
+
import asyncio
|
24 |
+
import io
|
25 |
+
import os
|
26 |
+
import re
|
27 |
+
from PIL import Image
|
28 |
+
|
29 |
+
from pyrogram import *
|
30 |
+
from pyrogram import enums
|
31 |
+
from pyrogram import Client, filters
|
32 |
+
from pyrogram.types import *
|
33 |
+
from pyrogram.errors import *
|
34 |
+
from RyuzakiLib import FaceAI, FullStackDev, GeminiLatest, RendyDevChat
|
35 |
+
from config import *
|
36 |
+
|
37 |
+
from database import db
|
38 |
+
from logger import LOGS
|
39 |
+
import datetime
|
40 |
+
from openai import AsyncOpenAI as openai
|
41 |
+
import akenoai.openai as akeno
|
42 |
+
|
43 |
+
@Client.on_message(
|
44 |
+
~filters.scheduled
|
45 |
+
& filters.command(["onchat"])
|
46 |
+
& ~filters.forwarded
|
47 |
+
)
|
48 |
+
async def addchatbot_user(client: Client, message: Message):
|
49 |
+
await db.add_chatbot(message.chat.id, client.me.id)
|
50 |
+
await message.reply_text("Added chatbot user")
|
51 |
+
|
52 |
+
BASE_PROMPT = f"""
|
53 |
+
You are my name Akeno AI and python language powered by @xtdevs on telegram support and language models GPT-5-ULTRA
|
54 |
+
- off topic free questions
|
55 |
+
{datetime.datetime.now()}
|
56 |
+
"""
|
57 |
+
|
58 |
+
@Client.on_message(
|
59 |
+
~filters.scheduled
|
60 |
+
& filters.command(["offchat"])
|
61 |
+
& ~filters.forwarded
|
62 |
+
)
|
63 |
+
async def rmchatbot_user(client: Client, message: Message):
|
64 |
+
await db.remove_chatbot(message.chat.id)
|
65 |
+
await message.reply_text("ok stopped GPT")
|
66 |
+
|
67 |
+
@Client.on_message(
|
68 |
+
filters.incoming
|
69 |
+
& (
|
70 |
+
filters.text
|
71 |
+
| filters.regex(r"\b(Randy|Rendi)\b(.*)", flags=re.IGNORECASE)
|
72 |
+
)
|
73 |
+
& (filters.private | filters.group)
|
74 |
+
& ~filters.bot
|
75 |
+
& ~filters.via_bot
|
76 |
+
& ~filters.forwarded,
|
77 |
+
group=2,
|
78 |
+
)
|
79 |
+
async def chatbot_talk(client: Client, message: Message):
|
80 |
+
chat_user = await db.get_chatbot(message.chat.id)
|
81 |
+
if not chat_user:
|
82 |
+
return
|
83 |
+
if message.reply_to_message and message.reply_to_message.from_user:
|
84 |
+
if message.reply_to_message.from_user.id != client.me.id:
|
85 |
+
return
|
86 |
+
if message.text:
|
87 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
|
88 |
+
await asyncio.sleep(1.5)
|
89 |
+
query = message.text.strip()
|
90 |
+
match = re.search(r"\b(Randy|Rendi)\b(.*)", query, flags=re.IGNORECASE)
|
91 |
+
if match:
|
92 |
+
rest_of_sentence = match.group(2).strip()
|
93 |
+
query_base = rest_of_sentence if rest_of_sentence else query
|
94 |
+
else:
|
95 |
+
query_base = query
|
96 |
+
parts = query.split(maxsplit=1)
|
97 |
+
command = parts[0].lower()
|
98 |
+
pic_query = parts[1].strip() if len(parts) > 1 else ""
|
99 |
+
try:
|
100 |
+
backup_chat = await db._get_openai_chat_from_db(message.from_user.id)
|
101 |
+
backup_chat.append({"role": "system", "content": BASE_PROMPT})
|
102 |
+
backup_chat.append({"role": "user", "content": query_base})
|
103 |
+
response = await akeno.OpenAI.run(
|
104 |
+
...,
|
105 |
+
openai_meta=openai,
|
106 |
+
model="gpt-4o-mini-2024-07-18",
|
107 |
+
messages=backup_chat
|
108 |
+
)
|
109 |
+
output = response
|
110 |
+
if len(output) > 4096:
|
111 |
+
with open("chat.txt", "w+", encoding="utf8") as out_file:
|
112 |
+
out_file.write(output)
|
113 |
+
await message.reply_document(
|
114 |
+
document="chat.txt",
|
115 |
+
disable_notification=True
|
116 |
+
)
|
117 |
+
os.remove("chat.txt")
|
118 |
+
else:
|
119 |
+
await message.reply_text(output)
|
120 |
+
backup_chat.append({"role": "assistant", "content": output})
|
121 |
+
await db._update_openai_chat_in_db(message.from_user.id, backup_chat)
|
122 |
+
await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
|
123 |
+
return
|
124 |
+
except Exception as e:
|
125 |
+
return await message.reply_text(f"Error: {e}")
|