asterisk at home festival weather configuration

MP3 Audio directly from NOAA! Updated URLs on 12-15-06

If you are in the San Diego NOAA Office area (and maybe others, check your regional NOAA Office homepage from http://weather.gov/), you can get an audio file of the current forecast for your zone and play it directly.

For the San Diego NOAA Office area, go to http://newweb.wrh.noaa.gov/sgx/ and click on the Audio selection under Forecasts. Get the URL of the audio file for your zone from the map.

Create an announcement in wav format from: http://www.research.att.com/~ttsweb/tts/demo.php
I like the UK-Audrey voice, but try some others too.
Make something like:
latest-forecast.wav "Please wait while I download the latest forecast"
select-weatherorvmc.wav "Press 1 for voicemail press 2 for the latest weather forecast"
You will have to convert the wav files from 16000Hz to 8000Hz and maybe speed them up a bit (135%) and reduce the volume (60%) before you use them. Put them in /var/lib/asterisk/sounds named latest-forecast.wav and select-weatherorvmc.wav

Add something like this to your extensions.conf (maybe in the [rans] section) to get the latest forecast when you dial whatever extension (like 865) that you choose.

My extensions.conf for audio weather:

exten => 865,1,Answer
exten => 865,2,Playback(latest-forecast)
exten => 865,3,System(/usr/bin/curl -s http://www.wrh.noaa.gov/sgx/data/audio/LAXSAFC42.mp3 > /tmp/LAXSAFC42.mp3)
exten => 865,4,Wait(1)
exten => 865,5,MP3Player(/tmp/LAXSAFC42.mp3)
exten => 865,6,System(rm /tmp/LAXSAFC42.mp3 -f) 
exten => 865,7,Hangup


If you have trouble, make sure you have curl installed, change the path above if necessary. find / -type f -name "curl" will find your curl executable. I'm using MP3Player instead of Playback because I don't have a sound card in the Asterisk box so mpg123 fails when it tries to use /dev/dsp. I suppose there is a way to have a dummy dev, but I don't know how. If you do use Playback instead of MP3Player, don't use the .mp3 file extension in your extensions.conf.

You can also do something like this in extensions.conf to be able to dial in and select the weather forecast.

My extensions.conf for audio weather:

[incoming]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,DigitTimeout,5
exten => s,4,ResponseTimeout,9
;exten => s,5,Dial(${CONSOLE},20)
exten => s,5,Dial(IAX2/3030,20)
exten => s,6,Voicemail(u3030)
exten => a,1,Background(select-weatherorvmc) ; Select 1-Voicemail, 2-Weather, 3- )
exten => 1,1,Goto(rans,8500,1)      ; Goto VoicemailMain
exten => 2,1,Goto(rans,865,2)       ; Goto Weather Forecast
;exten => a,1,Playback(select-weatherorvmc) ; Testing
;exten => a,2,Goto(rans,865,2)       ; Goto Weather Forecast
exten => t,1,Hangup
exten => i,1,Playback(invalid)	
exten => i,2,Hangup


~~~~~
Sweet, Now I can get the surf report too! Thanks for the pointer and to the NOAA. Good to see my tax money going to something useful.
~~~~~


I never was able to make this agi/pl script work on my asterisk box, but I did come up with an alternative configuration that does not use agi or scripting. Here it is:

First log into ftp://weather.noaa.gov/data/forecasts/city/ with an ftp client and browse to the appropriate weather file for example for my weather file is ftp://weather.noaa.gov/data/forecasts/city/ne/omaha.txt.

next create a bash file in your /usr/bin directory called convert
  1. !/bin/bash
/usr/bin/text2wave /tmp/weather.txt -F 8000 -o /tmp/weather.wav

make the file executable

chmod 755 /usr/bin/convert

next modify your extensions.conf

exten => *65,1,Answer 
exten => *65,2,Playback(national-weather-service)
exten => *65,3,System(/usr/bin/curl -s ftp://weather.noaa.gov/data/forecasts/city/ne/omaha.txt > /tmp/weather.txt)
exten => *65,4,Wait(1)
exten => *65,5,System(/usr/bin/convert)
exten => *65,6,Playback(/tmp/weather)
exten => *65,7,System(rm /tmp/weather.* -f)
exten => *65,8,Hangup



Reload your asterisk config
asterisk -rx reload

Dial *65 and listen to your weather report.

Any questions email me at hdevito@mchsi.com with subject 'asterisk consulting'

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


This script was developed by Mark Johnson.
All I did (Dean Collins) was type up the instructions and make it easy to understand.

This Script will allow you to dial an extension number on your Asterisk@home V0.6 or later pabx and have it read you the weather for your city.

How it does this is:
  1. When you dial your extension it causes the weather.agi script to run
  2. This makes the program net::ftp connect to the internet and download a text file from the weather.noaa.gov computer
  3. Festival then 'reads' this text file out to you

Net::ftp is now part of the perl library so you don't need to download this
Festival is installed from V0.6 or later of Asterisk@home so you dont need to download this

You need to work out which text file is suitable for yourself and modify the text below
my $custpath = "city/oh";
my $filename = "youngstown.txt";


The way I worked out what was suitable for me in NY, new_york was to log in via a ftp client and manually browse through to the appropriate folder. Now you have all the appropriate information  lets start.

Step 1 - Copy the text below into Notepad and name it weather.agi

 #!/usr/bin/perl -w

use Asterisk::AGI;
use Net::FTP;

my $AGI = new Asterisk::AGI;
my $server = "weather.noaa.gov";
my $basepath = "data/forecasts";
my $custpath = "city/oh";
my $filename = "youngstown.txt";

my @lines;

$AGI->exec('AGI',"festival-weather-script.pl|\"Please wait while I get the latest forecast.\"");

$ftp = Net::FTP->new($server, Debug => 0)
        or die "Cannot connect to $server: $@";


$ftp->login("anonymous","a\@b.com")
        or die "Cannot login with anonymous/a\@b.com", $ftp->message;


$ftp->cwd("$basepath/$custpath")
        or die "Cannot change working directory ", $ftp->message;

$ftp->binary;

$ftp->get($filename,"/tmp/$filename.$$") || die "Error while downloading /tmp/$filename.$$!!!\n", $ftp->message;

open(INPUT,"</tmp/$filename.$$") || die "Could not open /tmp/$filename.$$\n";
while(<INPUT>) {
            ~np~if(grep(/^\./,$_)) {~/np~
                        chomp($_);
                        ~np~$_ =~ s/^\.//;~/np~
                        push(@lines,$_);
                        print "$_\n";
            }
}

foreach(@lines) {
            $AGI->exec('AGI',"festival-weather-script.pl|\"$_\"");
}

$AGI->exec('AGI',"festival-weather-script.pl|\"Thank you and please call again!\"");

unlink("/tmp/$filename.$$");

exit;


Step 2 - Copy the text below into a text editor (Notepad) and name it festival-weather-script.pl
#!/usr/bin/perl

 #make a tts dir inside your sounds dir (as specified below)
 #adjust the t2wp variable to point to your festival bin directory

 use Asterisk::AGI;
 use File::Basename;
 use Digest::MD5 qw(md5_hex);
  
 $AGI = new Asterisk::AGI;
 
 my %input = $AGI->ReadParse();

 my ($text)=@ARGV;
 my $hash = md5_hex($text);
 my $sounddir = "/var/lib/asterisk/sounds/tts";
 my $wavefile = "$sounddir/"."tts-$hash.wav";
 my $t2wp= "/usr/bin/";
  
 unless (-f $wavefile) {
         open(fileOUT, ">$sounddir"."/say-text-$hash.txt");
         print fileOUT "$text";
         close(fileOUT);
         my $execf=$t2wp."text2wave $sounddir/say-text-$hash.txt -F 8000 -o $wavefile";
         system($execf);
        unlink($sounddir."/say-text-$hash.txt");
 }
 $AGI->stream_file('tts/'.basename($wavefile,".wav"),1);


STEP 3 - Copy the 2 files into your /var/lib/asterisk/agi-bin directory
I use a program called WinSCP to do this. (It looks just like a ftp program but allows you to securely swap files between your windows pc and asterisk server.)

STEP 4 - Create an extension number for your weather report.
I use Extension 850. The reason I use this is because in the asterisk@home installations AMP uses the 800 series for the Meetme rooms. As this is similar to a Meetme function I'm going to put all of my festival extensions from 850 onwards. Be carefule you dont have an extension on your pabx called 50 (because AMP will over write this Weather code with the meetme room number 850 for extension 50)

A. Log into Asterisk@home using a browser http://-asterisk-ip-adress/admin

Then click on Maintenance
Then click on ConfigEdit
Then click on Extensions.conf

Now you need to scroll down to where you see the [ext-meetme] context about � of the way down the file
[ext-meetme]
exten => _8X,1,Answer
exten => _8X,2,Wait(1)
exten => _8X,3,GotoIf($[${CALLERIDNUM} = ${EXTEN:1}]?5:4)
exten => _8X,4,MeetMe2(${EXTEN}|sM)
exten => _8X,5,MeetMe2(${EXTEN}|asM)

etc
etc
etc


B. Then add the following text at the end of the context

exten => 850,1,Answer
exten => 850,2,AGI(weather.agi);
exten => 850,3,Hangup


Thats it, reboot your pc (or just reload asterisk), dial 850 and listen to the weather.
Any problems copy and paste your cli with any errors.


Cheers,
Dean

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


I got sick of using the weather of "nearest" city from noaa ftp site since it did not have file for my city. After couple of days of hit and trial (i am novice in perl/asterisk/agi) I finally got the real time weather for MY city working. This script uses the wonderful perl module developed by Mina Naguib (http://mina.naguib.ca)
Here are the steps
1) Go to http://search.cpan.org/~mnaguib/Weather-Underground-3.02/Underground.pm
2) copy the link on upper right side for Weather-Underground-3.02.tar.gz
3) run following commands on your asterisk linux box

