From deb5c29561d01b69b5e839ffdc77e25c5e56fea6 Mon Sep 17 00:00:00 2001 From: yym68686 Date: Sat, 22 Apr 2023 16:54:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=8B=B1=E8=AF=91=E4=B8=AD?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AI.py | 43 +++++++++++++++++++++++++++++++++++-------- bot.py | 2 ++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/AI.py b/AI.py index d15cb6cf..7d6f90e9 100644 --- a/AI.py +++ b/AI.py @@ -1,7 +1,7 @@ import re import json -from md2tgmd import escape import threading +from md2tgmd import escape from runasync import run_async from config import API, NICK, COOKIES from revChatGPT.V3 import Chatbot as GPT @@ -13,8 +13,17 @@ def __init__(self): self.LastMessage_id = '' self.mess = '' - if COOKIES: - self.Bingbot = BingAI(cookies=json.loads(COOKIES)) + 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}") @@ -53,7 +62,7 @@ async def getBing(self, message, update, context): reply_to_message_id=update.message.message_id, ) self.mess = f"🤖️ Bing {numMessages} / {maxNumMessages} \n\n" + result - if COOKIES and API: + 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') @@ -85,7 +94,7 @@ async def getChatGPT(self, message, update, context): parse_mode='MarkdownV2', reply_to_message_id=update.message.message_id, ) - if COOKIES and API: + if self.bingcookie and API: self.LastMessage_id = message.message_id self.mess = "🤖️ ChatGPT3.5\n\n" + result else: @@ -98,7 +107,7 @@ async def getResult(self, update, context): 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 COOKIES and chat_content: + 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: @@ -107,11 +116,29 @@ async def getResult(self, update, context): async def reset_chat(self, update, context): if API: self.ChatGPTbot.reset() - if COOKIES: + if self.bingcookie: await self.resetBing() await context.bot.send_message( chat_id=update.message.chat_id, text="重置成功!", ) self.LastMessage_id = '' - self.mess = '' \ No newline at end of file + 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, + ) \ No newline at end of file diff --git a/bot.py b/bot.py index 7869a060..a78f5c9a 100644 --- a/bot.py +++ b/bot.py @@ -35,10 +35,12 @@ def setup(token): run_async(application.bot.set_my_commands([ BotCommand('start', 'Start the bot'), BotCommand('reset', 'Reset the bot'), + BotCommand('en2zh', 'translate English to Chinese'), ])) application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("reset", ai_bot.reset_chat)) + application.add_handler(CommandHandler("en2zh", ai_bot.en2zhtranslator)) application.add_handler(MessageHandler(filters.TEXT, ai_bot.getResult)) application.add_handler(MessageHandler(filters.COMMAND, unknown)) application.add_error_handler(error)