このページは役立ちましたか?

サンプル:お客様のDTMF(プッシュホン)入力を取得するAGIスクリプト

  • You do not have permissions to view this page - please try logging in.
目次
ヘッダーがありません

 お客様のプッシュフォン(DTMF)入力によって数字を取得し、それに応じて処理を行う一例です。

この例では、あらかじめ設定リスト(テキスト)を準備しておき、該当する数字列があるかどうかを判断するようにしています。

#!/usr/bin/perl
#
# 2012 Communication Business Avenue, Inc.
# User Input Check Script
#
use strict;
use Asterisk::AGI;
$|=1;

#
# Settings
#
my $DIGITS = 4; #USER Input DIGITS
my $userid_list = "/var/lib/asterisk/agi-bin/id_list.txt";
my $log = "/var/log/asterisk/checkid.log";

#
# Setup some variables
#
my %AGI; my $tests = 0; my $fail = 0; my $pass = 0;
use vars qw($astagi $astman);
$astagi = new Asterisk::AGI;
#$astman = new Asterisk::Manager;

while(<stdin>) {
	chomp;
	last unless length($_);
	if (/^agi_(\w+)\:\s+(.*)$/) {
		$AGI{$1} = $2;
	}
}

#Variables Check
#while( (my $name, my $value) = each %AGI ){
#    $astagi->verbose("$name, $AGI{$name}");
#}

#
# Announcement
#
$astagi->verbose("Start Announcemnet.");

#
# get DTMF
#
my $DTMF=0;
my $myres=0;
my $myres = $astagi->exec("BACKGROUND", "announce_1");

#First One Push
if($myres == 48){ $DTMF=0; }
if($myres == 49){ $DTMF=1; }
if($myres == 50){ $DTMF=2; }
if($myres == 51){ $DTMF=3; }
if($myres == 52){ $DTMF=4; }
if($myres == 53){ $DTMF=5; }
if($myres == 54){ $DTMF=6; }
if($myres == 55){ $DTMF=7; }
if($myres == 56){ $DTMF=8; }
if($myres == 57){ $DTMF=9; }

if(length($DTMF)<4){
 $DTMF.=get_dtmf($DIGITS-length($DTMF));
}

$astagi->verbose("DTMF=".$DTMF);

#no input
if(length($DTMF)<$DIGITS || checklist($DTMF)==0)
{
    #$astagi->exec("PLAYBACK","type_again");
    $astagi->exec("PLAYBACK","announce_2");

    #$astagi->exec("PLAYBACK","announce_3");
    $DTMF=0;
    $myres=0;
    $myres = $astagi->exec("BACKGROUND","announce_3");
    #First One Push
    if($myres == 48){ $DTMF=0; }
    if($myres == 49){ $DTMF=1; }
    if($myres == 50){ $DTMF=2; }
    if($myres == 51){ $DTMF=3; }
    if($myres == 52){ $DTMF=4; }
    if($myres == 53){ $DTMF=5; }
    if($myres == 54){ $DTMF=6; }
    if($myres == 55){ $DTMF=7; }
    if($myres == 56){ $DTMF=8; }
    if($myres == 57){ $DTMF=9; }
    if(length($DTMF)<4){
     $DTMF.=get_dtmf($DIGITS-length($DTMF));
    }

    if(length($DTMF)<$DIGITS|| checklist($DTMF)==0){
        #$astagi->exec("PLAYBACK","couldnotfind");

        #Hangup Version
        #$astagi->hangup();
        #exit 0;

        #Goto Next IVR Steps Version
        $DTMF=""; 
    }
}

#
# Finished! GOAL
#
#$astagi->hangup();
#ここで、ユーザの入力したデータを MY_USERIDに格納しています。
$astagi->set_variable("MY_USERID", $DTMF);

#
# Sub
#
sub checklist {
#Check the userid text file list
    my ($id) = @_;
    open MYFILE, "$userid_list";
    while(my $line = <myfile>){
        my $tmp=trim($line);
        if($tmp==$id){
            #Found!
            close MYFILE;
            return 1;
        }
    }   
    #didn't find!
    close MYFILE;
    return 0;
}

sub checkresult {
        my ($res) = @_;
        my $retval;
        $tests++;
        chomp $res;
        if ($res =~ /^200/) {
                $res =~ /result=(-?\d+)/;
                if (!length($1)) {
                        $astagi->verbose("FAIL ($res)\n");
                        $fail++;
                } else {
                        $astagi->verbose("PASS ($1)\n");
                        $pass++;
                }
        } else {
                print STDERR "FAIL (unexpected result '$res')\n";
                $fail++;
        }
}

sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

sub get_dtmf {
    my $getdigits = shift;
    my $DTMF = "";
    my $digit_loop = 0;
    my $digits_being_entered = 1;
    my $POUND =  ord('#'); 

    $astagi->verbose("DTMF Input start....");
    while (($digits_being_entered) && ($digit_loop < $getdigits))
    {
        my $digit = chr($astagi->wait_for_digit('10000'));
        if($digit =~ /\d/){
            $DTMF = "$DTMF$digit";

            #this makes input so slow
            #$astagi->say_alpha($digit);
            undef $digit;
         } else {
            $digits_being_entered = 0;
         }

         $digit_loop++;
    }
    $astagi->verbose("end of DTMF Input ...");
    #mylog("DTMF:".$DTMF);
    #$astagi->say_alpha($DTMF);
    return $DTMF;
}

sub mylog
{
    my ($mes) = @_;
    $mes =~ s/\n/ /gm;
    $mes =~ s/\r/ /gm;
    open(my $out_fh, ">>", $log) or die "Cannot open $log for write: $!";
    print $out_fh scalar(localtime) . " $mes\n";
    close $out_fh;
    return $mes;
}
</myfile></stdin>
このページは役立ちましたか?
タグ (タグ編集)
  • No tags

ファイル 0

 
あなたはコメントを投稿するには ログイン しなければなりません。
Powered by MindTouch Core