From 746a036dd5b7de1ed92ed73155428a95713ea754 Mon Sep 17 00:00:00 2001 From: Cassius0924 <2670226747@qq.com> Date: Sun, 4 Feb 2024 12:14:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=94=B6=E5=88=B0?= =?UTF-8?q?=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, 33 insertions(+), 42 deletions(-) delete mode 100644 wechatter/notifier.py create mode 100644 wechatter/sender/notifier.py diff --git a/README.md b/README.md index 5b75a13..26667b1 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>`:填入服务器的**内网IP**。如果是在自己电脑,则填入 `127.0.0.1`。 +- ``:登录令牌(可选)。 +- `<宿主机IP>`:填入 Docekr 的宿主机地址。 - `<接收消息端口>`:设置一个接收消息的端口,此项目中默认为 `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 52cedaf..fe78b7e 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.notifier import Notifier +from wechatter.sender import notify_logged_in, notify_logged_out 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("机器人登录成功") - Notifier.notify_logged_in() + notify_logged_in() elif content_dict["event"] == "logout": print("机器人已退出登录") - Notifier.notify_logged_out() + 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 38f2273..ebc936b 100644 --- a/wechatter/commands/_commands/copilot_gpt4.py +++ b/wechatter/commands/_commands/copilot_gpt4.py @@ -106,6 +106,7 @@ 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("创建新对话成功") @@ -115,6 +116,7 @@ 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("无历史对话记录,创建新对话成功") @@ -159,6 +161,7 @@ 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 eebc03a..c7bc5c5 100644 --- a/wechatter/message/message_parser.py +++ b/wechatter/message/message_parser.py @@ -6,7 +6,6 @@ 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: @@ -41,8 +40,6 @@ 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 deleted file mode 100644 index 4644ef9..0000000 --- a/wechatter/notifier.py +++ /dev/null @@ -1,31 +0,0 @@ -# 消息通知器 -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 53f54e1..2cdd15c 100644 --- a/wechatter/sender/__init__.py +++ b/wechatter/sender/__init__.py @@ -1,3 +1,4 @@ +from .notifier import notify_logged_in, notify_logged_out, notify_received from .sender import Sender -__all__ = ["Sender"] +__all__ = ["Sender", "notify_received", "notify_logged_in", "notify_logged_out"] diff --git a/wechatter/sender/notifier.py b/wechatter/sender/notifier.py new file mode 100644 index 0000000..cdf73da --- /dev/null +++ b/wechatter/sender/notifier.py @@ -0,0 +1,21 @@ +# 消息通知器 +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)