fix message
This commit is contained in:
parent
6e607ca89f
commit
a5d044d535
@ -8,11 +8,13 @@ from app import schemas
|
||||
from app.chain.message import MessageChain
|
||||
from app.core.config import settings
|
||||
from app.core.security import verify_token
|
||||
from app.db.models import User
|
||||
from app.db.systemconfig_oper import SystemConfigOper
|
||||
from app.db.userauth import get_current_active_superuser
|
||||
from app.log import logger
|
||||
from app.modules.wechat.WXBizMsgCrypt3 import WXBizMsgCrypt
|
||||
from app.schemas import NotificationSwitch
|
||||
from app.schemas.types import SystemConfigKey, NotificationType
|
||||
from app.schemas.types import SystemConfigKey, NotificationType, MessageChannel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -36,6 +38,20 @@ async def user_message(background_tasks: BackgroundTasks, request: Request):
|
||||
return schemas.Response(success=True)
|
||||
|
||||
|
||||
@router.post("/web", summary="接收WEB消息", response_model=schemas.Response)
|
||||
async def web_message(text: str, current_user: User = Depends(get_current_active_superuser)):
|
||||
"""
|
||||
WEB消息响应
|
||||
"""
|
||||
MessageChain().handle_message(
|
||||
channel=MessageChannel.Web,
|
||||
userid=current_user.id,
|
||||
username=current_user.name,
|
||||
text=text
|
||||
)
|
||||
return schemas.Response(success=True)
|
||||
|
||||
|
||||
def wechat_verify(echostr: str, msg_signature: str,
|
||||
timestamp: Union[str, int], nonce: str) -> Any:
|
||||
"""
|
||||
@ -103,7 +119,7 @@ def read_switchs(_: schemas.TokenPayload = Depends(verify_token)) -> Any:
|
||||
def set_switchs(switchs: List[NotificationSwitch],
|
||||
_: schemas.TokenPayload = Depends(verify_token)) -> Any:
|
||||
"""
|
||||
查询通知消息渠道开关
|
||||
设置通知消息渠道开关
|
||||
"""
|
||||
switch_list = []
|
||||
for switch in switchs:
|
||||
|
@ -12,9 +12,11 @@ from app.core.config import settings
|
||||
from app.core.context import MediaInfo, Context
|
||||
from app.core.event import EventManager
|
||||
from app.core.meta import MetaBase
|
||||
from app.db.message_oper import MessageOper
|
||||
from app.helper.message import MessageHelper
|
||||
from app.helper.torrent import TorrentHelper
|
||||
from app.log import logger
|
||||
from app.schemas import Notification, NotExistMediaInfo
|
||||
from app.schemas import Notification, NotExistMediaInfo, CommingMessage
|
||||
from app.schemas.types import EventType, MessageChannel, MediaType
|
||||
from app.utils.string import StringUtils
|
||||
|
||||
@ -43,6 +45,8 @@ class MessageChain(ChainBase):
|
||||
self.mediachain = MediaChain()
|
||||
self.eventmanager = EventManager()
|
||||
self.torrenthelper = TorrentHelper()
|
||||
self.messagehelper = MessageHelper()
|
||||
self.messageoper = MessageOper()
|
||||
|
||||
def __get_noexits_info(
|
||||
self,
|
||||
@ -100,10 +104,8 @@ class MessageChain(ChainBase):
|
||||
|
||||
def process(self, body: Any, form: Any, args: Any) -> None:
|
||||
"""
|
||||
识别消息内容,执行操作
|
||||
调用模块识别消息内容
|
||||
"""
|
||||
# 申明全局变量
|
||||
global _current_page, _current_meta, _current_media
|
||||
# 获取消息内容
|
||||
info = self.message_parser(body=body, form=form, args=args)
|
||||
if not info:
|
||||
@ -122,10 +124,35 @@ class MessageChain(ChainBase):
|
||||
if not text:
|
||||
logger.debug(f'未识别到消息内容::{body}{form}{args}')
|
||||
return
|
||||
# 处理消息
|
||||
self.handle_message(channel=channel, userid=userid, username=username, text=text)
|
||||
|
||||
def handle_message(self, channel: MessageChannel, userid: Union[str, int], username: str, text: str) -> None:
|
||||
"""
|
||||
识别消息内容,执行操作
|
||||
"""
|
||||
# 申明全局变量
|
||||
global _current_page, _current_meta, _current_media
|
||||
# 加载缓存
|
||||
user_cache: Dict[str, dict] = self.load_cache(self._cache_file) or {}
|
||||
# 处理消息
|
||||
logger.info(f'收到用户消息内容,用户:{userid},内容:{text}')
|
||||
# 保存消息
|
||||
self.messagehelper.put(
|
||||
CommingMessage(
|
||||
userid=userid,
|
||||
username=username,
|
||||
channel=channel,
|
||||
text=text
|
||||
), role="user")
|
||||
self.messageoper.add(
|
||||
channel=channel,
|
||||
userid=userid,
|
||||
username=username,
|
||||
text=text,
|
||||
action=0
|
||||
)
|
||||
# 处理消息
|
||||
if text.startswith('/'):
|
||||
# 执行命令
|
||||
self.eventmanager.send_event(
|
||||
|
@ -1,8 +1,7 @@
|
||||
import json
|
||||
import queue
|
||||
from typing import Any, Union, Optional
|
||||
from typing import Optional, Any
|
||||
|
||||
from app.schemas import Notification
|
||||
from app.utils.singleton import Singleton
|
||||
|
||||
|
||||
@ -14,7 +13,7 @@ class MessageHelper(metaclass=Singleton):
|
||||
self.sys_queue = queue.Queue()
|
||||
self.user_queue = queue.Queue()
|
||||
|
||||
def put(self, message: Union[str, Notification], role: str = "sys"):
|
||||
def put(self, message: Any, role: str = "sys"):
|
||||
"""
|
||||
存消息
|
||||
:param message: 消息
|
||||
@ -23,10 +22,10 @@ class MessageHelper(metaclass=Singleton):
|
||||
if role == "sys":
|
||||
self.sys_queue.put(message)
|
||||
else:
|
||||
if isinstance(message, Notification):
|
||||
self.user_queue.put(json.dumps(message.dict()))
|
||||
else:
|
||||
if isinstance(message, str):
|
||||
self.user_queue.put(message)
|
||||
elif hasattr(message, "dict"):
|
||||
self.user_queue.put(json.dumps(message.dict()))
|
||||
|
||||
def get(self, role: str = "sys") -> Optional[str]:
|
||||
"""
|
||||
|
@ -18,6 +18,18 @@ class CommingMessage(BaseModel):
|
||||
# 消息体
|
||||
text: Optional[str] = None
|
||||
|
||||
def dict(self):
|
||||
"""
|
||||
转换为字典
|
||||
"""
|
||||
return {
|
||||
"userid": self.userid,
|
||||
"username": self.username,
|
||||
"channel": self.channel.value if self.channel else None,
|
||||
"text": self.text,
|
||||
"action": 0
|
||||
}
|
||||
|
||||
|
||||
class Notification(BaseModel):
|
||||
"""
|
||||
@ -46,7 +58,8 @@ class Notification(BaseModel):
|
||||
"text": self.text,
|
||||
"image": self.image,
|
||||
"link": self.link,
|
||||
"userid": self.userid
|
||||
"userid": self.userid,
|
||||
"action": 1
|
||||
}
|
||||
|
||||
|
||||
|
@ -117,3 +117,4 @@ class MessageChannel(Enum):
|
||||
Slack = "Slack"
|
||||
SynologyChat = "SynologyChat"
|
||||
VoceChat = "VoceChat"
|
||||
Web = "Web"
|
||||
|
Loading…
x
Reference in New Issue
Block a user