|
@@ -2,6 +2,7 @@
|
|
|
import io
|
|
import io
|
|
|
import os
|
|
import os
|
|
|
import textwrap
|
|
import textwrap
|
|
|
|
|
+import time
|
|
|
|
|
|
|
|
import requests
|
|
import requests
|
|
|
import web
|
|
import web
|
|
@@ -17,10 +18,12 @@ from channel.wechatcom.wechatcomapp_client import WechatComAppClient
|
|
|
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
|
|
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
|
|
|
from common.log import logger
|
|
from common.log import logger
|
|
|
from common.singleton import singleton
|
|
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 config import conf
|
|
|
from voice.audio_convert import any_to_amr
|
|
from voice.audio_convert import any_to_amr
|
|
|
|
|
|
|
|
|
|
+MAX_UTF8_LEN = 2048
|
|
|
|
|
+
|
|
|
|
|
|
|
|
@singleton
|
|
@singleton
|
|
|
class WechatComAppChannel(ChatChannel):
|
|
class WechatComAppChannel(ChatChannel):
|
|
@@ -50,8 +53,15 @@ class WechatComAppChannel(ChatChannel):
|
|
|
def send(self, reply: Reply, context: Context):
|
|
def send(self, reply: Reply, context: Context):
|
|
|
receiver = context["receiver"]
|
|
receiver = context["receiver"]
|
|
|
if reply.type in [ReplyType.TEXT, ReplyType.ERROR, ReplyType.INFO]:
|
|
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:
|
|
elif reply.type == ReplyType.VOICE:
|
|
|
try:
|
|
try:
|
|
|
file_path = reply.content
|
|
file_path = reply.content
|