You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

120 lines
3.5 KiB

#!/usr/bin/env python3
"""
飞书 TTS 语音消息发送脚本
1. 生成 TTS MP3
2. 转换为 opus 格式
3. 用正确的格式发送给飞书
"""
import subprocess
import sys
import os
import tempfile
from pathlib import Path
def generate_tts(text: str, output_path: str = None) -> str:
"""
生成 TTS 语音(模拟,实际调用 openclaw tts 工具)
这里我们用简单的方式模拟,实际应该通过 OpenClaw 的 tts 工具
"""
# 注意:这个函数是模拟的,实际应该调用 OpenClaw 的 tts 工具
# 但由于我们无法直接调用 tts 工具,这里用占位符
print(f"[模拟] 生成 TTS: {text}")
# 实际使用时,应该通过 OpenClaw 的 tts 工具
# 这里我们返回一个已有的测试文件
if output_path is None:
output_path = "/tmp/tts-test.mp3"
# 复制刚才的测试文件作为示例
if os.path.exists("/tmp/tts-22wJcJ/voice-1772499274290.mp3"):
import shutil
shutil.copy("/tmp/tts-22wJcJ/voice-1772499274290.mp3", output_path)
else:
# 创建一个空文件作为占位符
with open(output_path, "w") as f:
f.write("")
return output_path
def mp3_to_opus(mp3_path: str, output_path: str = None) -> str:
"""
将 MP3 文件转换为 Opus 格式
"""
if output_path is None:
output_path = str(Path(mp3_path).with_suffix('.opus'))
# 使用 ffmpeg 转换
cmd = [
'ffmpeg',
'-i', mp3_path,
'-c:a', 'libopus',
'-b:a', '64k',
'-vbr', 'on',
'-compression_level', '10',
'-y',
output_path
]
try:
result = subprocess.run(
cmd,
check=True,
capture_output=True,
text=True
)
print(f"✓ 转换成功: {output_path}")
return output_path
except subprocess.CalledProcessError as e:
print(f"✗ 转换失败: {e.stderr}")
raise
def send_to_feishu(opus_path: str, text: str = None):
"""
发送语音消息给飞书
注意:这个函数需要通过 OpenClaw 的 message 工具
实际使用时,应该构造正确的 [[audio_as_voice]] + MEDIA: 格式
"""
print(f"准备发送语音消息: {opus_path}")
print("提示:在 OpenClaw 中,使用以下格式发送:")
print(f"[[audio_as_voice]]")
print(f"MEDIA:{opus_path}")
print()
print("或者使用 message 工具:")
print(f"message send --channel feishu --text '[[audio_as_voice]] MEDIA:{opus_path}'")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("使用方法: python feishu_tts.py <文本>")
print("示例: python feishu_tts.py '老板好,这是一条测试语音消息'")
sys.exit(1)
text = sys.argv[1]
try:
print("=" * 50)
print("飞书 TTS 语音消息发送工具")
print("=" * 50)
print()
# 1. 生成 TTS
print(f"[1/3] 生成 TTS 语音...")
mp3_path = generate_tts(text)
# 2. 转换为 opus
print(f"[2/3] 转换为 opus 格式...")
opus_path = mp3_to_opus(mp3_path)
# 3. 准备发送
print(f"[3/3] 准备发送...")
send_to_feishu(opus_path, text)
print()
print("✓ 完成!")
print(f"Opus 文件: {opus_path}")
except Exception as e:
print(f"✗ 失败: {e}")
import traceback
traceback.print_exc()
sys.exit(1)