server3.pl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. require ESL;
  2. use IO::Socket::INET;
  3. #ESL::eslSetLogLevel(7);
  4. my $ip = "127.0.0.1";
  5. my $sock = new IO::Socket::INET ( LocalHost => $ip, LocalPort => '8040', Proto => 'tcp', Listen => 1, Reuse => 1 );
  6. die "Could not create socket: $!\n" unless $sock;
  7. for(;;) {
  8. my $new_sock = $sock->accept();
  9. my $pid = fork();
  10. if ($pid) {
  11. close($new_sock);
  12. next;
  13. }
  14. my $host = $new_sock->sockhost();
  15. my $fd = fileno($new_sock);
  16. my $con = new ESL::ESLconnection($fd);
  17. my $info = $con->getInfo();
  18. #print $info->serialize();
  19. my $uuid = $info->getHeader("unique-id");
  20. printf "Connected call %s, from %s\n", $uuid, $info->getHeader("caller-caller-id-number");
  21. $con->sendRecv("myevents");
  22. $con->execute("answer");
  23. $con->sendRecv("divert_events on");
  24. $con->execute("detect_speech", "pocketsphinx yes_no yes_no");
  25. $con->execute("playback", "/ram/swimp.raw");
  26. while($con->connected()) {
  27. #my $e = $con->recvEventTimed(100);
  28. my $e = $con->recvEvent();
  29. if ($e) {
  30. my $name = $e->getHeader("event-name");
  31. print "EVENT [$name]\n";
  32. if ($name eq "DETECTED_SPEECH") {
  33. print $e->serialize();
  34. my $type = $e->getHeader("speech-type");
  35. if ($type = "detected-speech") {
  36. $con->execute("detect_speech", "resume");
  37. }
  38. }
  39. if ($name eq "DTMF") {
  40. my $digit = $e->getHeader("dtmf-digit");
  41. my $duration = $e->getHeader("dtmf-duration");
  42. print "DTMF digit $digit ($duration)\n";
  43. }
  44. }
  45. }
  46. print "BYE\n";
  47. close($new_sock);
  48. }