From 8224c2fc16b013023c89dc49a8f5ed497cd384f2 Mon Sep 17 00:00:00 2001 From: leeson Date: Sat, 8 Jul 2023 23:58:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E6=9C=8D=E5=8A=A1=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E8=AF=AD=E9=9F=B3=E8=BE=93=E5=87=BA=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=88=87=E5=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/wechatmp/wechatmp_channel.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/channel/wechatmp/wechatmp_channel.py b/channel/wechatmp/wechatmp_channel.py index 0c54a1d..6f21ce1 100644 --- a/channel/wechatmp/wechatmp_channel.py +++ b/channel/wechatmp/wechatmp_channel.py @@ -20,7 +20,7 @@ from common.log import logger from common.singleton import singleton from common.utils import split_string_by_utf8_length from config import conf -from voice.audio_convert import any_to_mp3 +from voice.audio_convert import any_to_mp3, split_audio # If using SSL, uncomment the following lines, and modify the certificate path. # from cheroot.server import HTTPServer @@ -162,13 +162,28 @@ class WechatMPChannel(ChatChannel): file_name = os.path.basename(file_path) file_type = "audio/mpeg" logger.info("[wechatmp] file_name: {}, file_type: {} ".format(file_name, file_type)) - # support: <2M, <60s, AMR\MP3 - response = self.client.media.upload("voice", (file_name, open(file_path, "rb"), file_type)) - logger.debug("[wechatmp] upload voice response: {}".format(response)) + media_ids = [] + duration, files = split_audio(file_path, 60 * 1000) + if len(files) > 1: + logger.info("[wechatmp] voice too long {}s > 60s , split into {} parts".format(duration / 1000.0, len(files))) + for path in files: + # support: <2M, <60s, AMR\MP3 + response = self.client.media.upload("voice", (os.path.basename(path), open(path, "rb"), file_type)) + logger.debug("[wechatcom] upload voice response: {}".format(response)) + media_ids.append(response["media_id"]) + os.remove(path) except WeChatClientException as e: logger.error("[wechatmp] upload voice failed: {}".format(e)) return - self.client.message.send_voice(receiver, response["media_id"]) + + try: + os.remove(file_path) + except Exception: + pass + + for media_id in media_ids: + self.client.message.send_voice(receiver, media_id) + time.sleep(1) logger.info("[wechatmp] Do send voice to {}".format(receiver)) elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片 img_url = reply.content