tuangtalk/chatgpt-wecom

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

Opened this issue · 1 comments

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

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