mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
个人订阅号长语音支持
This commit is contained in:
@@ -85,22 +85,26 @@ class WechatMPChannel(ChatChannel):
|
|||||||
logger.info("[wechatmp] text cached, receiver {}\n{}".format(receiver, reply_text))
|
logger.info("[wechatmp] text cached, receiver {}\n{}".format(receiver, reply_text))
|
||||||
self.cache_dict[receiver].append(("text", reply_text))
|
self.cache_dict[receiver].append(("text", reply_text))
|
||||||
elif reply.type == ReplyType.VOICE:
|
elif reply.type == ReplyType.VOICE:
|
||||||
try:
|
voice_file_path = reply.content
|
||||||
voice_file_path = reply.content
|
duration, files = split_audio(voice_file_path, 60 * 1000)
|
||||||
with open(voice_file_path, "rb") as f:
|
if len(files) > 1:
|
||||||
# support: <2M, <60s, mp3/wma/wav/amr
|
logger.info("[wechatmp] voice too long {}s > 60s , split into {} parts".format(duration / 1000.0, len(files)))
|
||||||
response = self.client.material.add("voice", f)
|
|
||||||
logger.debug("[wechatmp] upload voice response: {}".format(response))
|
for path in files:
|
||||||
# 根据文件大小估计一个微信自动审核的时间,审核结束前返回将会导致语音无法播放,这个估计有待验证
|
# support: <2M, <60s, mp3/wma/wav/amr
|
||||||
f_size = os.fstat(f.fileno()).st_size
|
try:
|
||||||
time.sleep(1.0 + 2 * f_size / 1024 / 1024)
|
with open(path, "rb") as f:
|
||||||
# todo check media_id
|
response = self.client.media.upload("voice", f)
|
||||||
except WeChatClientException as e:
|
logger.debug("[wechatmp] upload voice response: {}".format(response))
|
||||||
logger.error("[wechatmp] upload voice failed: {}".format(e))
|
f_size = os.fstat(f.fileno()).st_size
|
||||||
return
|
time.sleep(1.0 + 2 * f_size / 1024 / 1024)
|
||||||
media_id = response["media_id"]
|
# todo check media_id
|
||||||
logger.info("[wechatmp] voice uploaded, receiver {}, media_id {}".format(receiver, media_id))
|
except WeChatClientException as e:
|
||||||
self.cache_dict[receiver].append(("voice", media_id))
|
logger.error("[wechatmp] upload voice failed: {}".format(e))
|
||||||
|
return
|
||||||
|
media_id = response["media_id"]
|
||||||
|
logger.info("[wechatmp] voice uploaded, receiver {}, media_id {}".format(receiver, media_id))
|
||||||
|
self.cache_dict[receiver].append(("voice", media_id))
|
||||||
|
|
||||||
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
||||||
img_url = reply.content
|
img_url = reply.content
|
||||||
|
|||||||
Reference in New Issue
Block a user