|
@@ -19,7 +19,7 @@ from common.log import logger
|
|
|
from common.singleton import singleton
|
|
from common.singleton import singleton
|
|
|
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length
|
|
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length
|
|
|
from config import conf, subscribe_msg
|
|
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
|
|
MAX_UTF8_LEN = 2048
|
|
|
|
|
|
|
@@ -37,11 +37,7 @@ class WechatComAppChannel(ChatChannel):
|
|
|
self.aes_key = conf().get("wechatcomapp_aes_key")
|
|
self.aes_key = conf().get("wechatcomapp_aes_key")
|
|
|
print(self.corp_id, self.secret, self.agent_id, self.token, self.aes_key)
|
|
print(self.corp_id, self.secret, self.agent_id, self.token, self.aes_key)
|
|
|
logger.info(
|
|
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.crypto = WeChatCrypto(self.token, self.aes_key, self.corp_id)
|
|
|
self.client = WechatComAppClient(self.corp_id, self.secret)
|
|
self.client = WechatComAppClient(self.corp_id, self.secret)
|
|
@@ -69,18 +65,26 @@ class WechatComAppChannel(ChatChannel):
|
|
|
try:
|
|
try:
|
|
|
media_ids = []
|
|
media_ids = []
|
|
|
file_path = reply.content
|
|
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:
|
|
except WeChatClientException as e:
|
|
|
logger.error("[wechatcom] upload voice failed: {}".format(e))
|
|
logger.error("[wechatcom] upload voice failed: {}".format(e))
|
|
|
return
|
|
return
|
|
|
try:
|
|
try:
|
|
|
os.remove(file_path)
|
|
os.remove(file_path)
|
|
|
|
|
+ if amr_file != file_path:
|
|
|
|
|
+ os.remove(amr_file)
|
|
|
except Exception:
|
|
except Exception:
|
|
|
pass
|
|
pass
|
|
|
for media_id in media_ids:
|
|
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)
|
|
time.sleep(1)
|
|
|
logger.info("[wechatcom] sendVoice={}, receiver={}".format(reply.content, receiver))
|
|
logger.info("[wechatcom] sendVoice={}, receiver={}".format(reply.content, receiver))
|
|
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|