cd /tmp
wget http://search.cpan.org/CPAN/authors/id/M/MN/MNAGUIB/Weather-Underground-3.02.tar.gz
gunzip Weather-Underground-3.02.tar.gz
tar -xvf Weather-Underground-3.02.tar
cd Weather-Underground-3.02
perl Makefile.PL
make
make test
make install
cd /var/lib/asterisk/agi-bin

4) now edit your weather.agi using your favourite edit to

  1. !/usr/bin/perl -w

use Asterisk::AGI;
use Weather::Underground;

my $AGI = new Asterisk::AGI;

$weather = Weather::Underground->new(
place => "94555",
debug => 0
) || die "Error, could not create new weather object: $@\n";

$arrayref = $weather->get_weather()
|| die "Error, calling get_weather() failed: $@\n";

$AGI->exec('AGI',"festival-weather-script.pl|\"The latest weather forecast for $arrayref->[0]->{pl
ace}. Press 1 to skip ahead.\"");

$currentWeather = "Current weather for $arrayref->[0]->{place} is as follows. Current temperature
is $arrayref->[0]->{temperature_fahrenheit} degrees fahrenheit or $arrayref->[0]->{temperature_celsius} de
grees celcius. Currently it is $arrayref->[0]->{clouds} and weather conditions are $arrayref->[0]->{condit
ions}. For today moonrise time is $arrayref->[0]->{moonrise}\n\n";

