Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] 优化收到命令提醒 #48

Merged
merged 3 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions wechatter/app/routers/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion wechatter/commands/_commands/copilot_gpt4.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# 使用 Copilot-GPT4-Server 回复
from typing import List, Union

from loguru import logger
Expand Down Expand Up @@ -106,6 +105,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("创建新对话成功")
Expand All @@ -115,6 +115,7 @@ def _gptx(model: str, to: SendTo, message: str = "") -> None:
_send_text_msg(to, "对话未开始,继续上一次对话")
else: # /gpt4 <message>
# 如果没有对话记录,则创建新对话
_send_text_msg(to, f"正在调用 {model} 进行对话...")
if chat_info is None:
chat_info = CopilotGPT4.create_chat(wx_id=wx_id, model=model)
logger.info("无历史对话记录,创建新对话成功")
Expand Down Expand Up @@ -159,6 +160,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)
)
Expand Down
4 changes: 0 additions & 4 deletions wechatter/message/message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -41,9 +40,6 @@ def handle_message(self, message: Message) -> None:
to = SendTo(message.source)

# 是命令消息
# 回复消息已收到
Notifier.notify_received(to)

# 开始处理命令
cmd_handler = cmd_dict["handler"]
if cmd_handler is not None:
Expand Down
31 changes: 0 additions & 31 deletions wechatter/notifier.py

This file was deleted.

5 changes: 4 additions & 1 deletion wechatter/sender/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# isort: off
from .sender import Sender
from .notifier import notify_logged_in, notify_logged_out, notify_received
# isort: on

__all__ = ["Sender"]
__all__ = ["Sender", "notify_received", "notify_logged_in", "notify_logged_out"]
23 changes: 23 additions & 0 deletions wechatter/sender/notifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 消息通知器
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)


# FIXME: 登出消息发送不出去,因为发消息时候,机器人已经退出登录了
def notify_logged_out() -> None:
"""通知已退出登录"""
msg = "微信机器人已退出"
Sender.send_msg_to_admins(msg)
Loading