From df5353a0064d9a6f4f55750e35b96b87a7e749f8 Mon Sep 17 00:00:00 2001 From: Cassius0924 <2670226747@qq.com> Date: Sun, 4 Feb 2024 12:17:10 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"[Improvement]=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=94=B6=E5=88=B0=E5=91=BD=E4=BB=A4=E6=8F=90=E9=86=92"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++--- wechatter/app/routers/wechat.py | 6 ++-- wechatter/commands/_commands/copilot_gpt4.py | 3 -- wechatter/message/message_parser.py | 3 ++ wechatter/notifier.py | 31 ++++++++++++++++++++ wechatter/sender/__init__.py | 3 +- wechatter/sender/notifier.py | 21 ------------- 7 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 wechatter/notifier.py delete mode 100644 wechatter/sender/notifier.py diff --git a/README.md b/README.md index 26667b1..5b75a13 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,12 @@ docker run -d \ --name wxBotWebhook \ -p 3001:3001 \ -e LOGIN_API_TOKEN="" \ --e RECVD_MSG_API="http://<宿主机IP>:<接收消息端口>/receive_msg" \ +-e RECVD_MSG_API="http://<内网IP>:<接收消息端口>/receive_msg" \ dannicool/docker-wechatbot-webhook ``` -- ``:登录令牌(可选)。 -- `<宿主机IP>`:填入 Docekr 的宿主机地址。 +- ``:登录令牌(不是密码),自己设置一个好记的。 +- `<内网IP>`:填入服务器的**内网IP**。如果是在自己电脑,则填入 `127.0.0.1`。 - `<接收消息端口>`:设置一个接收消息的端口,此项目中默认为 `4000`。 3. 登录微信 @@ -45,7 +45,7 @@ dannicool/docker-wechatbot-webhook docker logs -f wxBotWebhook ``` -### 启动 WeChatter +### 启动服务器 1. 下载源代码 diff --git a/wechatter/app/routers/wechat.py b/wechatter/app/routers/wechat.py index fe78b7e..52cedaf 100644 --- a/wechatter/app/routers/wechat.py +++ b/wechatter/app/routers/wechat.py @@ -10,7 +10,7 @@ from wechatter.message import MessageHandler from wechatter.message_forwarder import MessageForwarder from wechatter.models.message import Message -from wechatter.sender import notify_logged_in, notify_logged_out +from wechatter.notifier import Notifier from wechatter.sqlite.sqlite_manager import SqliteManager router = APIRouter() @@ -79,10 +79,10 @@ def handle_system_event(content: str) -> None: # 判断是否为机器人登录消息 if content_dict["event"] == "login": print("机器人登录成功") - notify_logged_in() + Notifier.notify_logged_in() elif content_dict["event"] == "logout": print("机器人已退出登录") - notify_logged_out() + Notifier.notify_logged_out() elif content_dict["event"] == "error": pass else: diff --git a/wechatter/commands/_commands/copilot_gpt4.py b/wechatter/commands/_commands/copilot_gpt4.py index ebc936b..38f2273 100644 --- a/wechatter/commands/_commands/copilot_gpt4.py +++ b/wechatter/commands/_commands/copilot_gpt4.py @@ -106,7 +106,6 @@ def _gptx(model: str, to: SendTo, message: str = "") -> None: chat_info = CopilotGPT4.get_chating_chat_info(wx_id, model) if message == "": # /gpt4 # 判断对话是否有效 - _send_text_msg(to, "正在创建新对话...") if chat_info is None or CopilotGPT4.is_chat_valid(chat_info): CopilotGPT4.create_chat(wx_id=wx_id, model=model) logger.info("创建新对话成功") @@ -116,7 +115,6 @@ def _gptx(model: str, to: SendTo, message: str = "") -> None: _send_text_msg(to, "对话未开始,继续上一次对话") else: # /gpt4 # 如果没有对话记录,则创建新对话 - _send_text_msg(to, f"正在调用 {model} 进行对话...") if chat_info is None: chat_info = CopilotGPT4.create_chat(wx_id=wx_id, model=model) logger.info("无历史对话记录,创建新对话成功") @@ -161,7 +159,6 @@ def _gptx_continue(model: str, to: SendTo, message: str = "") -> None: logger.info("请输入对话记录编号") _send_text_msg(to, "请输入对话记录编号") return - _send_text_msg(to, f"正在切换到对话记录 {message}...") chat_info = CopilotGPT4.continue_chat( wx_id=wx_id, model=model, chat_index=int(message) ) diff --git a/wechatter/message/message_parser.py b/wechatter/message/message_parser.py index c7bc5c5..eebc03a 100644 --- a/wechatter/message/message_parser.py +++ b/wechatter/message/message_parser.py @@ -6,6 +6,7 @@ import wechatter.config as config from wechatter.bot.bot_info import BotInfo from wechatter.models.message import Message, SendTo +from wechatter.notifier import Notifier class MessageHandler: @@ -40,6 +41,8 @@ def handle_message(self, message: Message) -> None: to = SendTo(message.source) # 是命令消息 + # 回复消息已收到 + Notifier.notify_received(to) # 开始处理命令 cmd_handler = cmd_dict["handler"] diff --git a/wechatter/notifier.py b/wechatter/notifier.py new file mode 100644 index 0000000..4644ef9 --- /dev/null +++ b/wechatter/notifier.py @@ -0,0 +1,31 @@ +# 消息通知器 +from wechatter.models.message import SendMessage, SendMessageType, SendTo +from wechatter.sender import Sender + + +class Notifier: + """消息通知器,用于发送非命令产生的消息""" + + def __init__(self): + pass + + # TODO: 改成只在为API请求的命令时才调用 + @staticmethod + def notify_received(to: SendTo) -> None: + """通知收到命令请求""" + msg = "收到命令请求" + Sender.send_msg(to, SendMessage(SendMessageType.TEXT, msg)) + + # 机器人登录登出通知,若是登录(登出)则发送登录(登出)消息给所有管理员 + @staticmethod + def notify_logged_in() -> None: + """通知登录成功""" + msg = "微信机器人启动成功" + Sender.send_msg_to_admins(msg) + + # FIXME: 登出消息发送不出去,因为发消息时候,机器人已经退出登录了 + @staticmethod + def notify_logged_out() -> None: + """通知已退出登录""" + msg = "微信机器人已退出" + Sender.send_msg_to_admins(msg) diff --git a/wechatter/sender/__init__.py b/wechatter/sender/__init__.py index 2cdd15c..53f54e1 100644 --- a/wechatter/sender/__init__.py +++ b/wechatter/sender/__init__.py @@ -1,4 +1,3 @@ -from .notifier import notify_logged_in, notify_logged_out, notify_received from .sender import Sender -__all__ = ["Sender", "notify_received", "notify_logged_in", "notify_logged_out"] +__all__ = ["Sender"] diff --git a/wechatter/sender/notifier.py b/wechatter/sender/notifier.py deleted file mode 100644 index cdf73da..0000000 --- a/wechatter/sender/notifier.py +++ /dev/null @@ -1,21 +0,0 @@ -# 消息通知器 -from wechatter.models.message import SendMessage, SendMessageType, SendTo -from wechatter.sender import Sender - - -def notify_received(to: SendTo) -> None: - """通知收到命令请求""" - msg = "收到命令请求" - Sender.send_msg(to, SendMessage(SendMessageType.TEXT, msg)) - - -def notify_logged_in() -> None: - """通知登录成功""" - msg = "微信机器人启动成功" - Sender.send_msg_to_admins(msg) - - -def notify_logged_out() -> None: - """通知已退出登录""" - msg = "微信机器人已退出" - Sender.send_msg_to_admins(msg)