$AGI->exec('AGI',"festival-weather-script.pl|\"$currentWeather\"");

$AGI->exec('AGI',"festival-weather-script.pl|\"That concludes the extended weather forcast.\"");

unlink("/tmp/$filesname.$$");

exit;

5) make sure that you put your zip code instead of 94555 in above script and save it

Now that should get weather customized for your city and in real time (not some local cached file)

Cheers
Mukesh Aggarwal



Here is another weather script using ATT's TTS and NOAA.

  1. !/usr/bin/perl -w
use Asterisk::AGI;
use LWP::Simple;
use URI::Escape;

use HTTP: :Request: :Common;

Remove the extra spaces between the colons.(tiki kept centering the text with out them)
use LWP::UserAgent;
use Net::FTP;

$AGI = new Asterisk::AGI;

my $server = "weather.noaa.gov";
my $basepath = "data/forecasts";
my $custpath = "city/ks";
my $filename = "chanute.txt";


$ftp = Net::FTP->new($server,Debug => 0)
	or die "Cannot Connect to NOAA Server: $@";

$ftp->login("anonymous","a\@b.com")
	or die "Cannot login with anonymous";

$ftp->cwd("$basepath/$custpath")
	or die "invalid directory";

$ftp->binary;


$ftp->get($filename,"$filename") || die "Error Downloading ";


