server3.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/ruby
  2. # Simple Ruby ESL example that forks a new process for every connection.
  3. # Called like this: <action application="socket" data="localhost:8086 async full"/>
  4. #
  5. # Try pressing 5, 8 or 9 when the file is played and see what happens
  6. #
  7. # Contributed by Mikael Bjerkeland
  8. require "ESL"
  9. require 'socket'
  10. include Socket::Constants
  11. bind_address = "127.0.0.1"
  12. bind_port = 8086
  13. def time_now
  14. Time.now.strftime("%Y-%m-%d %H:%M:%S")
  15. end
  16. socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  17. sockaddr = Socket.sockaddr_in(bind_port, bind_address)
  18. socket.bind(sockaddr)
  19. socket.listen(5)
  20. puts "Listening for connections on #{bind_address}:#{bind_port}"
  21. loop do
  22. client_socket, client_sockaddr = socket.accept #_nonblock
  23. pid = fork do
  24. @con = ESL::ESLconnection.new(client_socket.fileno)
  25. info = @con.getInfo
  26. uuid = info.getHeader("UNIQUE-ID")
  27. @con.sendRecv("myevents")
  28. @con.sendRecv("divert_events on")
  29. puts "#{time_now} [#{uuid}] Call to [#{info.getHeader("Caller-Destination-Number")}]"
  30. @con.execute("log", "1, Wee-wa-wee-wa")
  31. @con.execute("info", "")
  32. @con.execute("answer", "")
  33. @con.execute("playback", "/usr/local/freeswitch/sounds/music/8000/suite-espanola-op-47-leyenda.wav")
  34. while @con.connected
  35. e = @con.recvEvent
  36. if e
  37. name = e.getHeader("Event-Name")
  38. puts "EVENT: #{name}"
  39. break if name == "SERVER_DISCONNECTED"
  40. if name == "DTMF"
  41. digit = e.getHeader("DTMF-DIGIT")
  42. duration = e.getHeader("DTMF-DURATION")
  43. puts "DTMF DIGIT #{digit} (#{duration})"
  44. if digit == "5"
  45. @con.execute("log", "1, WHA HA HA. User pressed 5")
  46. elsif digit == "8"
  47. # TODO: close connection without hangup in order to proceed in dialplan. How?
  48. elsif digit == "9"
  49. @con.execute("transfer", "99355151")
  50. end
  51. end
  52. end
  53. end
  54. puts "Connection closed"
  55. end
  56. Process.detach(pid)
  57. end