Skip to content

Commit

Permalink
加入英译中翻译功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Apr 22, 2023
1 parent e77cee7 commit deb5c29
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
43 changes: 35 additions & 8 deletions AI.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}")

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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 = ''
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,
)
2 changes: 2 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit deb5c29

Please sign in to comment.