my $command = "perl -i.old -ne 'print unless 1 .. 5' $filename";

system($command);

open (MYFILE, 'chanute.txt');

while (<MYFILE>) {

chomp;

$wt = $wt . "$_\n";

}

close (MYFILE);

$currentWeather = $wt;

my $url = 'http://morrissey.naturalvoices.com/tts/cgi-bin/nph-talk';
    my $req = HTTP::Request->new('POST', $url);
    $req->content('txt=' . uri_escape($currentWeather) .
                  '&voice=mike&speakButton=SPEAK');
    $req->content_type('application/x-www-form-urlencoded');
    my $res = LWP::UserAgent->new->simple_request($req);

    
    unless ($res->is_success || $res->code == 301) {
       die "Error connecting to TTS server: " . $res->status_line . ".\n"; }

    
    if ($res->content !~ /can be found <A HREF=([^>]*)>here<\/A>/i) {
       die "Response from TTS server not understood. Odd.\n"; }


    my $wav_url  = "http://morrissey.naturalvoices.com$1";
    my $wav_file = $1; # for use in saving to disk.
    $wav_file =~ s/.*?\/(\w+.wav)/$1/;
    getstore($wav_url, "/tmp/$wav_file") or
     die "Download of $wav_file failed: $!";



my $wCleanup = "sox /tmp/$wav_file -r 8000 -c 1 -s -w /var/lib/asterisk/sounds/weather.wav";

system($wCleanup);


Stephen - RTS
MP3 Audio directly from NOAA! Updated URLs on 12-15-06

