name: senseaudio-weather-broadcast description: Fetch weather data and generate a spoken weather broadcast using SenseAudio TTS. metadata: openclaw: emoji: "🌤️" requires: env: - SENSEAUDIO_API_KEY bins: - curl - jq primaryEnv: SENSEAUDIO_API_KEY homepage: https://senseaudio.cn/docs
Fetch real-time weather data and generate a natural-sounding weather broadcast audio using SenseAudio TTS.
獲取即時天氣資料,並使用 SenseAudio TTS 生成自然流暢的天氣播報音訊。
小蔥技能站7w4.net發現了升級外掛。
# Set your API key / 設定 API 金鑰
export SENSEAUDIO_API_KEY="your_api_key"
# Fetch weather and generate broadcast for Beijing / 獲取北京天氣並生成播報
CITY="Beijing"
WEATHER=$(curl -s "wttr.in/${CITY}?format=%l:+%c+%t+%h+%w&lang=zh")
# Generate broadcast text / 生成播報文本
BROADCAST_TEXT="天氣播報:${WEATHER}。祝您出行愉快!"
# Call SenseAudio TTS / 呼叫 SenseAudio TTS
curl -X POST https://api.senseaudio.cn/v1/t2a_v2 \
-H "Authorization: Bearer $SENSEAUDIO_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"SenseAudio-TTS-1.0\",
\"text\": \"$BROADCAST_TEXT\",
\"stream\": false,
\"voice_setting\": {
\"voice_id\": \"male_0004_a\",
\"speed\": 0.95
},
\"audio_setting\": {
\"format\": \"mp3\",
\"sample_rate\": 32000
}
}" -o response.json
# Extract and save audio / 提取並儲存音訊
jq -r '.data.audio' response.json | xxd -r -p > weather_broadcast.mp3
Free weather service, no API key required. / 免費天氣服務,無需 API 金鑰。
# Quick one-liner / 快速查詢
curl -s "wttr.in/Shanghai?format=3"
# Output: Shanghai: ⛅️ +18°C
# Detailed format / 詳細格式
curl -s "wttr.in/Shanghai?format=%l:+%c+%t+%h+%w"
# Output: Shanghai: ⛅️ +18°C 65% ↙12km/h
# Chinese output / 中文輸出
curl -s "wttr.in/上海?lang=zh&format=3"
Format codes / 格式程式碼:
- %c condition / 天氣狀況
- %t temperature / 溫度
- %h humidity / 溼度
- %w wind / 風速
- %l location / 地點
- %m moon phase / 月相
Options / 選項:
- ?lang=zh Chinese / 中文
- ?m metric / 公制
- ?1 today only / 僅今天
- ?0 current only / 僅當前
Free JSON API for programmatic use. / 免費 JSON API,適合程式化使用。
# Get coordinates first, then query / 先獲取座標,再查詢
# Beijing: 39.9, 116.4
curl -s "https://api.open-meteo.com/v1/forecast?latitude=39.9&longitude=116.4¤t_weather=true"
import requests
import os
SENSEAUDIO_API_KEY = os.environ.get("SENSEAUDIO_API_KEY")
TTS_URL = "https://api.senseaudio.cn/v1/t2a_v2"
def get_weather(city: str, lang: str = "zh") -> str:
"""Fetch weather from wttr.in / 從 wttr.in 獲取天氣"""
url = f"https://wttr.in/{city}?format=%l:+%c+%t+%h+%w&lang={lang}"
resp = requests.get(url, timeout=10)
return resp.text.strip()
def generate_broadcast_text(weather: str, lang: str = "zh") -> str:
"""Generate broadcast script / 生成播報文本"""
if lang == "zh":
return f"天氣播報:{weather}。<break time=300>請根據天氣情況合理安排出行,祝您生活愉快!"
else:
return f"Weather report: {weather}. <break time=300>Please plan your day accordingly. Have a great day!"
def text_to_speech(text: str, output_file: str = "weather_broadcast.mp3", voice_id: str = "male_0004_a"):
"""Convert text to speech using SenseAudio TTS / 使用 SenseAudio TTS 轉換文本為語音"""
headers = {
"Authorization": f"Bearer {SENSEAUDIO_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "SenseAudio-TTS-1.0",
"text": text,
"stream": False,
"voice_setting": {
"voice_id": voice_id,
"speed": 0.95,
"vol": 1.0,
"pitch": 0
},
"audio_setting": {
"format": "mp3",
"sample_rate": 32000
}
}
resp = requests.post(TTS_URL, json=payload, headers=headers, timeout=30)
result = resp.json()
if result.get("data") and result["data"].get("audio"):
audio_bytes = bytes.fromhex(result["data"]["audio"])
with open(output_file, "wb") as f:
f.write(audio_bytes)
print(f"Audio saved: {output_file}")
print(f"Duration: {result['extra_info']['audio_length']}ms")
return output_file
else:
raise Exception(f"TTS failed: {result.get('base_resp', {}).get('status_msg', 'Unknown error')}")
def weather_broadcast(city: str, output_file: str = "weather_broadcast.mp3", lang: str = "zh"):
"""Main function: fetch weather and generate broadcast / 主函式:獲取天氣並生成播報"""
print(f"Fetching weather for {city}...")
weather = get_weather(city, lang)
print(f"Weather: {weather}")
broadcast_text = generate_broadcast_text(weather, lang)
print(f"Broadcast text: {broadcast_text}")
print("Generating audio...")
return text_to_speech(broadcast_text, output_file)
if __name__ == "__main__":
# Example usage / 示例用法
weather_broadcast("Beijing", "beijing_weather.mp3", "zh")
# weather_broadcast("London", "london_weather.mp3", "en")
| Voice ID | Description / 描述 | Style / 風格 |
|---|---|---|
male_0004_a |
Warm male / 溫暖男聲 | News anchor / 新聞主播 |
female_0001_a |
Sweet female / 甜美女聲 | Friendly / 親切 |
See SenseAudio Voice List for all available voices.
檢視 SenseAudio 音色列表 獲取所有可用音色。
| Parameter / 引數 | Type / 型別 | Default / 預設 | Range / 範圍 | Description / 描述 |
|---|---|---|---|---|
speed |
float | 1.0 | 0.5-2.0 | Speech rate / 語速 |
vol |
float | 1.0 | 0-10 | Volume / 音量 |
pitch |
int | 0 | -12 to 12 | Pitch adjustment / 音調 |
| Format / 格式 | Sample Rate / 取樣率 | Use Case / 適用場景 |
|---|---|---|
| mp3 | 32000 | General use / 通用 |
| wav | 48000 | High quality / 高品質 |
| pcm | 16000 | IoT devices / 物聯網裝置 |
<break time=500> to add pauses in broadcast / 使用 <break time=500> 在播報中新增停頓speed: 0.95 for clearer broadcast / 設定 speed: 0.95 使播報更清晰New+York / 城市名有空格時需 URL 編碼?lang=zh for Chinese weather descriptions / 使用 ?lang=zh 獲取中文天氣描述try:
weather_broadcast("Beijing")
except requests.exceptions.Timeout:
print("Request timeout, please retry")
except Exception as e:
print(f"Error: {e}")
這是一款實用且文件友好的天氣播報工具,能快速將天氣資訊轉化為語音播報。優點是使用簡單、示例豐富,支援中英文雙語,並提供了備用資料來源保證穩定性。不足之處在於功能相對基礎,缺少自動化配置和錯誤提示機制,對新手使用者不太友好。總體而言,適合有技術基礎的使用者快速上手使用。