Adding Weather to Asterisk / Trixbox
One of the features dropped from the newer releases of Asterisk is the ability to dial an extension and receive the weather. I don't think this is a really important feature but it was a good exercise to have some fun with.
There are a few steps. First, download the weather in text format, and I pulled that from noaa.gov. Point your browser here: http://weather.noaa.gov/pub/data/forecasts/city/, then click on the link for your state, then the closest city to you which will be a text file.
That URL is what you need to cut and paste into the below bash script after the 'wget ' part. What this script does is change to the root directory, download the weather in text format, convert it to a wave file using flite (while saving it on top of weather.wav in the custom sounds directory - I'll get to that in a minute), then finally the two text files are deleted.
#!/bin/bash cd /root wget -O newark.txt "http://www.nws.noaa.gov/view/prodsByState.php?state=NJ&prodtype=summary#RWSOKX" sed -n '/000/,/$$/p' newark.txt > newark-sed.txt tail -n +9 newark-sed.txt > newark-out.txt flite -o /var/lib/asterisk/sounds/custom/weather.wav -f newark-out.txt rm -rf newark*.txt
I put this script into the root directory, and set crontab to execute it every four hours during the day like so:
#weather * 7,13,17 * * * /bin/bash /root/weather.sh &> /dev/null
And that's the easy part. The difficult part is to get asterisk to play that wave file when you dial an extension. No matter what I tried, I couldn't get the weather to play by dialing an extension without creating a custom extension by hand using a text editor, something I'd really like to avoid doing if I can. So, what I did instead was this:
- Load FreePBX in admin mode (signing in of course).
- Select "PBX" then "PBX Settings"
- Select "System Recordings" and upload weather.wav from when you tested your script.
- Give it the name "Weather".
Go to "Ring Groups" and add a new ring group. I picked 699 "just because" and made the following settings:
- Group Description: Weather
- Ring Strategy: ringall
- Ring Time: 60
- Extension List: 9999
- Announcement: weather
- (blah blah)
- Destination if no answer: Terminate Call: HANGUP
Because 699 is a ring group, it's available internally as well as externally just like any other ring group, so you could plug this into your IVR if you want and listen to the weather remotely.
The reason why I used a cron job/script to download / read / convert the weather is because many of the scripts I've seen do this on the fly, and this uses a bit of system resources. Since noaa.gov doesn't update the weather reports 100 times a day, I saw no benefit to downloading the weather and converting it each time. I pictures 50 employees checking the weather at the same time and thought to myself... um no.