fix webpush api
This commit is contained in:
@ -159,7 +159,7 @@ def set_switchs(switchs: List[NotificationSwitch],
|
|||||||
return schemas.Response(success=True)
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/subscribe", summary="客户端webpush通知订阅", response_model=schemas.Response)
|
@router.post("/webpush/subscribe", summary="客户端webpush通知订阅", response_model=schemas.Response)
|
||||||
def subscribe(subscription: schemas.Subscription, _: schemas.TokenPayload = Depends(verify_token)):
|
def subscribe(subscription: schemas.Subscription, _: schemas.TokenPayload = Depends(verify_token)):
|
||||||
"""
|
"""
|
||||||
客户端webpush通知订阅
|
客户端webpush通知订阅
|
||||||
@ -168,7 +168,7 @@ def subscribe(subscription: schemas.Subscription, _: schemas.TokenPayload = Depe
|
|||||||
return schemas.Response(success=True)
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/send-webpush", summary="发送webpush通知", response_model=schemas.Response)
|
@router.post("/webpush/send", summary="发送webpush通知", response_model=schemas.Response)
|
||||||
def send_notification(payload: schemas.SubscriptionMessage, _: schemas.TokenPayload = Depends(verify_token)):
|
def send_notification(payload: schemas.SubscriptionMessage, _: schemas.TokenPayload = Depends(verify_token)):
|
||||||
"""
|
"""
|
||||||
发送webpush通知
|
发送webpush通知
|
||||||
@ -178,7 +178,7 @@ def send_notification(payload: schemas.SubscriptionMessage, _: schemas.TokenPayl
|
|||||||
webpush(
|
webpush(
|
||||||
subscription_info=sub,
|
subscription_info=sub,
|
||||||
data=json.dumps(payload.dict()),
|
data=json.dumps(payload.dict()),
|
||||||
vapid_private_key=settings.VAPID.get("private_key"),
|
vapid_private_key=settings.VAPID.get("privateKey"),
|
||||||
vapid_claims={
|
vapid_claims={
|
||||||
"sub": settings.VAPID.get("subject")
|
"sub": settings.VAPID.get("subject")
|
||||||
},
|
},
|
||||||
|
66
app/modules/webpush/__init__.py
Normal file
66
app/modules/webpush/__init__.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import json
|
||||||
|
from typing import Union, Tuple
|
||||||
|
|
||||||
|
from pywebpush import webpush, WebPushException
|
||||||
|
|
||||||
|
from app.core.config import global_vars, settings
|
||||||
|
from app.log import logger
|
||||||
|
from app.modules import _ModuleBase, checkMessage
|
||||||
|
from app.schemas import MessageChannel, Notification
|
||||||
|
|
||||||
|
|
||||||
|
class WebPushModule(_ModuleBase):
|
||||||
|
def init_module(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_name() -> str:
|
||||||
|
return "VoceChat"
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test(self) -> Tuple[bool, str]:
|
||||||
|
"""
|
||||||
|
测试模块连接性
|
||||||
|
"""
|
||||||
|
return True, ""
|
||||||
|
|
||||||
|
def init_setting(self) -> Tuple[str, Union[str, bool]]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@checkMessage(MessageChannel.WebPush)
|
||||||
|
def post_message(self, message: Notification) -> None:
|
||||||
|
"""
|
||||||
|
发送消息
|
||||||
|
:param message: 消息内容
|
||||||
|
:return: 成功或失败
|
||||||
|
"""
|
||||||
|
if not message.title and not message.text:
|
||||||
|
logger.warn("标题和内容不能同时为空")
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
if message.title:
|
||||||
|
caption = message.title
|
||||||
|
content = message.text
|
||||||
|
else:
|
||||||
|
caption = message.text
|
||||||
|
content = ""
|
||||||
|
try:
|
||||||
|
for sub in global_vars.get_subscriptions():
|
||||||
|
webpush(
|
||||||
|
subscription_info=sub,
|
||||||
|
data=json.dumps({
|
||||||
|
"title": caption,
|
||||||
|
"body": content
|
||||||
|
}),
|
||||||
|
vapid_private_key=settings.VAPID.get("privateKey"),
|
||||||
|
vapid_claims={
|
||||||
|
"sub": settings.VAPID.get("subject")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
except WebPushException as err:
|
||||||
|
print("WebPush Error:", str(err))
|
||||||
|
|
||||||
|
except Exception as msg_e:
|
||||||
|
logger.error(f"发送消息失败:{msg_e}")
|
@ -138,6 +138,7 @@ class MessageChannel(Enum):
|
|||||||
SynologyChat = "SynologyChat"
|
SynologyChat = "SynologyChat"
|
||||||
VoceChat = "VoceChat"
|
VoceChat = "VoceChat"
|
||||||
Web = "Web"
|
Web = "Web"
|
||||||
|
WebPush = "WebPush"
|
||||||
|
|
||||||
|
|
||||||
# 用户配置Key字典
|
# 用户配置Key字典
|
||||||
|
Reference in New Issue
Block a user