If you are in the San Diego NOAA Office area (and maybe others, check your regional NOAA Office homepage from http://weather.gov/), you can get an audio file of the current forecast for your zone and play it directly.

For the San Diego NOAA Office area, go to http://newweb.wrh.noaa.gov/sgx/ and click on the Audio selection under Forecasts. Get the URL of the audio file for your zone from the map.

Create an announcement in wav format from: http://www.research.att.com/~ttsweb/tts/demo.php
I like the UK-Audrey voice, but try some others too.
Make something like:
latest-forecast.wav "Please wait while I download the latest forecast"
select-weatherorvmc.wav "Press 1 for voicemail press 2 for the latest weather forecast"
You will have to convert the wav files from 16000Hz to 8000Hz and maybe speed them up a bit (135%) and reduce the volume (60%) before you use them. Put them in /var/lib/asterisk/sounds named latest-forecast.wav and select-weatherorvmc.wav

Add something like this to your extensions.conf (maybe in the [rans] section) to get the latest forecast when you dial whatever extension (like 865) that you choose.

My extensions.conf for audio weather:

exten => 865,1,Answer
exten => 865,2,Playback(latest-forecast)
exten => 865,3,System(/usr/bin/curl -s http://www.wrh.noaa.gov/sgx/data/audio/LAXSAFC42.mp3 > /tmp/LAXSAFC42.mp3)
exten => 865,4,Wait(1)
exten => 865,5,MP3Player(/tmp/LAXSAFC42.mp3)
exten => 865,6,System(rm /tmp/LAXSAFC42.mp3 -f) 
exten => 865,7,Hangup


If you have trouble, make sure you have curl installed, change the path above if necessary. find / -type f -name "curl" will find your curl executable. I'm using MP3Player instead of Playback because I don't have a sound card in the Asterisk box so mpg123 fails when it tries to use /dev/dsp. I suppose there is a way to have a dummy dev, but I don't know how. If you do use Playback instead of MP3Player, don't use the .mp3 file extension in your extensions.conf.

You can also do something like this in extensions.conf to be able to dial in and select the weather forecast.

My extensions.conf for audio weather:

[incoming]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,DigitTimeout,5
exten => s,4,ResponseTimeout,9
;exten => s,5,Dial(${CONSOLE},20)
exten => s,5,Dial(IAX2/3030,20)
exten => s,6,Voicemail(u3030)
exten => a,1,Background(select-weatherorvmc) ; Select 1-Voicemail, 2-Weather, 3- )
exten => 1,1,Goto(rans,8500,1)      ; Goto VoicemailMain
exten => 2,1,Goto(rans,865,2)       ; Goto Weather Forecast
;exten => a,1,Playback(select-weatherorvmc) ; Testing
;exten => a,2,Goto(rans,865,2)       ; Goto Weather Forecast
exten => t,1,Hangup
exten => i,1,Playback(invalid)	
exten => i,2,Hangup


~~~~~
Sweet, Now I can get the surf report too! Thanks for the pointer and to the NOAA. Good to see my tax money going to something useful.
~~~~~


I never was able to make this agi/pl script work on my asterisk box, but I did come up with an alternative configuration that does not use agi or scripting. Here it is:

First log into ftp://weather.noaa.gov/data/forecasts/city/ with an ftp client and browse to the appropriate weather file for example for my weather file is ftp://weather.noaa.gov/data/forecasts/city/ne/omaha.txt.

next create a bash file in your /usr/bin directory called convert
  1. !/bin/bash
/usr/bin/text2wave /tmp/weather.txt -F 8000 -o /tmp/weather.wav

make the file executable

chmod 755 /usr/bin/convert

next modify your extensions.conf

exten => *65,1,Answer 
exten => *65,2,Playback(national-weather-service)
exten => *65,3,System(/usr/bin/curl -s ftp://weather.noaa.gov/data/forecasts/city/ne/omaha.txt > /tmp/weather.txt)
exten => *65,4,Wait(1)
exten => *65,5,System(/usr/bin/convert)
exten => *65,6,Playback(/tmp/weather)
exten => *65,7,System(rm /tmp/weather.* -f)
exten => *65,8,Hangup



Reload your asterisk config
asterisk -rx reload

Dial *65 and listen to your weather report.

Any questions email me at hdevito@mchsi.com with subject 'asterisk consulting'

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


This script was developed by Mark Johnson.
All I did (Dean Collins) was type up the instructions and make it easy to understand.

This Script will allow you to dial an extension number on your Asterisk@home V0.6 or later pabx and have it read you the weather for your city.

How it does this is:
  1. When you dial your extension it causes the weather.agi script to run
  2. This makes the program net::ftp connect to the internet and download a text file from the weather.noaa.gov computer
  3. Festival then 'reads' this text file out to you

Net::ftp is now part of the perl library so you don't need to download this
Festival is installed from V0.6 or later of Asterisk@home so you dont need to download this

You need to work out which text file is suitable for yourself and modify the text below
my $custpath = "city/oh";
my $filename = "youngstown.txt";


The way I worked out what was suitable for me in NY, new_york was to log in via a ftp client and manually browse through to the appropriate folder. Now you have all the appropriate information  lets start.

Step 1 - Copy the text below into Notepad and name it weather.agi

 #!/usr/bin/perl -w

use Asterisk::AGI;
use Net::FTP;

my $AGI = new Asterisk::AGI;
my $server = "weather.noaa.gov";
my $basepath = "data/forecasts";
my $custpath = "city/oh";
my $filename = "youngstown.txt";

my @lines;

$AGI->exec('AGI',"festival-weather-script.pl|\"Please wait while I get the latest forecast.\"");

$ftp = Net::FTP->new($server, Debug => 0)
        or die "Cannot connect to $server: $@";


$ftp->login("anonymous","a\@b.com")
        or die "Cannot login with anonymous/a\@b.com", $ftp->message;


$ftp->cwd("$basepath/$custpath")
        or die "Cannot change working directory ", $ftp->message;

$ftp->binary;

$ftp->get($filename,"/tmp/$filename.$$") || die "Error while downloading /tmp/$filename.$$!!!\n", $ftp->message;

open(INPUT,"</tmp/$filename.$$") || die "Could not open /tmp/$filename.$$\n";
while(<INPUT>) {
            ~np~if(grep(/^\./,$_)) {~/np~
                        chomp($_);
                        ~np~$_ =~ s/^\.//;~/np~
                        push(@lines,$_);
                        print "$_\n";
            }
}

foreach(@lines) {
            $AGI->exec('AGI',"festival-weather-script.pl|\"$_\"");
}

$AGI->exec('AGI',"festival-weather-script.pl|\"Thank you and please call again!\"");

unlink("/tmp/$filename.$$");

exit;


Step 2 - Copy the text below into a text editor (Notepad) and name it festival-weather-script.pl
#!/usr/bin/perl

 #make a tts dir inside your sounds dir (as specified below)
 #adjust the t2wp variable to point to your festival bin directory

 use Asterisk::AGI;
 use File::Basename;
 use Digest::MD5 qw(md5_hex);
  
 $AGI = new Asterisk::AGI;
 
 my %input = $AGI->ReadParse();

 my ($text)=@ARGV;
 my $hash = md5_hex($text);
 my $sounddir = "/var/lib/asterisk/sounds/tts";
 my $wavefile = "$sounddir/"."tts-$hash.wav";
 my $t2wp= "/usr/bin/";
  
 unless (-f $wavefile) {
         open(fileOUT, ">$sounddir"."/say-text-$hash.txt");
         print fileOUT "$text";
         close(fileOUT);
         my $execf=$t2wp."text2wave $sounddir/say-text-$hash.txt -F 8000 -o $wavefile";
         system($execf);
        unlink($sounddir."/say-text-$hash.txt");
 }
 $AGI->stream_file('tts/'.basename($wavefile,".wav"),1);


STEP 3 - Copy the 2 files into your /var/lib/asterisk/agi-bin directory
I use a program called WinSCP to do this. (It looks just like a ftp program but allows you to securely swap files between your windows pc and asterisk server.)

STEP 4 - Create an extension number for your weather report.
I use Extension 850. The reason I use this is because in the asterisk@home installations AMP uses the 800 series for the Meetme rooms. As this is similar to a Meetme function I'm going to put all of my festival extensions from 850 onwards. Be carefule you dont have an extension on your pabx called 50 (because AMP will over write this Weather code with the meetme room number 850 for extension 50)

A. Log into Asterisk@home using a browser http://-asterisk-ip-adress/admin

Then click on Maintenance
Then click on ConfigEdit
Then click on Extensions.conf

Now you need to scroll down to where you see the [ext-meetme] context about � of the way down the file
[ext-meetme]
exten => _8X,1,Answer
exten => _8X,2,Wait(1)
exten => _8X,3,GotoIf($[${CALLERIDNUM} = ${EXTEN:1}]?5:4)
exten => _8X,4,MeetMe2(${EXTEN}|sM)
exten => _8X,5,MeetMe2(${EXTEN}|asM)

etc
etc
etc


B. Then add the following text at the end of the context

exten => 850,1,Answer
exten => 850,2,AGI(weather.agi);
exten => 850,3,Hangup


Thats it, reboot your pc (or just reload asterisk), dial 850 and listen to the weather.
Any problems copy and paste your cli with any errors.


Cheers,
Dean

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


I got sick of using the weather of "nearest" city from noaa ftp site since it did not have file for my city. After couple of days of hit and trial (i am novice in perl/asterisk/agi) I finally got the real time weather for MY city working. This script uses the wonderful perl module developed by Mina Naguib (http://mina.naguib.ca)
Here are the steps
1) Go to http://search.cpan.org/~mnaguib/Weather-Underground-3.02/Underground.pm
2) copy the link on upper right side for Weather-Underground-3.02.tar.gz
3) run following commands on your asterisk linux box

