fix(wechatcomapp): split long text messages into multiple parts

This commit is contained in:
lanvent
2023-04-25 01:48:15 +08:00
parent c6601aaeed
commit 73c87d5959
6 changed files with 39 additions and 23 deletions

View File

@@ -32,3 +32,20 @@ def compress_imgfile(file, max_size):
if fsize(out_buf) <= max_size:
return out_buf
quality -= 5
def split_string_by_utf8_length(string, max_length, max_split=0):
encoded = string.encode("utf-8")
start, end = 0, 0
result = []
while end < len(encoded):
if max_split > 0 and len(result) >= max_split:
result.append(encoded[start:].decode("utf-8"))
break
end = min(start + max_length, len(encoded))
# 如果当前字节不是 UTF-8 编码的开始字节,则向前查找直到找到开始字节为止
while end < len(encoded) and (encoded[end] & 0b11000000) == 0b10000000:
end -= 1
result.append(encoded[start:end].decode("utf-8"))
start = end
return result