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

添加企业微信群机器人通知 #747

Open
wants to merge 6 commits into
base: 3.1-dev
Choose a base branch
from
Open
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: 5 additions & 1 deletion backend/src/module/notification/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ServerChanNotification,
TelegramNotification,
WecomNotification,
WecomRobotNotification,
)

logger = logging.getLogger(__name__)
Expand All @@ -23,6 +24,8 @@ def getClient(type: str):
return BarkNotification
elif type.lower() == "wecom":
return WecomNotification
elif type.lower() == "wecom-robot":
return WecomRobotNotification
else:
return None

Expand All @@ -43,8 +46,9 @@ def _get_poster(notify: Notification):
def send_msg(self, notify: Notification) -> bool:
self._get_poster(notify)
try:
self.notifier.post_msg(notify)
resp = self.notifier.post_msg(notify)
logger.debug(f"Send notification: {notify.official_title}")
return resp
except Exception as e:
logger.warning(f"Failed to send notification: {e}")
return False
Expand Down
1 change: 1 addition & 0 deletions backend/src/module/notification/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .server_chan import ServerChanNotification
from .telegram import TelegramNotification
from .wecom import WecomNotification
from .wecom_robot import WecomRobotNotification
51 changes: 51 additions & 0 deletions backend/src/module/notification/plugin/wecom_robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import logging

import requests

from module.models import Notification
from module.network import RequestContent

logger = logging.getLogger(__name__)


class WecomRobotNotification(RequestContent):
"""企业微信群机器人"""

def __init__(self, token, chat_id, **kwargs):
super().__init__()
# token is wecom group robot webhook key
self.notification_url = (
f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={token}"
)

@staticmethod
def gen_message(notify: Notification) -> str:
text = f"""
番剧名称:{notify.official_title}\n季度: 第{notify.season}季\n更新集数: 第{notify.episode}集
"""
return text.strip()

def post_msg(self, notify: Notification) -> bool:
title = "【番剧更新】"
msg = self.gen_message(notify)
picurl = notify.poster_path
if picurl.startswith("http"):
data = {
"msgtype": "news",
"news": {
"articles": [
{
"title": title,
"description": msg,
"url": "https://mikanime.tv",
"picurl": picurl,
}
]
},
}
else:
msg = f"{title}\n{msg}"
data = {"msgtype": "text", "text": {"content": msg}}
resp = requests.post(url=self.notification_url, json=data, timeout=3)
logger.debug(f"Wecom-robot notification: {resp.status_code}")
return resp.status_code == 200
26 changes: 26 additions & 0 deletions backend/src/test/test_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from module.models import Notification
from module.notification import PostNotification


class TestPostNotification(PostNotification):
@staticmethod
def _get_poster(notify: Notification):
notify.poster_path = notify.poster_path


def test_notification():
info1 = Notification(
official_title="番剧名",
season=1,
episode=1,
poster_path="https://mikanime.tv/images/Bangumi/202404/0fd46fc8.jpg",
)
info2 = Notification(
official_title="番剧名",
season=1,
episode=1,
poster_path="posters/0fd46fc8.jpg",
)
with TestPostNotification() as notifier:
assert notifier.send_msg(info1)
assert notifier.send_msg(info2)
6 changes: 4 additions & 2 deletions backend/src/test/test_rss_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ def test_rss_engine():

new_torrents = engine.pull_rss(result[1])
torrent = new_torrents[0]
assert torrent.name == "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]"

assert (
torrent.name
== "[Lilith-Raws] 无职转生,到了异世界就拿出真本事 / Mushoku Tensei - 11 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]"
)
1 change: 1 addition & 0 deletions webui/src/components/setting/config-notification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const notificationType: NotificationType = [
'server-chan',
'bark',
'wecom',
'wecom-robot'
];

const items: SettingItem<Notification>[] = [
Expand Down