fix message

This commit is contained in:
jxxghp
2023-06-08 20:21:59 +08:00
parent 3fc21af41e
commit 0caac7ac00
9 changed files with 99 additions and 90 deletions

View File

@ -1,11 +1,10 @@
from abc import abstractmethod, ABCMeta
from pathlib import Path
from typing import Optional, List, Tuple, Union, Set
from typing import Optional, List, Tuple, Union, Set, Any
from fastapi import Request
from ruamel.yaml import CommentedMap
from app.core.context import MediaInfo, TorrentInfo
from app.core.context import MediaInfo, TorrentInfo, Context
from app.core.meta import MetaBase
from app.utils.types import TorrentStatus
@ -59,13 +58,15 @@ class _ModuleBase(metaclass=ABCMeta):
"""
pass
def message_parser(self, request: Request) -> Optional[dict]:
def message_parser(self, body: Any, form: Any, args: Any) -> Optional[dict]:
"""
解析消息内容,返回字典,注意以下约定值:
userid: 用户ID
username: 用户名
text: 内容
:param request: 请求体
:param body: 请求体
:param form: 表单
:param args: 参数
:return: 消息内容、用户ID
"""
pass
@ -205,7 +206,7 @@ class _ModuleBase(metaclass=ABCMeta):
"""
pass
def post_torrents_message(self, title: str, items: List[TorrentInfo],
def post_torrents_message(self, title: str, items: List[Context],
userid: Union[str, int] = None) -> Optional[bool]:
"""
发送种子信息选择列表

View File

@ -1,8 +1,7 @@
from typing import Optional, Union, List, Tuple
import json
from typing import Optional, Union, List, Tuple, Any
from fastapi import Request
from app.core import MediaInfo, TorrentInfo, settings
from app.core import MediaInfo, settings, Context
from app.log import logger
from app.modules import _ModuleBase
from app.modules.telegram.telegram import Telegram
@ -18,13 +17,15 @@ class TelegramModule(_ModuleBase):
def init_setting(self) -> Tuple[str, Union[str, bool]]:
return "MESSAGER", "telegram"
async def message_parser(self, request: Request) -> Optional[dict]:
def message_parser(self, body: Any, form: Any, args: Any) -> Optional[dict]:
"""
解析消息内容,返回字典,注意以下约定值:
userid: 用户ID
username: 用户名
text: 内容
:param request: 请求体
:param body: 请求体
:param form: 表单
:param args: 参数
:return: 消息内容、用户ID
"""
"""
@ -50,7 +51,11 @@ class TelegramModule(_ModuleBase):
}
}
"""
msg_json: dict = await request.json()
try:
msg_json: dict = json.loads(body)
except Exception as err:
logger.error(f"解析Telegram消息失败{err}")
return None
if msg_json:
message = msg_json.get("message", {})
text = message.get("text")
@ -61,12 +66,15 @@ class TelegramModule(_ModuleBase):
logger.info(f"收到Telegram消息userid={user_id}, username={user_name}, text={text}")
# 检查权限
if text.startswith("/"):
if str(user_id) not in settings.TELEGRAM_ADMINS.split(',') \
if settings.TELEGRAM_ADMINS \
and str(user_id) not in settings.TELEGRAM_ADMINS.split(',') \
and str(user_id) != settings.TELEGRAM_CHAT_ID:
self.telegram.send_msg(title="只有管理员才有权限执行此命令", userid=user_id)
return {}
else:
if not str(user_id) in settings.TELEGRAM_USERS.split(','):
if settings.TELEGRAM_USERS \
and not str(user_id) in settings.TELEGRAM_USERS.split(','):
logger.info(f"用户{user_id}不在用户白名单中,无法使用此机器人")
self.telegram.send_msg(title="你不在用户白名单中,无法使用此机器人", userid=user_id)
return {}
return {
@ -99,7 +107,7 @@ class TelegramModule(_ModuleBase):
"""
return self.telegram.send_meidas_msg(title=title, medias=items, userid=userid)
def post_torrents_message(self, title: str, items: List[TorrentInfo],
def post_torrents_message(self, title: str, items: List[Context],
userid: Union[str, int] = None) -> Optional[bool]:
"""
发送种子信息选择列表

View File

@ -2,7 +2,7 @@ from threading import Event, Thread
from typing import Optional, List
from urllib.parse import urlencode
from app.core import settings, MediaInfo, TorrentInfo
from app.core import settings, MediaInfo, TorrentInfo, Context
from app.log import logger
from app.utils.http import RequestUtils
from app.utils.singleton import Singleton
@ -28,6 +28,7 @@ class Telegram(metaclass=Singleton):
# 消息轮循
if self._telegram_token and self._telegram_chat_id:
self._thread = Thread(target=self.__start_telegram_message_proxy)
self._thread.start()
def send_msg(self, title: str, text: str = "", image: str = "", userid: str = "") -> Optional[bool]:
"""
@ -46,19 +47,10 @@ class Telegram(metaclass=Singleton):
return False
try:
# text中的Markdown特殊字符转义
text = text.replace("[", r"\[").replace("_", r"\_").replace("*", r"\*").replace("`", r"\`")
# 拼装消息内容
titles = str(title).split('\n')
if len(titles) > 1:
title = titles[0]
if not text:
text = "\n".join(titles[1:])
else:
text = "%s\n%s" % ("\n".join(titles[1:]), text)
if text:
caption = "*%s*\n%s" % (title, text.replace("\n\n", "\n"))
# text中的Markdown特殊字符转义
text = text.replace("[", r"\[").replace("_", r"\_").replace("*", r"\*").replace("`", r"\`").replace("\n\n", "\n")
caption = f"*{title}*\n{text}"
else:
caption = title
@ -85,19 +77,19 @@ class Telegram(metaclass=Singleton):
for media in medias:
if not image:
image = media.get_message_image()
if media.get_vote_string():
if media.vote_average:
caption = "%s\n%s. [%s](%s)\n%s%s" % (caption,
index,
media.get_title_string(),
media.get_detail_url(),
media.get_type_string(),
media.get_vote_string())
f"类型:{media.type.value}",
f"评分:{media.vote_average}")
else:
caption = "%s\n%s. [%s](%s)\n%s" % (caption,
index,
media.get_title_string(),
media.get_detail_url(),
media.get_type_string())
f"类型:{media.type.value}")
index += 1
if userid:
@ -111,7 +103,7 @@ class Telegram(metaclass=Singleton):
logger.error(f"发送消息失败:{msg_e}")
return False
def send_torrents_msg(self, torrents: List[TorrentInfo], userid: str = "", title: str = "") -> Optional[bool]:
def send_torrents_msg(self, torrents: List[Context], userid: str = "", title: str = "") -> Optional[bool]:
"""
发送列表消息
"""
@ -120,7 +112,8 @@ class Telegram(metaclass=Singleton):
try:
index, caption = 1, "*%s*" % title
for torrent in torrents:
for context in torrents:
torrent = context.torrent_info
link = torrent.page_url
title = torrent.title
free = torrent.get_volume_factor_string()
@ -167,18 +160,13 @@ class Telegram(metaclass=Singleton):
# 发送图文消息
if image:
res = request.get_res("https://api.telegram.org/bot%s/sendPhoto?" % self._telegram_token + urlencode(
{"chat_id": chat_id, "photo": image, "caption": caption, "parse_mode": "Markdown"}))
if __res_parse(res):
return True
else:
photo_req = request.get_res(image)
if photo_req and photo_req.content:
res = request.post_res("https://api.telegram.org/bot%s/sendPhoto" % self._telegram_token,
data={"chat_id": chat_id, "caption": caption, "parse_mode": "Markdown"},
files={"photo": photo_req.content})
if __res_parse(res):
return True
photo_req = request.get_res(image)
if photo_req and photo_req.content:
res = request.post_res("https://api.telegram.org/bot%s/sendPhoto" % self._telegram_token,
data={"chat_id": chat_id, "caption": caption, "parse_mode": "Markdown"},
files={"photo": photo_req.content})
if __res_parse(res):
return True
# 发送文本消息
res = request.get_res("https://api.telegram.org/bot%s/sendMessage?" % self._telegram_token + urlencode(
{"chat_id": chat_id, "text": caption, "parse_mode": "Markdown"}))

