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:

Now the wave file is on the Asterisk server in the right place, which happens to be /var/lib/asterisk/sounds/custom/weather.wav. With the cron job running in the background, that wav file will be overwritten at 7am, 1pm, and 5pm daily. That's often enough to have accurate weather considering how often noaa.gov updates. I also don't bother updating at night because I'm sleeping and won't be checking the weather :-D

Go to "Ring Groups" and add a new ring group. I picked 699 "just because" and made the following settings:

The ring strategy is the default, and I set the ring time to 60 because I wanted to allow enough time for the weather.wav to play. The extension list contains a "9999", and that's a bogus extension - it does not exist nor is it even defined anywhere. This is the actual trick - by putting a bogus extension in there no phones will ring, and the weather.wav file will play in entirety *if* the ring time is set to a greater time than the length of the weather.wav, which it should be as flite's reading of the text file takes about 35 seconds. I have the destination set to terminate the call if no answer, and that means as soon as weather.wav is done playing, it hangs up.

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.