Skip to main content

Ruby ESL

Click here to expand Table of Contents

Ruby Examples

ESL in Inbound mode.

require 'ESL'

con = ESL::ESLconnection.new('127.0.0.1', '8021', 'ClueCon')
esl = con.sendRecv('api sofia status')
puts esl.getBody

ESL in Outbound mode in sync operation.

require 'socket'
require 'ESL'

server = TCPServer.new(8084)
loop do
con = server.accept
fd = con.to_i
esl = ESL::ESLconnection.new(fd)
esl.execute('answer')
esl.execute('playback', '/usr/local/freeswitch/sounds/music/8000/suite-espanola-op-47-leyenda.wav')
esl.execute('hangup')
end

ESL in Outbound async full mode (<action application="socket" data="localhost:8086 async full"/>). It forks a process of every connection in order to support simultaneous sessions.

#!/usr/bin/ruby

require "ESL"
require 'socket'
include Socket::Constants
bind_address = "127.0.0.1"
bind_port = 8086

def time_now
Time.now.strftime("%Y-%m-%d %H:%M:%S")
end

socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(bind_port, bind_address)
socket.bind(sockaddr)
socket.listen(5)
puts "Listening for connections on #{bind_address}:#{bind_port}"
loop do
client_socket, client_sockaddr = socket.accept #_nonblock
pid = fork do
@con = ESL::ESLconnection.new(client_socket.fileno)
info = @con.getInfo
uuid = info.getHeader("UNIQUE-ID")
@con.sendRecv("myevents")
@con.sendRecv("divert_events on")

puts "#{time_now} [#{uuid}] Call to [#{info.getHeader("Caller-Destination-Number")}]"
@con.execute("log", "1, Wee-wa-wee-wa")
@con.execute("info", "")
@con.execute("answer", "")
@con.execute("playback", "/usr/local/freeswitch/sounds/music/8000/suite-espanola-op-47-leyenda.wav")

while @con.connected
e = @con.recvEvent

if e
name = e.getHeader("Event-Name")
puts "EVENT: #{name}"
break if name == "SERVER_DISCONNECTED"
if name == "DTMF"
digit = e.getHeader("DTMF-DIGIT")
duration = e.getHeader("DTMF-DURATION")
puts "DTMF DIGIT #{digit} (#{duration})"
if digit == "5"
@con.execute("log", "1, WHA HA HA. User pressed 5")
elsif digit == "8"
# TODO: close connection without hangup in order to proceed in dialplan. How?
elsif digit == "9"
@con.execute("transfer", "99355151")
end
end

end
end
puts "Connection closed"
end

Process.detach(pid)
end