Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

后台ChatGPT响应的内容是完整的,可能是企业微信限制的字符数,在企业微信发送的内容不完整,大佬改造下将后面不完整的内容再继续发直到完整。谢谢 #34

Open
Richardtmp opened this issue Apr 17, 2023 · 1 comment

Comments

@Richardtmp
Copy link

No description provided.

@Richardtmp
Copy link
Author

Richardtmp commented Apr 27, 2023

weixin.py 关于推送文本消息。解决由于企业微信限制2048字节,导致内容被截断;

def send_text(self, message_list,towxuser):
    '''
    推送文本消息
    '''
    messages = WeChat.set_geshi(message_list)[1]
    max_bytes = 2048

    def next_utf8_slice(text, max_bytes):
        utf8_bytes = text.encode('utf-8')
        if len(utf8_bytes) <= max_bytes:
            return text, ''
        while max_bytes > 0:
            try:
                return utf8_bytes[:max_bytes].decode('utf-8'), utf8_bytes[max_bytes:].decode('utf-8')
            except UnicodeDecodeError:
                max_bytes -= 1
        return '', text

    def split_message(content):
        chunks = []
        while content:
            chunk, content = next_utf8_slice(content, max_bytes)
            chunks.append(chunk)
        return chunks
    
    message_chunks = split_message(messages)
    response_list = []
    for chunk in message_chunks:
        text_dict = {
            "touser": towxuser,
            "msgtype": "text",
            "agentid": self.agentid,
            "text": {
                "content": chunk
            },
            "safe": 0,
            "enable_id_trans": 0,
            "enable_duplicate_check": 0,
            "duplicate_check_interval": 1800
    }
        response_list.append(self.send(text_dict))

    return response_list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant