From df3ee777b2af10334dd426a96db87e691c93e220 Mon Sep 17 00:00:00 2001 From: liuwei Date: Thu, 29 May 2025 17:42:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AF=AD=E9=9F=B3=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/dashboard/blueprints/contacts.py | 12 +++++++++++- wechat_ipad/client/message.py | 6 +++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/admin/dashboard/blueprints/contacts.py b/admin/dashboard/blueprints/contacts.py index 59a8517..27dec1b 100644 --- a/admin/dashboard/blueprints/contacts.py +++ b/admin/dashboard/blueprints/contacts.py @@ -266,8 +266,18 @@ def api_send_message(): if 'file' not in request.files: return jsonify({'success': False, 'message': '未上传文件'}) file = request.files['file'] + if file.name.endswith('.mp3'): + format_str = "mp3" + elif file.name.endswith('.wav'): + format_str = "wav" + else: + return jsonify({ + 'success': False, + 'data': { + } + }) client_msg_id, create_time, new_msg_id = loop.run_until_complete( - server.client.send_voice_message(wxid, file.read(), format="mp3") + server.client.send_voice_message(wxid, file.read(), format=format_str) ) return jsonify({ 'success': True, diff --git a/wechat_ipad/client/message.py b/wechat_ipad/client/message.py index 57296fc..41c0677 100644 --- a/wechat_ipad/client/message.py +++ b/wechat_ipad/client/message.py @@ -319,7 +319,7 @@ class MessageMixin(WechatAPIClientBase): if not self.wxid: raise UserLoggedOut("请先登录") - elif format not in ["amr", "wave", "mp3", "silk", "speex"]: + elif format not in ["amr", "wav", "mp3", "silk", "speex"]: raise ValueError("format must be one of amr, wav, mp3") # read voice to byte @@ -337,7 +337,7 @@ class MessageMixin(WechatAPIClientBase): if format.lower() == "amr": audio = AudioSegment.from_file(BytesIO(voice_byte), format="amr") voice_base64 = base64.b64encode(voice_byte).decode() - elif format.lower() == "wave": + elif format.lower() == "wav": audio = AudioSegment.from_file(BytesIO(voice_byte), format="wav").set_channels(1) audio = audio.set_frame_rate(self._get_closest_frame_rate(audio.frame_rate)) voice_base64 = base64.b64encode( @@ -354,7 +354,7 @@ class MessageMixin(WechatAPIClientBase): duration = len(audio) # Type: AMR = 0, MP3 = 2, SILK = 4, SPEEX = 1, WAVE = 3 VoiceTime :音频长度 1000为一秒 - format_dict = {"amr": 0, "wave": 3, "mp3": 2, "silk": 4, "speex": 1} + format_dict = {"amr": 0, "wav": 3, "mp3": 2, "silk": 4, "speex": 1} # { # "Base64": "string", # "ToWxid": "string",