Perl-recmsg-AGI



#!/usr/bin/perl
# This will play your customized recorded message instead of voicemail default message when you dont answer calls
# it takes two parameters in the context, 1=param channel, 2=param option +[0|1|2]
#(((((((((((((example code in the extension.conf)))))))))))))))))
# exten => *190,1,DeadAgi(recmsg.pl|${CHANNEL}|0); for listen or play your recorded message
# exten => *191,1,DeadAgi(recmsg.pl|${CHANNEL}|1); for record your message
# exten => *192,1,DeadAgi(recmsg.pl|${CHANNEL}|2); for enable/disable 

Note: you can use line *190 as: 
exten => XXX,102,DeadAgi(recmsg.pl|${CHANNEL}|0); 
for playing your recorded message instead of playing default voicemail message

use Asterisk::AGI;
use lib "/var/lib/asterisk/agi-bin";
use Db;
$AGI = new Asterisk::AGI;
$|=1;
my %input 	=$AGI->ReadParse();
my $CLI  		=$ARGV[0];
my $OPTION	=$ARGV[1];
my $msgflag	=0;
if($CLI eq "" || $OPTION eq "")
{&myVerbose("User not recognized/invalid option : CLI:$CLI,OPTION:$OPTION");  
$AGI->hangup(); exit;
}
my $username	=undef;
my $ZapLine	=undef;
my $WebPassword=undef;
$res = $AGI->exec("NoCDR");
$AGI->answer();
if( $CLI =~ /^Zap/){	
	$CLI =~ /Zap\/(.*?)-/gsi;
	$ZapLine=$1;
	my @rData	=Db->ExecuteQuery("select msgflag,username,WebPassword 
                                                              from siprelation where zapchannel = '$ZapLine'");
	if ($rData[1][0] eq ""){&myVerbose("recmsg.pl User not recognized on channel bank CLI : $CLI");	
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/extension');
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/is-curntly-busy');
		$AGI->hangup();exit;
	}
	$msgflag = $rData[1][0];
	$username= $rData[1][1];
	$WebPassword=$rData[1][2];
}else{$CLI =~ /Sip\/(.*?)-/gsi;
	my @rData	=Db->ExecuteQuery("select msgflag,username,WebPassword from siprelation where username = '$1'");
	if ($rData[1][0] eq ""){&myVerbose("recmsg.pl User not recognized on channel bank CLI : $CLI");	
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/extension');
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/is-curntly-busy');
		$AGI->hangup();exit;
	}
	$msgflag = $rData[1][0];
	$username= $rData[1][1];
	$WebPassword=$rData[1][2];
}
my $g_OUTPUT ="\n[ START ][$CLI]\n";
my $DEBUG_OUTPUT_FLAG 	=0;
if($username eq ""){
&myVerbose("Extension not found: username:$username");  $AGI->hangup(); exit;
}
##################################################################2
if($OPTION==2){
my $DialedNo =$AGI->get_data('/var/lib/asterisk/recmsg/default/pls-enter-vm-password',10000,length($WebPassword));
	if($DialedNo==$WebPassword){
		if($msgflag==0){
			Db->ExecuteQuery("update siprelation set msgflag='1' where username='$username'");
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/activated');
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
			&myVerbose("OPTION:2 flag=1");
		}elsif($msgflag==1){
			Db->ExecuteQuery("update siprelation set msgflag='0' where username='$username'");
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/de-activated');
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
			&myVerbose("OPTION:2 flag=0");
		}else{
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/invalid');
			$AGI->hangup();
			&myVerbose("OPTION:2 invalid flag value: flag:$msgflag");
		}
	}else{
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/invalid');
		$AGI->hangup();
		&myVerbose("OPTION:2 password didnt match");
	}
}
##################################################################1
if($OPTION==1){
my $DialedNo =$AGI->get_data('/var/lib/asterisk/recmsg/default/pls-enter-vm-password',10000,length($WebPassword));
	if($DialedNo==$WebPassword){
		my $path="/var/lib/asterisk/recmsg/recmsg_".$username.".gsm|5|60|nt";
		$res = $AGI->exec("Record $path");
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
		&myVerbose("OPTION:1 file has recorded");
	}else{
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/invalid');
		$AGI->hangup();
		&myVerbose("OPTION:1 password didnt match");
	}	
}
##################################################################0
if($OPTION==0){
if($msgflag==1){
$AGI->stream_file('/var/lib/asterisk/recmsg/recmsg_'.$username);
sleep(1);
$AGI->stream_file('/var/lib/asterisk/recmsg/recmsg_'.$username);
$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
&myVerbose("OPTION:0 reading custom msg");
}else{
$AGI->stream_file('/var/lib/asterisk/recmsg/default/extension'); 
$AGI->stream_file('/var/lib/asterisk/recmsg/default/is-curntly-busy');
$AGI->hangup();
&myVerbose("OPTION:0 reading default vm_msg");
}
}
##########################################################
if($DEBUG_OUTPUT_FLAG){
	$g_OUTPUT	.="[ END ]\n\n";&savelog();
}
$AGI->hangup();exit;
sub myVerbose{
   my ($Msg) = @_;
   $g_OUTPUT .="$Msg\n";
   return;
}
sub savelog{
	open(FILE, ">>/var/lib/asterisk/agi-bin/dialoutlog.txt") 
	or die("Couldn't open dialoutlog.txt\n");
	print FILE "(". localtime() .")"."recmsg.agi".$g_OUTPUT;
	close(FILE);
}
# file [dialoutlog.txt] is used for debugging purpose
# GodLuck
# by imran,  file name [recmsg.pl], 16 May, 2006, imran_account@yahoo.com
# favorite quote "Dont do things until they're needed, Lazy Evaluation 1989"



#!/usr/bin/perl
# This will play your customized recorded message instead of voicemail default message when you dont answer calls
# it takes two parameters in the context, 1=param channel, 2=param option +[0|1|2]
#(((((((((((((example code in the extension.conf)))))))))))))))))
# exten => *190,1,DeadAgi(recmsg.pl|${CHANNEL}|0); for listen or play your recorded message
# exten => *191,1,DeadAgi(recmsg.pl|${CHANNEL}|1); for record your message
# exten => *192,1,DeadAgi(recmsg.pl|${CHANNEL}|2); for enable/disable 

Note: you can use line *190 as: 
exten => XXX,102,DeadAgi(recmsg.pl|${CHANNEL}|0); 
for playing your recorded message instead of playing default voicemail message

use Asterisk::AGI;
use lib "/var/lib/asterisk/agi-bin";
use Db;
$AGI = new Asterisk::AGI;
$|=1;
my %input 	=$AGI->ReadParse();
my $CLI  		=$ARGV[0];
my $OPTION	=$ARGV[1];
my $msgflag	=0;
if($CLI eq "" || $OPTION eq "")
{&myVerbose("User not recognized/invalid option : CLI:$CLI,OPTION:$OPTION");  
$AGI->hangup(); exit;
}
my $username	=undef;
my $ZapLine	=undef;
my $WebPassword=undef;
$res = $AGI->exec("NoCDR");
$AGI->answer();
if( $CLI =~ /^Zap/){	
	$CLI =~ /Zap\/(.*?)-/gsi;
	$ZapLine=$1;
	my @rData	=Db->ExecuteQuery("select msgflag,username,WebPassword 
                                                              from siprelation where zapchannel = '$ZapLine'");
	if ($rData[1][0] eq ""){&myVerbose("recmsg.pl User not recognized on channel bank CLI : $CLI");	
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/extension');
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/is-curntly-busy');
		$AGI->hangup();exit;
	}
	$msgflag = $rData[1][0];
	$username= $rData[1][1];
	$WebPassword=$rData[1][2];
}else{$CLI =~ /Sip\/(.*?)-/gsi;
	my @rData	=Db->ExecuteQuery("select msgflag,username,WebPassword from siprelation where username = '$1'");
	if ($rData[1][0] eq ""){&myVerbose("recmsg.pl User not recognized on channel bank CLI : $CLI");	
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/extension');
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/is-curntly-busy');
		$AGI->hangup();exit;
	}
	$msgflag = $rData[1][0];
	$username= $rData[1][1];
	$WebPassword=$rData[1][2];
}
my $g_OUTPUT ="\n[ START ][$CLI]\n";
my $DEBUG_OUTPUT_FLAG 	=0;
if($username eq ""){
&myVerbose("Extension not found: username:$username");  $AGI->hangup(); exit;
}
##################################################################2
if($OPTION==2){
my $DialedNo =$AGI->get_data('/var/lib/asterisk/recmsg/default/pls-enter-vm-password',10000,length($WebPassword));
	if($DialedNo==$WebPassword){
		if($msgflag==0){
			Db->ExecuteQuery("update siprelation set msgflag='1' where username='$username'");
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/activated');
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
			&myVerbose("OPTION:2 flag=1");
		}elsif($msgflag==1){
			Db->ExecuteQuery("update siprelation set msgflag='0' where username='$username'");
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/de-activated');
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
			&myVerbose("OPTION:2 flag=0");
		}else{
			$AGI->stream_file('/var/lib/asterisk/recmsg/default/invalid');
			$AGI->hangup();
			&myVerbose("OPTION:2 invalid flag value: flag:$msgflag");
		}
	}else{
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/invalid');
		$AGI->hangup();
		&myVerbose("OPTION:2 password didnt match");
	}
}
##################################################################1
if($OPTION==1){
my $DialedNo =$AGI->get_data('/var/lib/asterisk/recmsg/default/pls-enter-vm-password',10000,length($WebPassword));
	if($DialedNo==$WebPassword){
		my $path="/var/lib/asterisk/recmsg/recmsg_".$username.".gsm|5|60|nt";
		$res = $AGI->exec("Record $path");
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
		&myVerbose("OPTION:1 file has recorded");
	}else{
		$AGI->stream_file('/var/lib/asterisk/recmsg/default/invalid');
		$AGI->hangup();
		&myVerbose("OPTION:1 password didnt match");
	}	
}
##################################################################0
if($OPTION==0){
if($msgflag==1){
$AGI->stream_file('/var/lib/asterisk/recmsg/recmsg_'.$username);
sleep(1);
$AGI->stream_file('/var/lib/asterisk/recmsg/recmsg_'.$username);
$AGI->stream_file('/var/lib/asterisk/recmsg/default/thanks-for-using');
&myVerbose("OPTION:0 reading custom msg");
}else{
$AGI->stream_file('/var/lib/asterisk/recmsg/default/extension'); 
$AGI->stream_file('/var/lib/asterisk/recmsg/default/is-curntly-busy');
$AGI->hangup();
&myVerbose("OPTION:0 reading default vm_msg");
}
}
##########################################################
if($DEBUG_OUTPUT_FLAG){
	$g_OUTPUT	.="[ END ]\n\n";&savelog();
}
$AGI->hangup();exit;
sub myVerbose{
   my ($Msg) = @_;
   $g_OUTPUT .="$Msg\n";
   return;
}
sub savelog{
	open(FILE, ">>/var/lib/asterisk/agi-bin/dialoutlog.txt") 
	or die("Couldn't open dialoutlog.txt\n");
	print FILE "(". localtime() .")"."recmsg.agi".$g_OUTPUT;
	close(FILE);
}
# file [dialoutlog.txt] is used for debugging purpose
# GodLuck
# by imran,  file name [recmsg.pl], 16 May, 2006, imran_account@yahoo.com
# favorite quote "Dont do things until they're needed, Lazy Evaluation 1989"

Created by: ddx, Last modification: Fri 05 of Nov, 2010 (21:56 UTC) by admin
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+