VoiceMessage
语音消息,继承自 Message。
属性
| 属性 | 类型 | 说明 |
|---|---|---|
content | str | 语音描述(如 "3s 语音" 或 "3s 语音: 识别文字") |
sender | str | 发送者 |
source | Source | 消息来源 |
duration | int | 语音时长(秒) |
played | bool | 是否已播放 |
type_label | str | 固定为 "语音消息" |
支持的操作
hover、scroll_to_visible、refresh、quote、collect、revoke、delete
特有方法
play()
播放语音消息。
to_text(timeout=10)
语音转文字。右键菜单点击"转文字",等待识别完成后返回文本。
- timeout
float— 等待转换超时(秒),较长语音可能需更长时间 - 返回
str— 识别的文本,识别失败或超时返回空字符串
fold_text()
收起文字(隐藏语音转文字结果,仅在已转文字后可用)。
示例
python
from pywxauto import Weixin, Event
wx = Weixin()
@wx.on(Event.VOICE)
def on_voice(weixin, chat, message):
print(f"收到语音: {message.duration}秒, 已播放={message.played}")
# 转文字
text = message.to_text(timeout=15)
if text:
print(f"语音内容: {text}")
chat.send_text(f"你说的是: {text}", quote=message)
# 播放
message.play()