#!/usr/bin/perl use Asterisk::AGI; use DBI; my $database = "rt3"; my $data_source = "DBI:mysql:$database;"; my $username = "root"; my $password = ""; my $agi = new Asterisk::AGI; my %input = $agi->ReadParse(); $agi->answer(); my $callbacknumber = $agi->get_data('support/elabgreeting',3000,20); print STDERR "callbacknumber = $callbacknumber\n"; if ($callbacknumber eq "000") { $agi->exec('festival', '"begin recording now"'); $agi->exec('Record', 'support/newmessage:gsm'); sleep 2; $agi->exec('festival', '"you said"'); $agi->exec('playback', 'support/newmessage'); $agi->hangup(); exit; } if ($callbacknumber eq "9000") { $retries = 0; while ($userloggedin != 1) { $logid = $agi->get_data('support/loginid',3000,20); $logpw = $agi->get_data('support/passwd',3000,20); #verify login and pw my $dbh =DBI->connect( $data_source, $username, $password) or die "Can't connect to $data_source: $dbh->errstr\n"; my $sth = $dbh->prepare("select name, comments from Users where id = $logid;"); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; while ($row = $sth->fetchrow_hashref) { $logpwreal = $row->{'comments'}; $name = $row->{'name'}; } $sth->finish; $dbh->disconnect; if ($logpw eq $logpwreal) { $userloggedin = 1; #$agi->exec('festival', '"hello, you are logged in"'); } else { $retries++; if ($retries == 3) { $agi->exec('festival', '"Im sorry, too many login attempts, goodbye"'); $agi->hangup(); } } } ## figure out how many tickets are owned by nobody etc. $duperloop = 0; while ($duperloop == 0) { my $dbh =DBI->connect( $data_source, $username, $password) or die "Can't connect to $data_source: $dbh->errstr\n"; my $sth = $dbh->prepare("select id from Tickets where queue = 3 and status = 'new' and owner = 10;"); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; $agi->stream_file('support/currently'); $agi->say_digits($sth->rows); $agi->stream_file('support/ticketswaiting'); if ($sth->rows == 0) { print STDERR "THERE ARE NO TICKETS LEFT \n\n"; $duperloop = 1; next; } while (@data = $sth->fetchrow_array()) { $agi->stream_file('support/ticketnumber'); $agi->say_digits($data[0]); } $sth->finish; # get which ticket to operate on $retries = 0; $ticketvalid = 0; while ($ticketvalid != 1) { $ticketnum = 0; $ticketnum = $agi->get_data('support/ticketaction',3000,5); my $sth = $dbh->prepare("select id from Tickets where queue = 3 and status = 'new' and owner = 10;"); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; while (@data = $sth->fetchrow_array()) { if ($ticketnum == $data[0]) { print STDERR "correct ticket selected: ticketnum = $ticketnum data= $data[0] \n\n"; $ticketvalid = 1; last; } } #invalid entry try again if ($ticketvalid != 1) { $agi->exec('festival','"invalid ticket number try again"'); $retries++; if ($retries == 3) { $agi->exec('festival','"Im sorry too many errors, goodbye"'); $agi->hangup(); $sth->finish; $dbh->disconnect; exit; } } $sth->finish; } $sth->finish; $superloop = 0; while ($superloop == 0) { $menuchoice = 0; $retries = 0; while ($menuchoice == 0) { $menuchoice = $agi->stream_file('support/ticketmenu', '12345'); print STDERR "menuchoice = $menuchoice retries = $retries \n\n"; $retries++; if ($retries >= 3 && $menuchoice == 0) { $agi->exec('festival','"Im sorry too many errors, goodbye"'); $agi->hangup(); exit; } } if ($menuchoice == 49) { #number 1 was pressed take ownership and resolve $qdb = "update Tickets set Status = 'resolved' where Id = $ticketnum;"; my $sth = $dbh->do($qdb); $qdb = "update Tickets set Owner = $logid where Id = $ticketnum;"; my $sth = $dbh->do($qdb); $superloop = 1; } if ($menuchoice == 50) { #number 2 was pressed play contents of ticket $qdb = "select id from Transactions where Ticket = $ticketnum;"; my $sth = $dbh->prepare($qdb); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; @data = $sth->fetchrow_array; $tid = $data[0]; $qdb = "select Subject, Content from Attachments where Transactionid = $tid and Parent = 0;"; my $sth = $dbh->prepare($qdb); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; $row = $sth->fetchrow_hashref; $subjl = $row->{'Subject'}; $contl = $row->{'Content'}; $qdb = "select Content from Attachments where TransactionId = $tid and ContentType = 'text/plain';"; my $sth = $dbh->prepare($qdb); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; $row = $sth->fetchrow_hashref; $contrl = $row->{'Content'}; $ctrlall = $contl.$contrl; $subjl = "\"$subjl\""; @ctrllines = split /\n/, $ctrlall; print STDERR "CTRL: $ctrlall \n\n"; $agi->exec('playback', 'support/messagesubject'); $agi->exec('festival', $subjl); $agi->exec('playback', 'support/messagecontent'); foreach $line (@ctrllines) { $ctrlall = "\"$line\""; $agi->exec('festival', $ctrlall); } my $sth = $dbh->prepare("select parent from Attachments where TransactionId = $tid and filename = 'customer_message.wav';"); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; while ($row = $sth->fetchrow_hashref) { $parentid = $row->{'parent'} + 2; } $wavfile = "/opt/rt3/share/html/Ticket/Attachment/$tid/$parentid/customer_message.wav"; $wavfilenoext = "/opt/rt3/share/html/Ticket/Attachment/$tid/$parentid/customer_message"; print STDERR "IS WAV = $wavfile \n\n"; -e $wavfile and $agi->exec('playback', 'support/audiocontent'); $agi->get_data($wavfilenoext ,3000,5); $ticketnum = $agi->get_data('support/ticketaction',3000,5); } if ($menuchoice == 51) { #number 3 was pressed take ownership dont resolve $qdb = "update Tickets set Owner = $logid where Id = $ticketnum;"; my $sth = $dbh->do($qdb); $superloop = 1; } if ($menuchoice == 52) { #number 4 was pressed back to previous menu $agi->exec('festival', '"sorry this feature is not implemented yet"'); } if ($menuchoice == 53) { #number 5 was pressed logout and hangup $agi->exec('playback', 'support/goodbye'); agi->hangup(); exit; } } } $agi->hangup(); exit; } $correctentered = 0; while ($correctentered != 1) { if ($callbacknumber ne "") { $agi->exec('playback', 'support/youentered'); $agi->say_digits($callbacknumber); $correct = $agi->get_data('support/ifcorrect',3000,20); if ($correct eq '') { $correctentered = 1; $agi->exec('playback', 'support/nowleaveyourmessage'); } else { $callbacknumber = $correct; } } else { $correctentered = 1; $agi->exec('playback', 'support/nowleaveyourmessage'); } } #record the message $agi->exec('Record', 'support/newticketmessage:wav'); $agi->exec('playback', 'support/thankyou'); #create ticket use lib "/opt/rt3/lib"; use lib "/opt/rt3/etc"; use RT; use RT::Ticket; use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent); use RT qw(LoadConfig); CleanEnv(); RT::LoadConfig(); RT::Init; RT::DropSetGIDPermissions(); my $CurrentUser = GetCurrentUser(); ## unless ($CurrentUser->Id) { print "No RT user found. Please consult your RT administrator.\n"; exit(1); } $message = "Hello, this is a ticket request from the phone system \n"; if ($callbacknumber ne "") { $message = $message . "the return number left is $callbacknumber"; } else { $message = $message . "there was no callback number entered by phone. listen to the message content for the callback details"; } $subject = "Support phone ticket"; my $MIMEObj = MakeMIMEEntity( Subject => $subject, From => 'PBX@vidyah.com', Cc => '', Body => $message, Filename => "issue.txt", AttachmentFieldName => 'RT_Attachment'); $MIMEObj->attach(Type => "audio/x-wav", Encoding => "base64", Path => "/var/lib/asterisk/sounds/support/newticketmessage.wav", Filename => "customer_message.wav", Disposition => "attachment"); my $rt = new RT::Ticket($CurrentUser); my ($ticket,$trans,$msg) = $rt->Create( Queue=>'Elab-Support', Owner=>'Nobody'||'root', Subject=>'Support phone ticket', Status=>'new', Requestor=>['Nobody'], AdminCC=>[''], Priority=>0, FinalPriority=>99, MIMEObj => $MIMEObj ); sub MakeMIMEEntity { #TODO document what else this takes. my %args=( Subject => undef, From => undef, Cc => undef, Body => undef, AttachmentFieldName => undef, @_); #Make the update content have no 'weird' newlines in it $args{'Body'} =~ s/\r\n/\n/gs; my $Message = MIME::Entity->build ( Subject => $args{'Subject'} || "", From => $args{'From'}, Cc => $args{'Cc'}, Data => [ $args{'Body'} ] ); ## Support for attachments not necessary $Message->make_singlepart(); return ($Message); } $agi->say_digits($ticket); $agi->say_digits($trans); #find attachment directory my $dbh =DBI->connect( $data_source, $username, $password) or die "Can't connect to $data_source: $dbh->errstr\n"; my $sth = $dbh->prepare("select parent from Attachments where TransactionId = $trans and filename = 'customer_message.wav';"); $sth->execute or die "Unable execute query:$dbh->err, $dbh->errstr\n"; while ($row = $sth->fetchrow_hashref) { $parentid = $row->{'parent'} + 2; } $sth->finish; $dbh->disconnect; use File::Path; use File::Copy; $attachpath = "/opt/rt3/share/html/Ticket/Attachment/$trans/$parentid"; mkpath($attachpath); #copy("/var/lib/asterisk/sounds/support/newticketmessage.wav", "$attachpath/customer_message.wav"); move("/var/lib/asterisk/sounds/support/newticketmessage.wav", "$attachpath/customer_message.wav"); $agi->hangup(); exit;