Skip to content

Commit

Permalink
Merge pull request #46 from Cassius0924/revert-45-improve-notifier
Browse files Browse the repository at this point in the history
Revert "[Improvement] 优化收到命令提醒"
  • Loading branch information
Cassius0924 authored Feb 4, 2024
2 parents e8e1e41 + df5353a commit fd342f7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 33 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ docker run -d \
--name wxBotWebhook \
-p 3001:3001 \
-e LOGIN_API_TOKEN="<Token>" \
-e RECVD_MSG_API="http://<宿主机IP>:<接收消息端口>/receive_msg" \
-e RECVD_MSG_API="http://<内网IP>:<接收消息端口>/receive_msg" \
dannicool/docker-wechatbot-webhook
```

- `<Token>`:登录令牌(可选)
- `<宿主机IP>`填入 Docekr 的宿主机地址
- `<Token>`:登录令牌(不是密码),自己设置一个好记的
- `<内网IP>`填入服务器的**内网IP**。如果是在自己电脑,则填入 `127.0.0.1`
- `<接收消息端口>`:设置一个接收消息的端口,此项目中默认为 `4000`

3. 登录微信
Expand All @@ -45,7 +45,7 @@ dannicool/docker-wechatbot-webhook
docker logs -f wxBotWebhook
```

### 启动 WeChatter
### 启动服务器

1. 下载源代码

Expand Down
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.sender import notify_logged_in, notify_logged_out
from wechatter.notifier import Notifier
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("机器人登录成功")
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:
Expand Down
3 changes: 0 additions & 3 deletions wechatter/commands/_commands/copilot_gpt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("创建新对话成功")
Expand All @@ -116,7 +115,6 @@ 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 @@ -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)
)
Expand Down
3 changes: 3 additions & 0 deletions wechatter/message/message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -40,6 +41,8 @@ def handle_message(self, message: Message) -> None:
to = SendTo(message.source)

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

# 开始处理命令
cmd_handler = cmd_dict["handler"]
Expand Down
31 changes: 31 additions & 0 deletions wechatter/notifier.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions wechatter/sender/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
21 changes: 0 additions & 21 deletions wechatter/sender/notifier.py

This file was deleted.

0 comments on commit fd342f7

Please sign in to comment.