cd /tmp
wget http://search.cpan.org/CPAN/authors/id/M/MN/MNAGUIB/Weather-Underground-3.02.tar.gz
gunzip Weather-Underground-3.02.tar.gz
tar -xvf Weather-Underground-3.02.tar
cd Weather-Underground-3.02
perl Makefile.PL
make
make test
make install
cd /var/lib/asterisk/agi-bin

4) now edit your weather.agi using your favourite edit to

  1. !/usr/bin/perl -w

use Asterisk::AGI;
use Weather::Underground;

my $AGI = new Asterisk::AGI;

$weather = Weather::Underground->new(
place => "94555",
debug => 0
) || die "Error, could not create new weather object: $@\n";

$arrayref = $weather->get_weather()
|| die "Error, calling get_weather() failed: $@\n";

$AGI->exec('AGI',"festival-weather-script.pl|\"The latest weather forecast for $arrayref->[0]->{pl
ace}. Press 1 to skip ahead.\"");

$currentWeather = "Current weather for $arrayref->[0]->{place} is as follows. Current temperature
is $arrayref->[0]->{temperature_fahrenheit} degrees fahrenheit or $arrayref->[0]->{temperature_celsius} de
grees celcius. Currently it is $arrayref->[0]->{clouds} and weather conditions are $arrayref->[0]->{condit
ions}. For today moonrise time is $arrayref->[0]->{moonrise}\n\n";

