From 0f73724ab10cf1c0ad4802e8601193fbd2deff7e Mon Sep 17 00:00:00 2001 From: XYenon Date: Sun, 6 Mar 2022 20:22:40 +0800 Subject: [PATCH] feat(forward_msg): support forward msg --- .vscode/launch.json | 3 ++- efb_qq_plugin_go_cqhttp/GoCQHttp.py | 28 +++++++++++++++++++++++++ efb_qq_plugin_go_cqhttp/MsgDecorator.py | 17 ++++++++++----- efb_qq_plugin_go_cqhttp/__init__.py | 2 +- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 7d45a16..d8ef827 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,8 @@ "name": "Python: 模块", "type": "python", "request": "launch", - "module": "ehforwarderbot" + "module": "ehforwarderbot", + "args": ["-v"] } ] } diff --git a/efb_qq_plugin_go_cqhttp/GoCQHttp.py b/efb_qq_plugin_go_cqhttp/GoCQHttp.py index 97d363e..71d657a 100644 --- a/efb_qq_plugin_go_cqhttp/GoCQHttp.py +++ b/efb_qq_plugin_go_cqhttp/GoCQHttp.py @@ -104,6 +104,24 @@ class GoCQHttp(BaseClient): self.is_logged_in = False self.msg_decorator = QQMsgProcessor(instance=self) + def forward_msgs_wrapper(msg_elements: List[Dict[str, Any]]) -> List[Dict[str, Any]]: + fmt_msgs = [] + for msg in msg_elements: + from_user = self.get_user_info(msg["sender"]["user_id"]) + header_text = {"data": {"text": f'{from_user["remark"]}({from_user["nickname"]}):\n'}, "type": "text"} + footer_text = {"data": {"text": "\n- - - - - - - - - - - - - - -\n"}, "type": "text"} + msg["content"].insert(0, header_text) + msg["content"].append(footer_text) + for i, inner_msg in enumerate(msg["content"]): + if "content" in inner_msg: + if i == 1: + fmt_msgs.pop() + msg["content"].pop() + fmt_msgs += forward_msgs_wrapper([inner_msg]) + else: + fmt_msgs.append(inner_msg) + return fmt_msgs + def message_element_wrapper( context: Dict[str, Any], msg_element: Dict[str, Any], chat: Chat ) -> Tuple[str, List[Message], List[Tuple[Tuple[int, int], Union[Chat, ChatMember]]]]: @@ -145,6 +163,16 @@ class GoCQHttp(BaseClient): f'「{ref_user["remark"]}({ref_user["nickname"]}):{msg_data["text"]}」\n' "- - - - - - - - - - - - - - -\n" ) + elif msg_type == "forward": + forward_msgs = self.coolq_api_query("get_forward_msg", message_id=msg_data["id"])["messages"] + logging.debug(f"Forwarded message: {forward_msgs}") + fmt_forward_msgs = forward_msgs_wrapper(forward_msgs) + logging.debug(f"Formated forwarded message: {forward_msgs}") + header_msg = {"data": {"text": "合并转发消息开始\n- - - - - - - - - - - - - - -\n"}, "type": "text"} + footer_msg = {"data": {"text": "合并转发消息结束"}, "type": "text"} + fmt_forward_msgs.insert(0, header_msg) + fmt_forward_msgs.append(footer_msg) + return message_elements_wrapper(context, fmt_forward_msgs, chat) else: messages.extend(self.call_msg_decorator(msg_type, msg_data, chat)) return main_text, messages, at_list diff --git a/efb_qq_plugin_go_cqhttp/MsgDecorator.py b/efb_qq_plugin_go_cqhttp/MsgDecorator.py index 9dcf715..02e3912 100644 --- a/efb_qq_plugin_go_cqhttp/MsgDecorator.py +++ b/efb_qq_plugin_go_cqhttp/MsgDecorator.py @@ -299,11 +299,18 @@ class QQMsgProcessor: preview=meta_view["preview"], ) - elif dict_data['app'] == 'com.tencent.map': - efb_msg.text = "【位置消息】\n地址:{}\n点击导航(高德):https://urljump.vercel.app/?query=amapuri://route/plan?dev=0&dlat={}&dlon={}".format(dict_data['meta']['Location.Search']['address'],dict_data['meta']['Location.Search']['lat'],dict_data['meta']['Location.Search']['lng']) - - elif dict_data['app'] == 'com.tencent.qq.checkin': - efb_msg.text = "【群签到】\n内容:{}\n图片:{}".format(dict_data['meta']['checkInData']['desc'],dict_data['meta']['checkInData']['cover']['url']) + elif dict_data["app"] == "com.tencent.map": + efb_msg.text = "【位置消息】\n地址:{}\n点击导航(高德): \ + https://urljump.vercel.app/?query=amapuri://route/plan?dev=0&dlat={}&dlon={}".format( + dict_data["meta"]["Location.Search"]["address"], + dict_data["meta"]["Location.Search"]["lat"], + dict_data["meta"]["Location.Search"]["lng"], + ) + + elif dict_data["app"] == "com.tencent.qq.checkin": + efb_msg.text = "【群签到】\n内容:{}\n图片:{}".format( + dict_data["meta"]["checkInData"]["desc"], dict_data["meta"]["checkInData"]["cover"]["url"] + ) except Exception: self.logger.error(f"json_wrapper_info: {data}\nexc_info:{sys.exc_info()[0]}") diff --git a/efb_qq_plugin_go_cqhttp/__init__.py b/efb_qq_plugin_go_cqhttp/__init__.py index 4625cde..0dc70fe 100644 --- a/efb_qq_plugin_go_cqhttp/__init__.py +++ b/efb_qq_plugin_go_cqhttp/__init__.py @@ -1,3 +1,3 @@ from . import GoCQHttp # noqa: F401 -__version__ = "2.1.0" +__version__ = "2.2.0"