#!/usr/bin/perl # # VD_amd.agi version 0.3 # # NOTE: This script is only needed with app_amd enabled systems # # runs after a call goes through AMD in extensions.conf to send the call on to # it's proper destination # # You need to put lines similar to those below in your extensions.conf file: # # ; VICIDIAL_auto_dialer transfer script AMD with Load Balanced: # exten => 8369,1,AGI(call_log.agi,${EXTEN}) # exten => 8369,2,(AMD),AMD # exten => 8369,3,AGI(VD_amd.agi,${EXTEN}) # exten => 8369,4,AGI(agi-VDAD_LB_transfer.agi,${EXTEN}) # exten => 8369,5,AGI(agi-VDAD_LB_transfer.agi,${EXTEN}) # exten => 8369,6,AGI(agi-VDAD_LB_transfer.agi,${EXTEN}) # exten => 8369,7,Hangup # # # Copyright (C) 2006 Matt Florell LICENSE: GPLv2 # # changes: # 60206-1434 first build # 60207-1205 added database records entries/modifications # 60207-1507 added ability to send AMs to campaign-defined message to leave $V = 0; # set to 1 for verbose mode $M = 0; # set to 1 for 2 line messages mode $A = 1; # set to 1 for AMD output messages mode $AMD_LOG = 1; # set to 1 for logfile ### Make sure this file is in a path or put the absolute path to it require("/home/cron/AST_SERVER_conf.pl"); # local configuration file use Net::MySQL; use Asterisk::AGI; $AGI = new Asterisk::AGI; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year = ($year + 1900); $mon++; if ($mon < 10) {$mon = "0$mon";} if ($mday < 10) {$mday = "0$mday";} if ($hour < 10) {$Fhour = "0$hour";} if ($min < 10) {$min = "0$min";} if ($sec < 10) {$sec = "0$sec";} $now_date_epoch = time(); $now_date = "$year-$mon-$mday $hour:$min:$sec"; $start_time=$now_date; $CIDdate = "$mon$mday$hour$min$sec"; $tsSQLdate = "$year$mon$mday$hour$min$sec"; $SQLdate = "$year-$mon-$mday $hour:$min:$sec"; $SQLdateBEGIN = $SQLdate; ### begin parsing run-time options ### if (length($ARGV[0])>1) { if ($V) {print STDERR "Perl Environment Dump:\n";} $i=0; while ($#ARGV >= $i) { $args = "$args $ARGV[$i]"; if ($V) {print STDERR "$i|$ARGV[$i]|\n";} $i++; } if ($args =~ /--help/i) { print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n"; } else { if ($args =~ /-V/i) { $V=1; } if ($args =~ /-debug/i) { $DG=1; } if ($args =~ /-dbAVS/i) { $DGA=1; } if ($args =~ /-q/i) { $q=1; $Q=1; } if ($args =~ /-t/i) { $TEST=1; $T=1; } } } else { if ($V) {print "no command line options set\n";} } $|=1; while() { chomp; last unless length($_); if ($V) { if (/^agi_(\w+)\:\s+(.*)$/) { $AGI{$1} = $2; } } if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;} if (/^agi_priority\:\s+(.*)$/) {$priority = $1;} if (/^agi_channel\:\s+(.*)$/) {$channel = $1;} if (/^agi_extension\:\s+(.*)$/) {$extension = $1;} if (/^agi_type\:\s+(.*)$/) {$type = $1;} if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1; $calleridnum = $callerid;} if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;} } if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) ) { $callerid =~ s/^\"//gi; $callerid =~ s/\".*$//gi; # ### set the callerid to the ACQS value(calleridname) # print "SET CALLERID $callerid\n"; # checkresult($result); # print STDERR "callerID changed: $callerid\n"; } if ( ( (length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) ) ) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) ) { $callerid = $calleridname; # ### set the callerid to the ACQS value(calleridname) # print "SET CALLERID $callerid\n"; # checkresult($result); # print STDERR "callerID changed: $callerid\n"; } if ($V) { if ($V) {print STDERR "AGI Environment Dump:\n";} foreach $i (sort keys %AGI) { if ($V) {print STDERR " -- $i = $AGI{$i}\n";} } } $VDADcampaign=''; $VDADphone=''; $VDADphone_code=''; if ($V) {print STDERR "AGI Environment Dump: |$unique_id|$channel|$extension|$type|$callerid|$calleridname|$priority|\n";} $callerid =~ s/\"//gi; $CIDlead_id = $callerid; $CIDlead_id = substr($CIDlead_id, 11, 9); $CIDlead_id = ($CIDlead_id + 0); $VD_lead_id = $CIDlead_id; if ($V) {print STDERR "\nVD_amd : $callerid $channel $priority\n";} if ($M) {print STDERR "+++++ VD amd START : |$unique_id|$channel|$extension|$type|$callerid|$CIDlead_id|$now_date|$AST_ver|$priority|$calleridname|\n";} if ( ($channel =~ /Local/i) && ($AST_ver !~ /^1\.0\.8|^1\.0\.9/) ) { if ($M) {print STDERR "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority\n";} if ($priority > 2) {sleep(1);} exit; } $AMDSTATUS = $AGI->get_variable(AMDSTATUS); $AMDCAUSE = $AGI->get_variable(AMDCAUSE); if ($A) {print STDERR "AAAAA AMD results: |$AMDSTATUS|$AMDCAUSE|\n";} # AAAAA AMD results: |AMD_PERSON|AMD_HUMAN-300-300| # AAAAA AMD results: |AMD_MACHINE|AMD_LONGGREETING-1500-1500| if ($AMD_LOG) { open(Lout, ">>/home/cron/AMD_log.txt") || die "Can't open /home/cron/AMD_log.txt: $!\n"; print Lout "$now_date|$VD_lead_id| |$AMDSTATUS|$AMDCAUSE|$calleridnum|\n"; close(Lout); } ################################################################################## ########## AMD says it's a person so exit and go on to transfer scripts ########## if ($AMDSTATUS =~ /PERSON|NOTSURE|HANGUP/) { if ($A) {print STDERR " AMD exiting\n";} exit; } else { ################################################################################ ########## AMD says it's a machine so modify statuses and hangup call ########## ########## something will go here later for leaving a message on AMs ########## if (!$DB_port) {$DB_port='3306';} my $dbhA = Net::MySQL->new(hostname=>"$DB_server", database=>"$DB_database", user=>"$DB_user", password=>"$DB_pass", port => "$DB_port") or die "Couldn't connect to database: $DB_server - $DB_database\n"; ########## FIND ext_context from the servers table ########## $stmtA = "SELECT ext_context FROM servers where server_ip = '$server_ip';"; $dbhA->query("$stmtA"); if ($dbhA->has_selected_record) { $iter=$dbhA->create_record_iterator; while ( $record = $iter->each) { $DBext_context = "$record->[0]"; if ($DBext_context) {$ext_context = $DBext_context;} } } ########## FIND vicidial_auto_calls record ########## $stmtA = "SELECT campaign_id,phone_code,phone_number FROM vicidial_auto_calls where callerid='$callerid' order by auto_call_id desc limit 1;"; if($M){print STDERR "\n\n|$stmtA|\n";} $dbhA->query("$stmtA"); if ($dbhA->has_selected_record) { $iter=$dbhA->create_record_iterator; while ( $record = $iter->each) { $VD_campaign_id = "$record->[0]"; $VD_phone_code = "$record->[1]"; $VD_phone_number = "$record->[2]"; } } ########## UPDATE vicidial_list ########## $stmtA = "UPDATE vicidial_list set status='A' where lead_id = '$VD_lead_id';"; if($M){print STDERR "\n\n|$stmtA|\n";} $dbhA->query("$stmtA"); my $affected_rows = $dbhA->get_affected_rows_length; if ($M) {print STDERR "-- VDAD vicidial_list update: |$affected_rows|$VD_lead_id\n";} ########## DELETE vicidial_auto_calls entry ########## $stmtA = "DELETE FROM vicidial_auto_calls where callerid='$callerid' order by auto_call_id desc limit 1;"; $dbhA->query("$stmtA"); my $affected_rows = $dbhA->get_affected_rows_length; if ($M) {print STDERR "-- VDAC record deleted: |$affected_rows| |$VD_lead_id|$uniqueid|$VD_callerid|$server_ip\n";} ########## FIND AND UPDATE vicidial_log ########## $stmtA = "SELECT start_epoch FROM vicidial_log where uniqueid='$uniqueid' and lead_id='$VD_lead_id' order by call_date desc limit 1;"; if($M){print STDERR "\n\n|$stmtA|\n";} $dbhA->query("$stmtA"); $epc_countCUSTDATA=0; if ($dbhA->has_selected_record) { $iter=$dbhA->create_record_iterator; while ( $record = $iter->each) { $VD_start_epoch = "$record->[0]"; $epc_countCUSTDATA++; } } if (!$epc_countCUSTDATA) { if($M){print STDERR "\nVD amd: no VDL record found: $uniqueid $calleridname $VD_lead_id $uniqueid\n";} $end_epoch = ($now_date_epoch + 3); $stmtA = "INSERT INTO vicidial_log (uniqueid,lead_id,campaign_id,call_date,start_epoch,status,phone_code,phone_number,user,processed,length_in_sec,end_epoch) values('$uniqueid','$VD_lead_id','$VD_campaign_id','$now_date','$now_date_epoch','A','$VD_phone_code','$VD_phone_number','VDAD','N','3','$end_epoch')"; if($M){print STDERR "\n|$stmtA|\n";} $dbhA->query($stmtA); } else { $VD_seconds = ($now_date_epoch - $VD_start_epoch); $stmtA = "UPDATE vicidial_log set status='A',end_epoch='$now_date_epoch',length_in_sec='$VD_seconds' where uniqueid='$uniqueid' and lead_id='$VD_lead_id' order by call_date desc limit 1;"; if($M){print STDERR "\n\n|$stmtA|\n";} $dbhA->query("$stmtA"); my $affected_rows = $dbhA->get_affected_rows_length; if ($M) {print STDERR "-- VDAD vicidial_log update: |$affected_rows|$uniqueid|\n";} } ### Grab vmail forward message values from the database $stmtA = "SELECT am_message_exten,amd_send_to_vmx FROM vicidial_campaigns where campaign_id = '$VD_campaign_id';"; if($M){print STDERR "\n\n|$stmtA|\n";} $dbhA->query("$stmtA"); if ($dbhA->has_selected_record) { $iter=$dbhA->create_record_iterator; while ( $record = $iter->each) { $DB_am_message_exten = "$record->[0]"; $DB_amd_send_to_vmx = "$record->[1]"; } } if ($M) {print STDERR "-- AMD campaign values: |$VD_campaign_id|$DB_am_message_exten|$DB_amd_send_to_vmx|\n";} if ( ($DB_amd_send_to_vmx =~ /Y/) && (length($DB_am_message_exten) > 0) ) { $stmtA = "UPDATE vicidial_log set status='AM' where uniqueid='$uniqueid' and lead_id='$VD_lead_id' order by call_date desc limit 1;"; if($M){print STDERR "\n\n|$stmtA|\n";} $dbhA->query("$stmtA"); my $affected_rows = $dbhA->get_affected_rows_length; if ($M) {print STDERR "-- VDAD vicidial_log AM update: |$affected_rows|$uniqueid|\n";} $stmtA = "UPDATE vicidial_list set status='AM' where lead_id='$VD_lead_id';"; if($M){print STDERR "\n\n|$stmtA|\n";} $dbhA->query("$stmtA"); my $affected_rows = $dbhA->get_affected_rows_length; if ($M) {print STDERR "-- VDAD vicidial_list AM update: |$affected_rows|$uniqueid|\n";} print STDERR "\nexiting the AMD app, transferring call to $DB_am_message_exten"; print "SET CONTEXT $ext_context\n"; checkresult($result); print "SET EXTENSION $DB_am_message_exten\n"; checkresult($result); print "SET PRIORITY 1\n"; checkresult($result); exit; } $dbhA->close; $AGI->hangup($channel); exit; } exit; sub checkresult { my ($res) = @_; my $retval; $tests++; chomp $res; if ($res =~ /^200/) { $res =~ /result=(-?\d+)/; if (!length($1)) { # print STDERR "FAIL ($res)\n"; $fail++; } else { # print STDERR "PASS ($1)\n"; $pass++; } } else { # print STDERR "FAIL (unexpected result '$res')\n"; $fail++; } }