Skip to content

Commit

Permalink
feat: 集成bark机器人状态变化提醒
Browse files Browse the repository at this point in the history
  • Loading branch information
Cassius0924 committed Mar 4, 2024
1 parent 01afb46 commit c6400af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ python3 -m wechatter
## 支持的功能

- [x] **掉线提醒**:当机器人掉线时,通过 Bark 推送提醒消息。
- [x] **消息可引用回复**:用户可以通过引用并回复命令消息进一步获取消息内容。带`(可引用:***)`的机器人消息即为可进一步互动的可引用消息。
- [x] **消息转发**:转发用户或群的消息到其他用户或群,并支持引用回复转发消息。需进行[配置](#%EF%B8%8F-message-forwarding-配置)
![message_forwarding_and_quoted_reply_show](docs/images/message_forwarding_and_quoted_reply_show.png)
Expand Down Expand Up @@ -156,6 +157,7 @@ python3 -m wechatter
| --- | --- | --- |
| `admin_list` | 设置管理员,用于接收机器人状态变化通知 | 填入管理员微信名(不是备注) |
| `admin_group_list` |`admin_list` 同理,接收机器人状态变化通知 | 填入群名称(不是群备注) |
| `bark_url` | 用于接收机器人状态变化通知的 Bark URL | [Bark](https://github.com/Finb/Bark) 仅限 iOS 和 iPadOS |

### ⚙️ Bot 配置

Expand Down
1 change: 1 addition & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ wx_webhook_token: "your_wx_webhook_token"
# Admin
admin_list: [ "文件传输助手", "AdminName" ]
admin_group_list: [ "AdminGroupName" ]
bark_url: your_bark_url


# Bot
Expand Down
15 changes: 5 additions & 10 deletions wechatter/app/routers/wechat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from typing import Union

from fastapi import APIRouter, Form, UploadFile
Expand Down Expand Up @@ -39,7 +38,6 @@ async def recv_wechat_msg(
"""
用于接收 wxBotWebhook 转发过来的消息的接口
"""

# 更新机器人信息(id和name)
# BotInfo.update_from_source(source)

Expand All @@ -50,7 +48,7 @@ async def recv_wechat_msg(
# 判断是否是系统事件
if type in ["system_event_login", "system_event_logout", "system_event_error"]:
logger.info(f"收到系统事件:{type}")
handle_system_event(content)
handle_system_event(type)
return

# 不是系统消息,则是用户发来的消息
Expand Down Expand Up @@ -84,19 +82,16 @@ async def recv_wechat_msg(
# return {"success": True, "data": {"type": "text", "content": "hello world!"}}


def handle_system_event(content: str) -> None:
def handle_system_event(event: str) -> None:
"""
判断系统事件类型,并调用相应的函数
"""
content_dict: dict = json.loads(content)
# 判断是否为机器人登录消息
if content_dict["event"] == "login":
logger.info("机器人登录成功")
if event == "system_event_login":
notifier.notify_logged_in()
elif content_dict["event"] == "logout":
logger.info("机器人已退出登录")
elif event == "system_event_logout":
notifier.notify_logged_out()
elif content_dict["event"] == "error":
elif event == "system_event_error":
pass
else:
pass
Expand Down
22 changes: 20 additions & 2 deletions wechatter/sender/notifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# 消息通知器
from typing import TYPE_CHECKING

from loguru import logger

from wechatter.config import config
from wechatter.sender import sender
from wechatter.utils import get_request
from wechatter.utils.url_joiner import join_urls

if TYPE_CHECKING:
from wechatter.models.wechat import SendTo
Expand All @@ -21,13 +26,26 @@ def notify_logged_in() -> None:
通知登录成功
"""
msg = "微信机器人启动成功"
logger.info(msg)
sender.mass_send_msg_to_admins(msg)
if config.get("bark_url"):
url = join_urls(
config["bark_url"],
f"WeChatter/🟢 微信机器人({config['bot_name']})登录成功",
)
get_request(url)


# FIXME: 登出消息发送不出去,因为发消息时候,机器人已经退出登录了
def notify_logged_out() -> None:
"""
通知已退出登录
"""
msg = "微信机器人已退出"
sender.mass_send_msg_to_admins(msg)
logger.info(msg)
# bark 提醒
if config.get("bark_url"):
url = join_urls(
config["bark_url"],
f"WeChatter/🔴 微信机器人({config['bot_name']})已退出登录?copy={config['wx_webhook_base_api']}/login?token={config['wx_webhook_token']}",
)
get_request(url)

0 comments on commit c6400af

Please sign in to comment.