📜 ⬆️ ⬇️

Asterisk Voice Tree with Perl AGI

Good afternoon, dear readers.

I want to share my example of creating a voice menu for Asterisk using Perl AGI.

After reading the Voice of the Asterisk article with my own hands , I had a few comments on the method of implementing the voice menu proposed in this article.
My main complaint is that the caller is forced to listen to the menu to the end, and only then we let him press one of the proposed keys. If he makes a mistake by pressing the wrong key, he will have to listen to the menu again.
It seems to me that the subscriber will quickly get bored.
')
Also, the company is often called by people who already know the internal number of the employee. It is necessary to give them the opportunity to dial the number, without waiting for the end of the voice menu.

In addition, almost standard is pressing the "zero" key to call the secretary.
If the subscriber does not want to listen to the menu, but wants to immediately speak with the secretary - I think we can allow him that.

I will describe the rest in the given example.


Asterisk works for me on FreeBSD. Therefore, the ways that I will indicate in the future, you may differ.

I assume that you have already installed Asterisk.
You will also need an installed Perl and Asterisk module: AGI

I won’t begin to describe the installation here for a long time, I’ll just add that if Perl is already installed, then Asterisk: AGI is installed with one command:
cpan -i Asterisk:AGI 

After which we can use it in our scripts.

So, here are the snippets from the Asterisk configuration files:
extensions.conf
 [default] exten => 84951223344,1,Macro(call-in,${EXTEN}) ;      call-in exten => _X.,1,Hangup ; 

I use the macro for additional processing of an incoming call before connecting. You can do without it.

 [macro-call-in] exten => s,1(agi),AGI(ivr.pl) ;       Perl-. 

/usr/local/share/asterisk/agi-bin/ivr.pl
 #!/usr/bin/perl use Asterisk::AGI; $AGI = new Asterisk::AGI; %input = $AGI->ReadParse(); $timeout = 1300; #    .   . #         . #    $AGI->answer(); #        #     $key1 = $AGI->stream_file(["ivr"], "0123456789"); #   ,     . #    ,  *  # (     ). $key2 = $AGI->wait_for_digit($timeout); $key3 = $AGI->wait_for_digit($timeout); $key4 = $AGI->wait_for_digit($timeout); #     (     ). $num = join ('', chr($key1), chr($key2), chr($key3), chr($key4)); #     ,    . #    ,   . $num = $num + 0; #      . if ( $key1 == 0 ) { $num = 0 } #      ,     ,    . #   . #     Asterisk (  ). $AGI->verbose("IVR number is $num"); #     -     . if (length($num) = 4) { $AGI->exec("Macro","call-local,Local/$num\@office"); #       office.   call-local .  $AGI->hangup(); } #     ,    . #       ,     . elsif (length($num) > 1) { $num = 0; } #      if ($num == 0) { $AGI->exec("Macro", "operator"); } if ($num == 1) { $AGI->exec("Macro", "sale"); } if ($num == 2) { $AGI->exec("Macro", "accounting"); } if ($num == 3) { $AGI->exec("Macro", "marketing"); } if ($num == 4) { $AGI->exec("Macro", "fax"); } 

Further calls are sent to the appropriate macros.

Macro examples:

Call Marketing:
extensions.conf
 [macro-marketing] exten => s,1,Dial(SIP/1008,20,tTm) 

Call extension:
extensions.conf
 [macro-call-local] exten => s,1,Dial(${ARG1},25,tTm) 

That's all.

Maybe the proposed method will seem unnecessarily complicated.
However, I like to keep all the logic of the IVR in a separate file.
Also, the menu can be easily added, add submenus (by creating additional scripts).

In addition, I use AGI in some additional scripts (for handling missed calls, forwarding to mobile, etc.), so it was convenient for me to use it for the voice menu.

Thanks for attention.

Source: https://habr.com/ru/post/172561/


All Articles