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

@@ -1,6 +1,8 @@
# 企业微信应用号channel
企业微信官方提供了客服、应用等API本channel使用的是企业微信的应用API的能力。因为未来可能还会开发客服能力所以本channel的类型名叫作`wechatcom_app`
企业微信官方提供了客服、应用等API本channel使用的是企业微信的应用API的能力。
因为未来可能还会开发客服能力所以本channel的类型名叫作`wechatcom_app`
`wechatcom_app` channel支持插件系统和图片声音交互等能力除了无法加入群聊作为个人使用的私人助理已绰绰有余。
@@ -29,7 +31,7 @@
- 在详情页如果点击`企业可信IP`的配置(没看到可以不管)填入你服务器的公网IP
- 点击`接收消息`下的启用API接收消息
- `URL`填写格式为`http://url:port/wxcomapp`是程序监听的端口默认是9898
- `URL`填写格式为`http://url:port/wxcomapp``port`是程序监听的端口默认是9898
如果是未认证的企业url可直接使用服务器的IP。如果是认证企业需要使用备案的域名可使用二级域名。
- `Token`可随意填写,停留在这个页面
- 在程序根目录`config.json`中增加配置(**去掉注释**`wechatcomapp_aes_key`是当前页面的`wechatcomapp_aes_key`

View File

@@ -2,6 +2,7 @@
import io
import os
import textwrap
import time
import requests
import web
@@ -17,10 +18,12 @@ from channel.wechatcom.wechatcomapp_client import WechatComAppClient
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
from common.log import logger
from common.singleton import singleton
from common.utils import compress_imgfile, fsize
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length
from config import conf
from voice.audio_convert import any_to_amr
MAX_UTF8_LEN = 2048
@singleton
class WechatComAppChannel(ChatChannel):
@@ -50,8 +53,15 @@ class WechatComAppChannel(ChatChannel):
def send(self, reply: Reply, context: Context):
receiver = context["receiver"]
if reply.type in [ReplyType.TEXT, ReplyType.ERROR, ReplyType.INFO]:
self.client.message.send_text(self.agent_id, receiver, reply.content)
logger.info("[wechatcom] sendMsg={}, receiver={}".format(reply, receiver))
reply_text = reply.content
texts = split_string_by_utf8_length(reply_text, MAX_UTF8_LEN)
if len(texts) > 1:
logger.info("[wechatcom] text too long, split into {} parts".format(len(texts)))
for i, text in enumerate(texts):
self.client.message.send_text(self.agent_id, receiver, text)
if i != len(texts) - 1:
time.sleep(0.5) # 休眠0.5秒,防止发送过快乱序
logger.info("[wechatcom] Do send text to {}: {}".format(receiver, reply_text))
elif reply.type == ReplyType.VOICE:
try:
file_path = reply.content