View File

@ -1,8 +1,7 @@
from typing import Optional, Union, List, Tuple
from fastapi import Request
import xml.dom.minidom
from app.core import MediaInfo, TorrentInfo, settings
from typing import Optional, Union, List, Tuple, Any
from app.core import MediaInfo, settings, Context
from app.log import logger
from app.modules import _ModuleBase
from app.modules.wechat.WXBizMsgCrypt3 import WXBizMsgCrypt
@ -20,26 +19,31 @@ class WechatModule(_ModuleBase):
def init_setting(self) -> Tuple[str, Union[str, bool]]:
return "MESSAGER", "wechat"
def message_parser(self, request: Request) -> Optional[dict]:
def message_parser(self, body: Any, form: Any, args: Any) -> Optional[dict]:
"""
解析消息内容,返回字典,注意以下约定值:
userid: 用户ID
username: 用户名
text: 内容
:param request: 请求体
:param body: 请求体
:param form: 表单
:param args: 参数
:return: 消息内容、用户ID
"""
try:
# URL参数
sVerifyMsgSig = request.query_params.get("msg_signature")
sVerifyTimeStamp = request.query_params.get("timestamp")
sVerifyNonce = request.query_params.get("nonce")
sVerifyMsgSig = args.get("msg_signature")
sVerifyTimeStamp = args.get("timestamp")
sVerifyNonce = args.get("nonce")
if not sVerifyMsgSig or not sVerifyTimeStamp or not sVerifyNonce:
logger.error(f"微信请求参数错误:{args}")
return None
# 解密模块
wxcpt = WXBizMsgCrypt(sToken=settings.WECHAT_TOKEN,
sEncodingAESKey=settings.WECHAT_ENCODING_AESKEY,
sReceiveId=settings.WECHAT_CORPID)
# 报文数据
sReqData = request.form()
sReqData = form
if not sReqData:
return None
logger.debug(f"收到微信请求:{sReqData}")
@ -132,7 +136,7 @@ class WechatModule(_ModuleBase):
# 再发送内容
return self.wechat.send_medias_msg(medias=items, userid=userid)
def post_torrents_message(self, title: str, items: List[TorrentInfo],
def post_torrents_message(self, title: str, items: List[Context],
userid: Union[str, int] = None) -> Optional[bool]:
"""
发送种子信息选择列表

View File

@ -3,7 +3,7 @@ import threading
from datetime import datetime
from typing import Optional, List
from app.core import settings, MediaInfo, TorrentInfo
from app.core import settings, MediaInfo, Context
from app.log import logger
from app.utils.http import RequestUtils
from app.utils.singleton import Singleton
@ -165,10 +165,10 @@ class WeChat(metaclass=Singleton):
articles = []
index = 1
for media in medias:
if media.get_vote_string():
title = f"{index}. {media.get_title_string()}\n{media.get_type_string()}{media.get_vote_string()}"
if media.vote_average:
title = f"{index}. {media.get_title_string()}\n类型:{media.type.value}评分:{media.vote_average}"
else:
title = f"{index}. {media.get_title_string()}\n{media.get_type_string()}"
title = f"{index}. {media.get_title_string()}\n类型:{media.type.value}"
articles.append({
"title": title,
"description": "",
@ -187,7 +187,7 @@ class WeChat(metaclass=Singleton):
}
return self.__post_request(message_url, req_json)
def send_torrents_msg(self, torrents: List[TorrentInfo], userid: str = "", title: str = "") -> Optional[bool]:
def send_torrents_msg(self, torrents: List[Context], userid: str = "", title: str = "") -> Optional[bool]:
"""
发送列表消息
"""
@ -197,7 +197,8 @@ class WeChat(metaclass=Singleton):
try:
index, caption = 1, "*%s*" % title
for torrent in torrents:
for context in torrents:
torrent = context.torrent_info
link = torrent.page_url
title = torrent.title
free = torrent.get_volume_factor_string()