Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Answering Machine scripting engine

by nodus (Novice)
on Jul 23, 2004 at 19:59 UTC ( [id://376977]=perlquestion: print w/replies, xml ) Need Help??

nodus has asked for the wisdom of the Perl Monks concerning the following question:

This is rough code that is untested (at work, no modem) but I am anxious to see what people think of it. Its goal is to implement a simple scripting interface for vgetty for menu systems and answering machines. I am probably going to add a email notify and a web interface to the messages. This is my first somewhat real perl program so rip it apart :)

this is a sample menu file
play('intro.rmd'); &menu; option(1,sub { &menu; play('menu2.rmd'); option(1, sub { play('explaination.rmd'); }); option(2, sub { play('mesage.rmd'); record('messages');#dir to record to }); }); option(2, sub{ play('blah.rmd'); });
this is the main script that gets run by vgetty
#!/usr/bin/perl -w #ansService cover under the gpl #see COPING for licence info use strict; use Common; use Modem::Vgetty; my %menuHash; my $config; my $time; my $v=&init(); my $dtfm; #One important thing: for most analog modems, #you must set the number of RINGs to wait for #to two (2) or higher (set `rings 2' in #`vgetty.config'), because the ID code is sent #between the first and the second RING. If #mgetty picks up the phone too soon, the modem #can't get this information. $SIG{__DIE__} = sub { # do special handling my $message = $_[0] . "\n"; $message .= "\n\nUser Agent Information\n"; print LOG ($message); Common::display_error_page_and_exit(); }; open LOG,">> /var/log/ansService.log"or die "Cannot create logfile: $! +"; my $caller =$ENV{CALLER_ID}; $time=localtime(time); $time=~s/(\d\d)\s(\d\d):(\d\d).*\d{2}(\d{2})/$2.$3.'.'.$1.'.'.$4/; print LOG "$time ansering service startingup" &menuStart; until($dtfm|){ } $v->wait(10); #**********internal subs********** ###############init############### #initializes vgetty and #creates the $v var ################################## sub init{ my $v = new Modem::Vgetty; $v->add_handler('BUSY_TONE', 'endh', sub { $v->stop; exit(0); }); local $SIG{ALRM} = sub { $v->stop; }; $v->enable_events; $v->autostop('ON'); $v->waitfor('READY'); $v->add_handler('RECEIVED_DTMF', 'readnum',\&read_dtmf); $v->add_handler('HANDSET_OFF_HOOK','picked_up',exit 0); $v->add_handler('BUSY_TONE', 'finish',sub { $voice->stop;exit 0;}) +; $v->enable_events; return $v; } ############menuStart############# #load the config file and eval ################################## sub menuStart{ %menuHash=undef; open CONFIG , '/etc/ansService/menu' or die "cant open /etc/ansService/menu:$! \n"; my @file = <CONFIG>; close CONFIG; my $file1; foreach(@file){ $file1.=$_; } config=file1; eval config or die ; } ############menuChoice############ #do option based on dtfm choice #by execing something in the menu array ################################## sub menuChoice{ if $menuHash{$_[0]} { eval $menuHash{$_[0]}; }else play("notaoption.rmd") } ############read_dtfm############# #reads the dtfm and passes it off to menuChoice #and takes care of most of the error checking ################################## sub read_dtfm { my ($self,$input,$mydtmf) = @_; print "Reading DTFM...$mydtmf\n"; $dtmf = $mydtmf; $self->stop; $self->expect('READY'); &menuChoice($dtfm); $self->stop; $self->shutdown; exit 0; } #*******control structures******** ###############menu############### #clear old menu array and #populate the new one by execing the sub #should do stuff like play a message #then have options ################################## sub menu{ %menuHash=undef; eval @_[0]; } #############option############### #put option in menu array ################################## sub option{ my ($optionNum, $optionSub) = @_; $menuHash{$optionNum}=$optionSub; } ################up################ #go up one level... somehow. ################################## sub up{ } ###############top################ #go to start of menu ################################## sub top{ &menuStart; } #***********opertations*********** ##############record############## #records a message #no handling for multiple folders ################################## sub record{ my ($dir,)=@_; my $filename="/var/lib/ansService/recordings/$dir/$caller-$time.rm +d"; print LOG "recording to: $filename"; $v->beep(50,10); $v->waitfor('READY'); $v->record($filename); alarm(20); $v->stop; } ###############play############### #plays a message ################################## sub play{ $v->play_and_wait("/var/lib/ansService/".$_[0]); $v->expect('READY'); }
if you got this far I thank you

Replies are listed 'Best First'.
Re: Answering Machine scripting engine
by mandog (Curate) on Jul 26, 2004 at 02:46 UTC

    A few quick, random and superficial thoughts:

    • IMNSHO, The &init style is somewhat deprecated in favor of init()
    • use Common looks like an private library
    • my %menuHash; look like semiStudlyCaps which is deprecated in In Chapter 24 of The Camel
    • In perl's before 5.8 overiding the die handler is supposed to be dangerous, (see the Camel) CGI scripts do it to allow errors to the browser. You can achieve the same effect by wrapping die. sub mydie{#do stuff; die $_[0];}
    • my $caller =$ENV{CALLER_ID}; How do you know this environment variable is set at this point in your script?
    • $filename ...should perhaps be a $CONSTANT at the top of your file
    • You might look at past flower box discussion

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://376977]
Approved by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found