forked from thehunmonkgroup/jester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.lua
More file actions
108 lines (92 loc) · 3.27 KB
/
socket.lua
File metadata and controls
108 lines (92 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
--[[
This is a socket listener for running Jester sequences via the FreeSWITCH
event system. It is experimental, use at your own risk.
To start listener, set it as a startup script in
conf/autoload_configs/lua.conf.xml:
<param name="startup-script" value="jester/socket.lua [server] [port] [password]"/>
Or via luarun:
luarun jester/socket.lua [server] [port] [password]
It listens for CUSTOM events of subclass 'jester::socket'.
Firing an event for the listener looks something like this:
sendevent CUSTOM
Event-Subclass: jester::socket
Jester-Profile: socket
Jester-Sequence: mysequence
Jester-Sequence-Args: arg1,arg2
Jester-Sequence:
Required. The name of the sequence to run.
Jester-Profile:
Optional. The profile to run the sequence under. Defaults to 'socket'.
Jester-Sequence-Args:
Optional. Arguments to pass to the sequence, in the same form that normal
sequence arguments are passed.
To exit the listener, you can send this event:
sendevent CUSTOM
Event-Subclass: jester::socket
Jester-Socket-Exit: yes
WARNING: there is no session object available with this approach, so be
careful not to use actions that need a session (play, record, get_digits,
etc.) or the listener will crash! The sequences should be more along the
lines of performing database/file manipulation, logging to file, etc.
]]
require "jester.core"
require "jester.conf"
require "jester.debug"
require "ESL"
--[[
Logs socket information to the FreeSWITCH console.
]]
function jester.socket_log(message)
jester.log(message, "JESTER SOCKET")
end
--[[
Connect to FreeSWITCH.
]]
function jester.socket_connect()
-- Login information for the event socket.
local host = argv[1] or "localhost"
local port = argv[2] or "8021"
local password = argv[3] or "ClueCon"
jester.sock = assert(ESL.ESLconnection(host, port, password))
end
-- This is always true on a socket connection, setting it here allows early
-- logging.
jester.is_freeswitch = true
jester.socket_connect()
if jester.sock and jester.sock:connected() then
jester.socket_log("connected")
-- Subscribe only to Jester socket events.
jester.sock:events("plain", "CUSTOM jester::socket")
jester.continue_socket = true
while jester.sock and jester.sock:connected() and jester.continue_socket do
local event = jester.sock:recvEvent()
-- Provide a way to turn exit the listener.
if event:getHeader("Jester-Socket-Exit") then
jester.continue_socket = false
jester.socket_log("received disconnect command")
else
local sequence = event:getHeader("Jester-Sequence")
if sequence then
local profile = event:getHeader("Jester-Profile") or "socket"
local sequence_args = event:getHeader("Jester-Sequence-Args") or ""
local args = {
profile,
sequence,
}
if sequence_args then
table.insert(args, sequence_args)
end
jester.socket_log(string.format([[received Jester event: %s]], table.concat(args, " ")))
jester.bootstrap(args)
if jester.bootstrapped then
-- Main loop.
jester.main()
end
end
end
end
jester.socket_log("disconnecting")
if jester.sock and jester.sock:connected() then
jester.sock:disconnect()
end
end