$AGI->exec('AGI',"festival-weather-script.pl|\"$currentWeather\"");

$AGI->exec('AGI',"festival-weather-script.pl|\"That concludes the extended weather forcast.\"");

unlink("/tmp/$filesname.$$");

exit;

5) make sure that you put your zip code instead of 94555 in above script and save it

Now that should get weather customized for your city and in real time (not some local cached file)

Cheers
Mukesh Aggarwal



Here is another weather script using ATT's TTS and NOAA.

  1. !/usr/bin/perl -w
use Asterisk::AGI;
use LWP::Simple;
use URI::Escape;

use HTTP: :Request: :Common;

Remove the extra spaces between the colons.(tiki kept centering the text with out them)
use LWP::UserAgent;
use Net::FTP;

$AGI = new Asterisk::AGI;

my $server = "weather.noaa.gov";
my $basepath = "data/forecasts";
my $custpath = "city/ks";
my $filename = "chanute.txt";


$ftp = Net::FTP->new($server,Debug => 0)
	or die "Cannot Connect to NOAA Server: $@";

$ftp->login("anonymous","a\@b.com")
	or die "Cannot login with anonymous";

$ftp->cwd("$basepath/$custpath")
	or die "invalid directory";

$ftp->binary;


$ftp->get($filename,"$filename") || die "Error Downloading ";


my $command = "perl -i.old -ne 'print unless 1 .. 5' $filename";

system($command);

open (MYFILE, 'chanute.txt');

while (<MYFILE>) {

chomp;

$wt = $wt . "$_\n";

}

close (MYFILE);

$currentWeather = $wt;

my $url = 'http://morrissey.naturalvoices.com/tts/cgi-bin/nph-talk';
    my $req = HTTP::Request->new('POST', $url);
    $req->content('txt=' . uri_escape($currentWeather) .
                  '&voice=mike&speakButton=SPEAK');
    $req->content_type('application/x-www-form-urlencoded');
    my $res = LWP::UserAgent->new->simple_request($req);

    
    unless ($res->is_success || $res->code == 301) {
       die "Error connecting to TTS server: " . $res->status_line . ".\n"; }

    
    if ($res->content !~ /can be found <A HREF=([^>]*)>here<\/A>/i) {
       die "Response from TTS server not understood. Odd.\n"; }


    my $wav_url  = "http://morrissey.naturalvoices.com$1";
    my $wav_file = $1; # for use in saving to disk.
    $wav_file =~ s/.*?\/(\w+.wav)/$1/;
    getstore($wav_url, "/tmp/$wav_file") or
     die "Download of $wav_file failed: $!";



my $wCleanup = "sox /tmp/$wav_file -r 8000 -c 1 -s -w /var/lib/asterisk/sounds/weather.wav";

system($wCleanup);


Stephen - RTS
Created by: dean.collins, Last modification: Tue 11 of Sep, 2012 (00:17 UTC) by admin
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+