Lua MythTV alert example

From FreeSWITCH Wiki
Jump to: navigation, search
require("socket")

hosts = {}
hosts['frog.nice.net.nz'] = 6948
hosts['hotpants.nice.net.nz'] = 6948
hosts['snowman.nice.net.nz'] = 6948

cid_text = [[<mythnotify version="1">
  <container name="notify_cid_info">
    <textarea name="notify_cid_num">
      <value> "NAME" (NUM) is calling</value>
    </textarea>
  </container>
</mythnotify>]]

name = session:getVariable("effective_caller_id_name")
number = session:getVariable("effective_caller_id_number")
if not number then
        number = session:getVariable("caller_id_number")
end
if not name then
        name = 'Unknown'
end

freeswitch.consoleLog("info", "name" .. name .. "\n")
freeswitch.consoleLog("info", "number".. number .. "\n")

cid_text = string.gsub(cid_text, 'NAME', name)
cid_text = string.gsub(cid_text, 'NUM', number)

for host, port in pairs(hosts) do
        udp = assert(socket.udp())
        ip = assert(socket.dns.toip(host))
        assert(udp:sendto(cid_text, ip, port))
end

An alternative version that broadcasts to all hosts

I needed something a little different as my frontends use dhcp, and are not always on. Thus, sometimes their dhcp lease has expired, which means they don't have a hostname (I use dnsmasq, btw). This causes the script above to error out with a "host not found" error. I just changed it to broadcast to all hosts on my lan.

require("socket")

broadcast_ip = '255.255.255.255'
-- this assumes all frontends are using the standard port
port = 6948

cid_text = [[<mythnotify version="1">
  <container name="notify_cid_info">
    <textarea name="notify_cid_num">
      <value> "NAME" (NUM) is calling</value>
    </textarea>
  </container>
</mythnotify>]]

name = session:getVariable("effective_caller_id_name")
number = session:getVariable("effective_caller_id_number")
if not number then
        number = session:getVariable("caller_id_number")
end
if not name then
        name = 'Unknown'
end
fnumber = number:sub(1,3).."-"..number:sub(4,6).."-"..number:sub(7,10)

freeswitch.consoleLog("info", "name: " .. name .. "\n")
freeswitch.consoleLog("info", "number: ".. number .. "\n")

cid_text = string.gsub(cid_text, 'NAME', name)
cid_text = string.gsub(cid_text, 'NUM', fnumber)

udp = assert(socket.udp())
assert(udp:setoption("broadcast", true))
assert(udp:sendto(cid_text, broadcast_ip, port))