From d90e3c29a56a1b952635723126dda139e378cf9c Mon Sep 17 00:00:00 2001 From: Dean <36684698+BrettDean@users.noreply.github.com> Date: Thu, 11 Jul 2024 20:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BE=AE=E4=BF=A1=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81=EF=BC=9A=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=95=BF=E6=96=87=E6=9C=AC=E5=88=86=E5=9D=97=E5=8F=91?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- app/modules/wechat/wechat.py | 59 +++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index f7305d1a..501fbd9f 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ config/sites/** *.pyc *.log .vscode -venv \ No newline at end of file +venv +.DS_Store diff --git a/app/modules/wechat/wechat.py b/app/modules/wechat/wechat.py index 92e7eda7..18662cb4 100644 --- a/app/modules/wechat/wechat.py +++ b/app/modules/wechat/wechat.py @@ -100,28 +100,57 @@ class WeChat: """ message_url = self._send_msg_url % self.__get_access_token() if text: - conent = "%s\n%s" % (title, text.replace("\n\n", "\n")) + content = "%s\n%s" % (title, text.replace("\n\n", "\n")) else: - conent = title + content = title if link: - conent = f"{conent}\n点击查看:{link}" + content = f"{content}\n点击查看:{link}" if not userid: userid = "@all" - req_json = { - "touser": userid, - "msgtype": "text", - "agentid": self._appid, - "text": { - "content": conent - }, - "safe": 0, - "enable_id_trans": 0, - "enable_duplicate_check": 0 - } - return self.__post_request(message_url, req_json) + # Check if content exceeds 2048 bytes and split if necessary + if len(content.encode('utf-8')) > 2048: + content_chunks = [] + current_chunk = "" + for line in content.splitlines(): + if len(current_chunk.encode('utf-8')) + len(line.encode('utf-8')) > 2048: + content_chunks.append(current_chunk.strip()) + current_chunk = "" + current_chunk += line + "\n" + if current_chunk: + content_chunks.append(current_chunk.strip()) + + # Send each chunk as a separate message + for chunk in content_chunks: + req_json = { + "touser": userid, + "msgtype": "text", + "agentid": self._appid, + "text": { + "content": chunk + }, + "safe": 0, + "enable_id_trans": 0, + "enable_duplicate_check": 0 + } + result = self.__post_request(message_url, req_json) + else: + req_json = { + "touser": userid, + "msgtype": "text", + "agentid": self._appid, + "text": { + "content": content + }, + "safe": 0, + "enable_id_trans": 0, + "enable_duplicate_check": 0 + } + return self.__post_request(message_url, req_json) + + return result def __send_image_message(self, title: str, text: str, image_url: str, userid: str = None, link: str = None) -> Optional[bool]: