Any idea how to read an external file, grab some stuff and push it back into an Asterisk variable? I can do it the other way with: system(echo "${UNIQUEID} =>" >> /home/ast/curr_calls) but I'm a bit stumped on how to go the other way around.... much thanks, Paul Hales
>From: Paul Hales <pdhales@optusnet.com.au> >Subject: [Asterisk-Users] Asterisk question >To: Asterisk Users Mailing List - Non-Commercial Discussion > <asterisk-users@lists.digium.com> >Message-ID: <1141005863.4401.1.camel@localhost.localdomain> >Content-Type: text/plain > >Any idea how to read an external file, grab some stuff and push it back >into an Asterisk variable? > >I can do it the other way with: >system(echo "${UNIQUEID} =>" >> /home/ast/curr_calls) > >but I'm a bit stumped on how to go the other way around.... > >much thanks, > >Paul Hales > > >I'll go out on a limb here and take a guess that it could be done as an AGI script that incorporates SED (http://www.gnu.org/software/sed/) and AWK (http://www.gnu.org/software/gawk/gawk.html). I've used both for some bash scripting in the past. . . Regards, Steve Cayona Super Technologies, Inc.
I was going to see if I can execute a bash script as an AGI - just looking around the internet for examples at the moment. Anybody got an example spare? I'm just a bit stuck on how to start this, but I am quite comfortable writing asterisk dialplan stuff and bash scripts.... later, PaulH> steve <steve@supertec.com> wrote: > > > >From: Paul Hales <pdhales@optusnet.com.au> > >Subject: [Asterisk-Users] Asterisk question > >To: Asterisk Users Mailing List - Non-Commercial Discussion > > <asterisk-users@lists.digium.com> > >Message-ID: <1141005863.4401.1.camel@localhost.localdomain> > >Content-Type: text/plain > > > >Any idea how to read an external file, grab some stuff and push it back > >into an Asterisk variable? > > > >I can do it the other way with: > >system(echo "${UNIQUEID} =>" >> /home/ast/curr_calls) > > > >but I'm a bit stumped on how to go the other way around.... > > > >much thanks, > > > >Paul Hales > > > > > > > I'll go out on a limb here and take a guess that it could be done as an > AGI script that incorporates SED (http://www.gnu.org/software/sed/) and > AWK (http://www.gnu.org/software/gawk/gawk.html). I've used both for > some bash scripting in the past. . . > > Regards, > Steve Cayona > Super Technologies, Inc. > > _______________________________________________ > --Bandwidth and Colocation provided by Easynews.com -- > > Asterisk-Users mailing list > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users
read STDIN while [ "x$STDIN" != "x" ] do export VARNAME=`echo $STDIN | cut -f1 -d ":"` export VARVALUE=`echo "$STDIN" |cut -f2 -d ":" | cut -c2-255` case $VARNAME in ("agi_request") export AGIREQUEST=$VARVALUE;; ("agi_channel") export AGICHANNEL=$VARVALUE;; ("agi_language") export AGILANGUAGE=$VARVALUE;; ("agi_type") export AGITYPE=$VARVALUE;; ("agi_uniqueid") export AGIUNIQUEID=$VARVALUE;; ("agi_callerid") export AGICALLERID="$VARVALUE";; ("agi_calleridname") export AGICALLERIDNAME="$VARVALUE";; ("agi_dnid") export AGIDNID=$VARVALUE;; ("agi_rdnis") export AGIRDNIS=$VARVALUE;; ("agi_context") export AGICONTEXT=$VARVALUE;; ("agi_extension") export AGIEXTENSION=$VARVALUE;; ("agi_priority") export AGIPRIORITY=$VARVALUE;; ("agi_enhanced") export AGIENHANCED=$VARVALUE;; ("agi_accountcode") export AGIACCOUNTCODE=$VARVALUE;; esac read STDIN done } # # You now have all the stuff Asterisk gives you stored in Variables # # # Do what ever you want here: # #> -----Original Message----- > From: asterisk-users-bounces@lists.digium.com > [mailto:asterisk-users-bounces@lists.digium.com] On Behalf Of > pdhales@optusnet.com.au > Sent: Monday, February 27, 2006 10:53 PM > To: Asterisk Users Mailing List - Non-Commercial Discussion > Subject: Re: [Asterisk-Users] Re: Asterisk Question > > > I was going to see if I can execute a bash script as an AGI - > just looking around the internet for examples at the moment. > Anybody got an example spare? > I'm just a bit stuck on how to start this, but I am quite > comfortable writing asterisk dialplan stuff and bash scripts.... > > later, > > PaulH
Paul, Just curious - what kind of stuff are you reading from the file? -MC> -----Original Message----- > From: asterisk-users-bounces@lists.digium.com [mailto:asterisk-users- > bounces@lists.digium.com] On Behalf Of pdhales@optusnet.com.au > Sent: Monday, February 27, 2006 7:53 PM > To: Asterisk Users Mailing List - Non-Commercial Discussion > Subject: Re: [Asterisk-Users] Re: Asterisk Question > > > I was going to see if I can execute a bash script as an AGI - justlooking> around the internet for examples at the moment. > Anybody got an example spare? > I'm just a bit stuck on how to start this, but I am quite comfortable > writing asterisk dialplan stuff and bash scripts.... > > later, > > PaulH > > > steve <steve@supertec.com> wrote: > > > > > > >From: Paul Hales <pdhales@optusnet.com.au> > > >Subject: [Asterisk-Users] Asterisk question > > >To: Asterisk Users Mailing List - Non-Commercial Discussion > > > <asterisk-users@lists.digium.com> > > >Message-ID: <1141005863.4401.1.camel@localhost.localdomain> > > >Content-Type: text/plain > > > > > >Any idea how to read an external file, grab some stuff and push itback> > >into an Asterisk variable? > > > > > >I can do it the other way with: > > >system(echo "${UNIQUEID} =>" >> /home/ast/curr_calls) > > > > > >but I'm a bit stumped on how to go the other way around.... > > > > > >much thanks, > > > > > >Paul Hales > > > > > > > > > > > I'll go out on a limb here and take a guess that it could be done asan> > AGI script that incorporates SED (http://www.gnu.org/software/sed/)and> > AWK (http://www.gnu.org/software/gawk/gawk.html). I've used bothfor> > some bash scripting in the past. . . > > > > Regards, > > Steve Cayona > > Super Technologies, Inc. > > > > _______________________________________________ > > --Bandwidth and Colocation provided by Easynews.com -- > > > > Asterisk-Users mailing list > > To UNSUBSCRIBE or update options visit: > > http://lists.digium.com/mailman/listinfo/asterisk-users > _______________________________________________ > --Bandwidth and Colocation provided by Easynews.com -- > > Asterisk-Users mailing list > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users
> -----Original Message----- > From: asterisk-users-bounces@lists.digium.com [mailto:asterisk-users- > bounces@lists.digium.com] On Behalf Of pdhales@optusnet.com.au > Sent: Monday, February 27, 2006 7:53 PM > To: Asterisk Users Mailing List - Non-Commercial Discussion > Subject: Re: [Asterisk-Users] Re: Asterisk Question > > > I was going to see if I can execute a bash script as an AGI - justlooking> around the internet for examples at the moment. > Anybody got an example spare? > I'm just a bit stuck on how to start this, but I am quite comfortable > writing asterisk dialplan stuff and bash scripts.... > > later, > > PaulH >Paul, I'm a Perl man myself. Here's a simple dialplan extension and corresponding AGI script:
> -----Original Message----- > From: asterisk-users-bounces@lists.digium.com [mailto:asterisk-users- > bounces@lists.digium.com] On Behalf Of pdhales@optusnet.com.au > Sent: Monday, February 27, 2006 7:53 PM > To: Asterisk Users Mailing List - Non-Commercial Discussion > Subject: Re: [Asterisk-Users] Re: Asterisk Question > > > I was going to see if I can execute a bash script as an AGI - justlooking> around the internet for examples at the moment. > Anybody got an example spare? > I'm just a bit stuck on how to start this, but I am quite comfortable > writing asterisk dialplan stuff and bash scripts.... > > later, > > PaulH >Paul, I'm a Perl guy myself. Here's a simple dialplan extension and AGI script written in Perl and using the very cool Asterisk::AGI module: ; AGI test exten => 555,1,Noop(Starting AGI test) exten => 555,n,Answer exten => 555,n,Wait(1) exten => 555,n,Playback(beep) exten => 555,n,AGI(agi_var_test.pl) exten => 555,n,SayDigits(${EXTERN_VAR}) exten => 555,n,Wait(1) exten => 555,n,Playback(beep) exten => 555,n,Hangup Here's the Perl script: #!/usr/bin/perl # # agi_var_test.pl # # Reads in info from file /etc/group # assigns asterisk GID to Asterisk variable EXTERN_VAR # use strict; use warnings; use Asterisk::AGI; # the AGI object my $agi = new Asterisk::AGI; # pull AGI variables into %input my %input = $agi->ReadParse(); my $infile = '/etc/group'; open(FILEIN,"<",$infile) or die "$infile - $!\n"; while(<FILEIN>) { chomp; next unless m/^asterisk/; my @REC = split ":",$_; print STDERR "agi_var_test.pl: Setting EXTERN_VAR to $REC[2]\n"; $agi->set_variable("EXTERN_VAR", $REC[2]); last; } # while(<FILEIN>) close(FILEIN); Basically the script just parses /etc/group until it finds the asterisk entry. It then parses the data line and extracts the GID. Finally, it prints the value to STDERR (for debugging purposes) and then assigns the value to EXTERN_VAR. This is more a proof-of-concept than anything else, but it does show the value of AGI and Asterisk::AGI. -MC
> That's getting pretty close - thanks for that. > > I just couldn't find any decent info on the web about working withAGI.>Ditto. However, I pieced some stuff together by sifting through my well-worn copy of TFOT and bouncing around between the wiki, the sample AGI scripts and asterisk.gnuinter.net. I had never sat down to write an AGI script before - I hadn't needed one - but I thought, "How hard can it be?" Ugh. The Asterisk::AGI module is very handy, and I highly recommend it. I've only written one AGI script in my life (up to now) but I've written 10's of thousands of lines of Perl and I know a good module when I see one. Now, as far as reading one number at a time, are you reading one line from the file at a time? In other words, does your file look something like this: 1237 2340 5434 123 9173 ... I was wondering what you were planning on doing with each number. Were you looking at multiple passes to your AGI script, or were you going to run the script once and collect all of the data in one pass? -MC
> Thanks for this example - it has really got me started! > > Short question - how can I put a variable into my perl script? > > I imagine it's something like > exten => 780,1,AGI(agi_ret_val2.pl|${back}) > > But how can I get my perl script to pick this value up? > > Again - thanks to everyone who has helped me with this. >Paul, You can also set an Asterisk variable and access it with the get_variable method of Asterisk::AGI: extensions.conf: exten => 780,1,SetVar(MyVar=test) exten => 780,n,AGI(agi_ret_val2.pl) In agi_ret_val2.pl: my $var = $agi->get_variable('MyVar'); I'm using this with a proof on concept script. I'll post it tomorrow when I'm done. -MC
> Thanks for this example - it has really got me started! >Paul, I did some tinkering and I think I found something that might be helpful. If not, I did at least learn quite a bit about AGI scripting and dialplan writing! :) Okay, first I created a pretend file with numbers: /tmp/numbers.txt It is simply a list of numbers from 001 to 020, like so: 001 002 003 ... 019 020 Since you have to check the file in reverse order, I have the AGI script read in the values, assign them to an array, @NUMS, then I perform a "reverse" on @NUMS: @NUMS = reverse @NUMS; Now "020" is the first value in the array, or $NUMS[0]. "019" is the second value, or $NUMS[1]. If the number of previous messages that have been read (which I put in the dialplan variable NUM_MSGS_READ) is 0, then the "next" number is referenced by grabbing $NUMS[0]. If the number of previous messages that have been read is 1, then the "next" number is referenced by grabbing $NUMS[1], and so on. I created two different dialplan extensions so I could tinker. The first one hard codes the value of NUM_MSGS_READ to 3. Thus the return value of NEXT_NUM is $NUMS[3], which is 017. The second one lets the caller enter in the value for NEXT_NUM, which is good for testing purposes. I tried to add some validation logic to handle exceptions. I didn't test it thoroughly but hopefully it illustrates the point. Here are the dialplan extensions: ; AGI test2.1 ; hard coded NUM_MSGS_READ ; exten => 555,1,Noop(Starting AGI test) exten => 555,n,Answer exten => 555,n,Wait(1) exten => 555,n,SetVar(NUM_MSGS_READ=3) exten => 555,n,Playback(beep) exten => 555,n,AGI(agi_var_test2.pl) exten => 555,n,GotoIf($[${NEXT_NUM:1} = INVALID ]?invalid:ok) exten => 555,n(ok),SayDigits(${NEXT_NUM}) exten => 555,n,Wait(1) exten => 555,n,Playback(beep) exten => 555,n,Hangup exten => 555,n(invalid),Playback(invalid) exten => 555,n,Hangup ; AGI test2.2 ; For kicks, let the caller set the value of NUM_MSGS_READ ; exten => 556,1,Noop(Starting AGI test) exten => 556,n,Answer ;exten => 556,n,SetVar(NUM_MSGS_READ=X) exten => 556,n,Wait(1) exten => 556,n,Playback(please-enter-the) exten => 556,n,Playback(digits) exten => 556,n,Read(NUM_MSGS_READ,,2,,,10) exten => 556,n,Playback(beep) exten => 556,n,AGI(agi_var_test2.pl) exten => 556,n,GotoIf($[${NEXT_NUM:1} = INVALID ]?invalid:ok) exten => 556,n(ok),SayDigits(${NEXT_NUM}) exten => 556,n,Wait(1) exten => 556,n,Playback(beep) exten => 556,n,Hangup exten => 556,n(invalid),Playback(invalid) exten => 556,n,Hangup exten => 556,t,Playback(vm-goodbye) exten => 556,t,Hangup The Read() application in exten 556 is configured to accept up to two digits with a 10 second digit timeout. On a timeout it plays a goodbye message. In both extensions, there is a test to make sure that NEXT_NUM doesn't contain the string "INVALID" - if it does then we go to a priority and voice that to the caller. The AGI script will return "1INVALID" or "2INVALID" depending upon why the script failed. See the notes in the code for details. Here's the AGI script, agi_var_test2.pl: #!/usr/bin/perl # # agi_var_test2.pl # # Reads in info from file /tmp/numbers.txt into array @NUMS # Uses Asterisk var NUM_MSGS_READ to count from end of @NUMS # Assigns appropriate value from @NUMS to Asterisk var NEXT_NUM # use strict; use warnings; use Asterisk::AGI; # the AGI object my $agi = new Asterisk::AGI; # pull AGI variables into %input my %input = $agi->ReadParse(); my @NUMS = (); # Array to hold data from file my $number; # Value to be sent back to Asterisk my $infile = '/tmp/numbers.txt'; open(FILEIN,"<",$infile) or die "$infile - $!\n"; while(<FILEIN>) { chomp; # Perform some data validation, just to be safe s/^\s|\s+$|^\s+$//; # remove whitespace, just in case next unless length; # skip if nothing left... next unless m/^\d+$/; # skip if line contains any non-digits push @NUMS,$_; } # while(<FILEIN>) close(FILEIN); # reverse array to make it easier to find our value # e.g. last line of file becomes first item in @NUMS: # $NUMS[0] = last item of file # $NUMS[1] = next to last item of file, etc. @NUMS = reverse @NUMS; my $arraypos = $agi->get_variable('NUM_MSGS_READ'); if ( ! $arraypos ) { # if we get to this point, something is wrong... $agi->set_variable('NEXT_NUM','1INVALID'); exit(1); # 1INVALID = AGI script could not read NUM_MSGS_READ } # since @NUMS is zero based, $arraypos = next unread number $number = $NUMS[$arraypos]; if ( $number ) { $agi->set_variable('NEXT_NUM',$number); exit(0); # Normal exit! } else { $agi->set_variable('NEXT_NUM','2INVALID'); exit(1); # 2INVALID = NUM_MSGS_READ larger than number of items in @NUMS } Okay, that's all I've got for now. Let us know how it goes! -MC
> I actually got it all working - but it's great to see where we did the > same > thing, and where we differ. > > I ended up using the 'pop' perl command - inside a loop to go back one > item > at a time through my list.... > > PaulHNice work! Perl = TMTOWTDI = There's More Than One Way To Do It -MC
Not without some dialplan magic. You could have the setgroup for every call, then use groupcount to figure out how many. On 3/5/06, Paul Hales <pdhales@optusnet.com.au> wrote:> > Is there a variable to read to see how many calls are currently open? > (related to channel status?) > > PaulH > > _______________________________________________ > --Bandwidth and Colocation provided by Easynews.com -- > > Asterisk-Users mailing list > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users >