Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions channel/gewechat/gewechat_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,24 @@ def __init__(self, msg, client: GewechatClient):
elif msg_type == 34: # Voice message
self.ctype = ContextType.VOICE
self.content = self.msg_data.get('Content', {}).get('string', '')
silk_file_name = f"voice_{uuid.uuid4()}.silk"
silk_file_path = TmpDir().path() + silk_file_name
if 'ImgBuf' in self.msg_data and 'buffer' in self.msg_data['ImgBuf'] and self.msg_data['ImgBuf']['buffer']:
silk_data = base64.b64decode(self.msg_data['ImgBuf']['buffer'])
silk_file_name = f"voice_{uuid.uuid4()}.silk"
silk_file_path = TmpDir().path() + silk_file_name
with open(silk_file_path, "wb") as f:
f.write(silk_data)
self.content = silk_file_path
else: # 测试语音超过8秒,无法从ImgBuf":{"iLen":0}获取数据,需要从云端下载
try:
res = client.download_voice(self.app_id, self.content, self.msg_data.get("MsgId"))
file_url = res.get('data').get('fileUrl')
with requests.get(file_url, headers={'User-Agent': 'Mozilla/5.0'}, stream=True) as r:
r.raise_for_status()
with open(silk_file_path, 'wb') as f:
for chunk in r.iter_content(1024):
if chunk: f.write(chunk)
except Exception as e:
logger.error(f"[gewechat] download voice error: {e}")
self.content = silk_file_path
elif msg_type == 3: # Image message
self.ctype = ContextType.IMAGE
self.content = TmpDir().path() + str(self.msg_id) + ".png"
Expand Down