Following my talking clock service from [ here ], let’s go further and create a Weather service you can access when dialing 959!
Same, we’ll use an Ubuntu 26.04 VM on ARM.
Let’s install the required software on Ubuntu:
apt update
apt install -y asterisk curl jq sox libttspico-utils
Then you will need an OpenWeatherMap API key that you can get from: https://openweathermap.org/api
So now, let’s create our Weather Script:
nano /usr/local/bin/weather-chiajna.sh
that should contain:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#!/bin/bash API_KEY="YOUR_OPENWEATHER_API_KEY" CITY="Chiajna" COUNTRY="RO" TMPJSON="/tmp/weather.json" TMPWAV="/tmp/weather.wav" # Fetch current weather curl -s "https://api.openweathermap.org/data/2.5/weather?q=${CITY},${COUNTRY}&units=metric&appid=${API_KEY}" > "$TMPJSON" # Parse JSON response TEMP=$(jq '.main.temp | round' "$TMPJSON") DESC=$(jq -r '.weather[0].description' "$TMPJSON") HUM=$(jq '.main.humidity' "$TMPJSON") # Create spoken text TEXT="The current weather in Chiajna Romania is ${DESC}. Temperature is ${TEMP} degrees Celsius. Humidity is ${HUM} percent." # Generate TTS WAV pico2wave -w="$TMPWAV" "$TEXT" # Convert to Asterisk-compatible WAV format sox "$TMPWAV" -t wav -r 8000 -c 1 -b 16 /var/lib/asterisk/sounds/en/weather.wav # Permissions chown asterisk:asterisk /var/lib/asterisk/sounds/en/weather.wav chmod 644 /var/lib/asterisk/sounds/en/weather.wav |
Make it executable: chmod +x /usr/local/bin/weather-chiajna.sh
I won’t go to the creation of extensions and dialplans, since this was covered in my old post.
Here’s a sample final dialplan:
[from-yeastar]
exten => 959,1,NoOp(Weather service loop)
same => n,Answer()
; Generate initial weather prompt
same => n,System(/usr/local/bin/weather-chiajna.sh)
; Initialize counter
same => n,Set(COUNT=0)
; Playback loop
same => n(loop),Playback(/var/lib/asterisk/sounds/en/weather)
same => n,Wait(5)
; Increase counter
same => n,Set(COUNT=$[${COUNT} + 1])
; Refresh weather every 60 loops (~5 minutes)
same => n,GotoIf($[${COUNT} < 60]?loop)
; Reset counter and refresh weather
same => n,Set(COUNT=0)
same => n,System(/usr/local/bin/weather-chiajna.sh)
; Continue looping
same => n,Goto(loop)
Enjoy!

Leave a Reply