Explorar el Código

fix: send voice in wechatcomapp rightly

master
lanvent hace 1 año
padre
commit
fb22e01b89
Se han modificado 2 ficheros con 9 adiciones y 5 borrados
  1. +7
    -3
      channel/wechatcom/wechatcomapp_channel.py
  2. +2
    -2
      voice/audio_convert.py

+ 7
- 3
channel/wechatcom/wechatcomapp_channel.py Ver fichero

@@ -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


+ 2
- 2
voice/audio_convert.py Ver fichero

@@ -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

Cargando…
Cancelar
Guardar