File size: 5,464 Bytes
732fabe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b2672
732fabe
 
4f931f3
732fabe
1fc4fa4
 
732fabe
 
 
 
 
 
 
 
 
 
 
4bdfc4c
732fabe
4bdfc4c
 
 
732fabe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fc4fa4
afd5448
732fabe
 
 
 
 
 
 
 
4f931f3
732fabe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f931f3
732fabe
 
2ce2a62
732fabe
f27d1f9
 
 
 
 
 
 
1ce619d
f27d1f9
 
 
732fabe
 
 
 
4f931f3
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2020-2024 (c) Randy W @xtdevs, @xtsea
#
# from : https://github.com/TeamKillerX
# Channel : @RendyProjects
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import requests
import time
import json
import asyncio
import io
import os
import re
from PIL import Image

from pyrogram import *
from pyrogram import enums
from pyrogram import Client, filters
from pyrogram.types import *
from pyrogram.errors import *
from RyuzakiLib import FaceAI, FullStackDev, GeminiLatest, RendyDevChat
from config import *

from database import db
from logger import LOGS
import datetime
from chatbot import send_log
from openai import AsyncOpenAI as openai
import akenoai.openai as akeno
import akenoai.logger as akeno_log

from . import force_sub

@Client.on_message(
    ~filters.scheduled
    & filters.command(["onchat"])
    & ~filters.forwarded
)
async def addchatbot_user(client: Client, message: Message):
    await db.add_chatbot(message.chat.id, client.me.id)
    await message.reply_text("Added chatbot user")

BASE_PROMPT = f"""
You are my name Akeno AI and python language powered by @xtdevs on telegram support and language models GPT-5-ULTRA

- off topic free questions
- Code python clone user telegram available : [HERE LINK](https://github.com/TeamKillerX/akenoai-lib/blob/main/examples/clone.py) by credits @xtdevs
- Support pyrogram and telethon not python-telegram-bot

{datetime.datetime.now()}
"""

@Client.on_message(
    ~filters.scheduled
    & filters.command(["offchat"])
    & ~filters.forwarded
)
async def rmchatbot_user(client: Client, message: Message):
    await db.remove_chatbot(message.chat.id)
    await message.reply_text("ok stopped GPT")

@Client.on_message(
    filters.incoming
    & (
        filters.text
        | filters.regex(r"\b(Randy|Rendi)\b(.*)", flags=re.IGNORECASE)
    )
    & (filters.private | filters.group)
    & ~filters.bot
    & ~filters.via_bot
    & ~filters.forwarded,
    group=2,
)
@force_sub
@akeno_log.log_performance
async def chatbot_talk(client: Client, message: Message):
    chat_user = await db.get_chatbot(message.chat.id)
    if not chat_user:
        return
    if message.reply_to_message and message.reply_to_message.from_user:
        if message.reply_to_message.from_user.id != client.me.id:
            return
    if message.text:
        pro = await message.reply("Processing your request...", quote=True)
        await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
        await asyncio.sleep(1.5)
        query = message.text.strip()
        match = re.search(r"\b(Randy|Rendi)\b(.*)", query, flags=re.IGNORECASE)
        if match:
            rest_of_sentence = match.group(2).strip()
            query_base = rest_of_sentence if rest_of_sentence else query
        else:
            query_base = query
        parts = query.split(maxsplit=1)
        command = parts[0].lower()
        pic_query = parts[1].strip() if len(parts) > 1 else ""
        try:
            backup_chat = await db._get_openai_chat_from_db(message.from_user.id)
            backup_chat.append({"role": "system", "content": BASE_PROMPT})
            backup_chat.append({"role": "user", "content": query_base})
            response = await akeno.OpenAI.run(
                ...,
                openai_meta=openai,
                model="gpt-4o-mini-2024-07-18",
                messages=backup_chat
            )
            output = response
            if len(output) > 4096:
                with open("chat.txt", "w+", encoding="utf8") as out_file:
                    out_file.write(output)
                await message.reply_document(
                    document="chat.txt",
                    disable_notification=True
                )
                await pro.delete()
                os.remove("chat.txt")
            else:
                await pro.edit_text(output, disable_web_page_preview=True)
            backup_chat.append({"role": "assistant", "content": output})
            user_detail = (
                f"**Akeno GPT Bot**\n"
                f"**User Username**: @{message.from_user.username if message.from_user else None}\n"
                f"**User ID**: `{message.from_user.id}`\n"
                f"**Chat Title**: `{message.chat.title if message.chat else None}`\n"
                f"**Chat ID**: `{message.chat.id if message.chat else None}`\n"
            )
            response_log = await send_log(user_detail)
            if response_log is None:
                LOGS.warning("Error response")
            LOGS.info(response_log)
            await db._update_openai_chat_in_db(message.from_user.id, backup_chat)
            await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
            return
        except Exception as e:
            return await pro.edit_text(f"Error: {e}")