forked from yym68686/ChatGPT-Telegram-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.py
144 lines (134 loc) · 6.37 KB
/
AI.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
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
import re
import json
import threading
from md2tgmd import escape
from runasync import run_async
from config import API, NICK, COOKIES
from revChatGPT.V3 import Chatbot as GPT
from telegram.constants import ChatAction
from EdgeGPT import Chatbot as BingAI, ConversationStyle
class AIBot:
def __init__(self):
self.LastMessage_id = ''
self.mess = ''
self.bingcookie = COOKIES
if self.bingcookie:
try:
self.Bingbot = BingAI(cookies=json.loads(self.bingcookie))
except Exception as e:
print('\033[31m')
print("Bing 登陆失败!请更换 COOKIES")
print("error", e)
print('\033[0m')
self.bingcookie = None
if API:
self.ChatGPTbot = GPT(api_key=f"{API}")
self.botNick = NICK.lower() if NICK else None
self.botNicKLength = len(self.botNick) if self.botNick else 0
print("nick:", self.botNick)
async def getBing(self, message, update, context):
result = ''
prompt = ""
try:
# creative balanced precise
result = await self.Bingbot.ask(prompt=prompt + message, conversation_style=ConversationStyle.creative)
numMessages = result["item"]["throttling"]["numUserMessagesInConversation"]
maxNumMessages = result["item"]["throttling"]["maxNumUserMessagesInConversation"]
print(numMessages, "/", maxNumMessages, end="")
result = result["item"]["messages"][1]["text"]
if numMessages == maxNumMessages:
await self.Bingbot.reset()
except Exception as e:
print('\033[31m')
print("response_msg", result)
print("error", e)
print('\033[0m')
numMessages = 0
maxNumMessages = 0
result = "实在不好意思,我现在无法对此做出回应。 要不我们换个话题?"
await self.Bingbot.reset()
result = re.sub(r"\[\^\d+\^\]", '', result)
print(" BingAI", result)
if self.LastMessage_id == '':
message = await context.bot.send_message(
chat_id=update.message.chat_id,
text=escape(f"🤖️ Bing {numMessages} / {maxNumMessages} \n\n" + result),
parse_mode='MarkdownV2',
reply_to_message_id=update.message.message_id,
)
self.mess = f"🤖️ Bing {numMessages} / {maxNumMessages} \n\n" + result
if self.bingcookie and API:
self.LastMessage_id = message.message_id
else:
await context.bot.edit_message_text(chat_id=update.message.chat_id, message_id=self.LastMessage_id, text=escape(self.mess + f"\n\n\n🤖️ Bing {numMessages} / {maxNumMessages} \n\n" + result), parse_mode='MarkdownV2')
self.LastMessage_id = ''
self.mess = ''
async def resetBing(self):
await self.Bingbot.reset()
async def getChatGPT(self, message, update, context):
result = ''
try:
result = self.ChatGPTbot.ask(message)
except Exception as e:
print('\033[31m')
print("response_msg", result)
print("error", e)
print('\033[0m')
if "overloaded" in str(e):
result = "OpenAI 服务器过载。"
else:
result = "ChatGPT 出错啦。"
self.ChatGPTbot.reset()
print("ChatGPT", result)
if self.LastMessage_id == '':
message = await context.bot.send_message(
chat_id=update.message.chat_id,
text=escape("🤖️ ChatGPT3.5\n\n" + result),
parse_mode='MarkdownV2',
reply_to_message_id=update.message.message_id,
)
if self.bingcookie and API:
self.LastMessage_id = message.message_id
self.mess = "🤖️ ChatGPT3.5\n\n" + result
else:
await context.bot.edit_message_text(chat_id=update.message.chat_id, message_id=self.LastMessage_id, text=escape(self.mess + "\n\n\n🤖️ ChatGPT3.5\n\n" + result), parse_mode='MarkdownV2')
self.LastMessage_id = ''
self.mess = ''
async def getResult(self, update, context):
await context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
self.LastMessage_id = ''
print("\033[32m", update.effective_user.username, update.effective_user.id, update.message.text, "\033[0m")
chat_content = update.message.text if NICK is None else update.message.text[self.botNicKLength:].strip() if update.message.text[:self.botNicKLength].lower() == self.botNick else None
if self.bingcookie and chat_content:
_thread = threading.Thread(target=run_async, args=(self.getBing(chat_content, update, context),))
_thread.start()
if API and chat_content:
await self.getChatGPT(chat_content, update, context)
async def reset_chat(self, update, context):
if API:
self.ChatGPTbot.reset()
if self.bingcookie:
await self.resetBing()
await context.bot.send_message(
chat_id=update.message.chat_id,
text="重置成功!",
)
self.LastMessage_id = ''
self.mess = ''
async def en2zhtranslator(self, update, context):
prompt = "I want you to act as a chinese translator. I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in Chinese. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. My first sentence is \""
if len(context.args) > 0:
message = ' '.join(context.args)
chat_content = prompt + message + '"'
print("en2zh", message)
if API and message:
await self.getChatGPT(chat_content, update, context)
self.LastMessage_id = ''
self.mess = ''
else:
message = await context.bot.send_message(
chat_id=update.message.chat_id,
text="请在命令后面放入要翻译的文本。",
parse_mode='MarkdownV2',
reply_to_message_id=update.message.message_id,
)