From Yasiru, 5 Years ago, written in Perl.
- view diff
Embed
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. require ESL;
  5. use IO::Socket::INET;
  6.  
  7. my $ip = "127.0.0.1";
  8. my $sock = new IO::Socket::INET ( LocalHost => $ip,
  9.                                   LocalPort => '8040',
  10.                                   Proto => 'tcp',
  11.                                   Listen => 1,
  12.                                   Reuse => 1 );
  13. die "Could not create socket: $!\n" unless $sock;
  14.  
  15. my $host = "127.0.0.1";
  16. my $port = "8021";
  17. my $pass = "ClueCon";
  18. my $con2  = new ESL::ESLconnection($host, $port, $pass);
  19.  
  20. for(;;) {
  21.     my $new_sock = $sock->accept();
  22.     my $pid = fork();
  23.     if ($pid) {
  24.         print "New child pid $pid created...\n";
  25.         close($new_sock);
  26.         next;
  27.     }
  28.     my $fd = fileno($new_sock);
  29.     my $con = new ESL::ESLconnection($fd);
  30.     my $info = $con->getInfo();
  31.     my $uuid = $info->getHeader("unique-id");
  32.     printf "Connected call %s, from %s\n", $uuid, $info->getHeader("caller-caller-id-number");
  33.  
  34.     $con->execute("set", "hangup_after_bridge=false");
  35.     $con->execute("set", "continue_on_fail=true");
  36.     $con->execute("set", "ignore_early_media=true");
  37.     $con->execute("answer");
  38.     $con->execute("playback","ivr/ivr-welcome_to_freeswitch.wav");
  39.     $con->execute("sleep","500");
  40.  
  41.     my $con2  = new ESL::ESLconnection($host, $port, $pass);
  42.  
  43.     my $out_uuid = $con2->api('create_uuid')->getBody();
  44.     my $out_job = $con2->bgapi("originate", "{ignore_early_media=true,hangup_after_bridge=false,continue_on_fail=false,originate_timeout=30,origination_uuid=$out_uuid}sofia/gateway/eliza-outbound/0410001000 9664");
  45.     my $job_uuid = $out_job->getHeader("Job-UUID");
  46.     print "Call to coach. Job-UUID : $job_uuid, call uuid : $uuid, out_uuid : $out_uuid \n";
  47.  
  48.     my $stay_connected = 1;
  49.     $con2->events("plain","all");
  50.     while($stay_connected) {
  51.         print "loop...\n";
  52.         my $e = $con2->recvEvent();
  53.         if($e) {
  54.             my $ev_uuid = $e->getHeader("Unique-ID");
  55.             if ( $ev_uuid && ($ev_uuid eq $out_uuid) ) {
  56.                 if ( $e->getHeader("Answer-State") eq "answered" ) {
  57.                     print "Out call $out_uuid got answered!!!\n\n";
  58.                     goto BREAKLOOP;
  59.                 } else {
  60.                     print "Answer State : ".$e->getHeader("Answer-State")."\n";
  61.                 }
  62.             } else {
  63.                 #nothing...
  64.             }
  65.         } else {
  66.             print "No event...\n";
  67.             #$con->execute("set", "playback_timeout_sec=20");
  68.             #$con->execute("playback",'$${hold_music}');
  69.         }
  70.     }
  71.     BREAKLOOP:
  72.     #$con2->execute('uuid_break',"$out_uuid");
  73.     $con2->api("uuid_bridge $uuid $out_uuid");
  74.     $con->execute("hangup");
  75.     $con->disconnect();
  76.     $con2->disconnect();
  77.     close($new_sock);
  78. }
  79. print "BYE\n";