tanlie 1 tahun lalu
induk
melakukan
242c9689c9
1 mengubah file dengan 14 tambahan dan 10 penghapusan
  1. 14 10
      channel/wechatcom/wechatcomapp_channel.py

+ 14 - 10
channel/wechatcom/wechatcomapp_channel.py

@@ -19,7 +19,7 @@ from common.log import logger
 from common.singleton import singleton
 from common.utils import compress_imgfile, fsize, split_string_by_utf8_length
 from config import conf, subscribe_msg
-from voice.audio_convert import any_to_amr, split_audio, any_to_mp3
+from voice.audio_convert import any_to_amr, split_audio
 
 MAX_UTF8_LEN = 2048
 
@@ -37,11 +37,7 @@ class WechatComAppChannel(ChatChannel):
         self.aes_key = conf().get("wechatcomapp_aes_key")
         print(self.corp_id, self.secret, self.agent_id, self.token, self.aes_key)
         logger.info(
-            "[wechatcom] init: corp_id: {}, secret: {}, agent_id: {}, token: {}, aes_key: {}".format(self.corp_id,
-                                                                                                     self.secret,
-                                                                                                     self.agent_id,
-                                                                                                     self.token,
-                                                                                                     self.aes_key)
+            "[wechatcom] init: corp_id: {}, secret: {}, agent_id: {}, token: {}, aes_key: {}".format(self.corp_id, self.secret, self.agent_id, self.token, self.aes_key)
         )
         self.crypto = WeChatCrypto(self.token, self.aes_key, self.corp_id)
         self.client = WechatComAppClient(self.corp_id, self.secret)
@@ -69,18 +65,26 @@ class WechatComAppChannel(ChatChannel):
             try:
                 media_ids = []
                 file_path = reply.content
-                response = self.client.media.upload("file", open(file_path, "rb"))
-                logger.debug("[wechatcom] upload voice response: {}".format(response))
-                media_ids.append(response["media_id"])
+                amr_file = os.path.splitext(file_path)[0] + ".amr"
+                any_to_amr(file_path, amr_file)
+                duration, files = split_audio(amr_file, 60 * 1000)
+                if len(files) > 1:
+                    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
             try:
                 os.remove(file_path)
+                if amr_file != file_path:
+                    os.remove(amr_file)
             except Exception:
                 pass
             for media_id in media_ids:
-                self.client.message.send_file(self.agent_id, receiver, media_id)
+                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:  # 从网络下载图片