From fb22e01b89f37ac41223ef167853b0c8e709500c Mon Sep 17 00:00:00 2001 From: lanvent Date: Thu, 27 Apr 2023 23:04:24 +0800 Subject: [PATCH] fix: send voice in wechatcomapp rightly --- channel/wechatcom/wechatcomapp_channel.py | 10 +++++++--- voice/audio_convert.py | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/channel/wechatcom/wechatcomapp_channel.py b/channel/wechatcom/wechatcomapp_channel.py index a79ca65..72804e4 100644 --- a/channel/wechatcom/wechatcomapp_channel.py +++ b/channel/wechatcom/wechatcomapp_channel.py @@ -63,15 +63,17 @@ class WechatComAppChannel(ChatChannel): logger.info("[wechatcom] Do send text to {}: {}".format(receiver, reply_text)) elif reply.type == ReplyType.VOICE: try: + media_ids = [] file_path = reply.content amr_file = os.path.splitext(file_path)[0] + ".amr" any_to_amr(file_path, amr_file) - files = split_audio(amr_file, 60000) + duration, files = split_audio(amr_file, 60 * 1000) if len(files) > 1: - logger.info("[wechatcom] voice too long, split into {} parts".format(len(files))) + logger.info("[wechatcom] voice too long {}s > 60s , split into {} parts".format(duration / 1000.0, len(files))) for path in files: response = self.client.media.upload("voice", open(path, "rb")) logger.debug("[wechatcom] upload voice response: {}".format(response)) + media_ids.append(response["media_id"]) except WeChatClientException as e: logger.error("[wechatcom] upload voice failed: {}".format(e)) return @@ -81,7 +83,9 @@ class WechatComAppChannel(ChatChannel): os.remove(amr_file) except Exception: pass - self.client.message.send_voice(self.agent_id, receiver, response["media_id"]) + for media_id in media_ids: + self.client.message.send_voice(self.agent_id, receiver, media_id) + time.sleep(1) logger.info("[wechatcom] sendVoice={}, receiver={}".format(reply.content, receiver)) elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片 img_url = reply.content diff --git a/voice/audio_convert.py b/voice/audio_convert.py index 4b72d6b..9c9515a 100644 --- a/voice/audio_convert.py +++ b/voice/audio_convert.py @@ -111,7 +111,7 @@ def split_audio(file_path, max_segment_length_ms=60000): audio = AudioSegment.from_file(file_path) audio_length_ms = len(audio) if audio_length_ms <= max_segment_length_ms: - return [file_path] + return audio_length_ms, [file_path] segments = [] for start_ms in range(0, audio_length_ms, max_segment_length_ms): end_ms = min(audio_length_ms, start_ms + max_segment_length_ms) @@ -124,4 +124,4 @@ def split_audio(file_path, max_segment_length_ms=60000): path = f"{file_prefix}_{i+1}" + f".{format}" segment.export(path, format=format) files.append(path) - return files + return audio_length_ms, files