Lua Database agent login example
About
Simple agent login script using a PostgreSQL database.
Script db_agent_login.lua
--[[
db_agent_login.lua - Simple agent login script using a PostgreSQL database.
Lua Application for FreeSWITCH::mod_lua.
by Daniel Swarbrick (pressureman @ #freeswitch)
Use/modify/distribute freely.
Disclaimer: Use at your own risk. No implied warranties or help if/when stuff blows up.
]]
require "luasql.postgres"
function myHangupHook(s, status, arg)
freeswitch.consoleLog("NOTICE", "myHangupHook: " .. status .. "\n")
-- close db_conn and terminate
db_conn:close()
error()
end
env = assert(luasql.postgres())
db_conn = assert(env:connect("dbname=mydb user=freeswitch password='t0psecret'"))
session:answer()
session:setHangupHook("myHangupHook")
max_attempts = 3
while max_attempts > 0 do
-- expect 1-6 digits, max_tries=3, timeout=4s, terminator=#
agent_id = session:playAndGetDigits(1, 6, 3, 4000, '#', 'phrase:voicemail_enter_id:#', '', '\\d+|#')
-- did we actually get an agent_id?
if agent_id == "" then
session:sayPhrase("voicemail_goodbye")
session:hangup()
end
-- expect 1-4 digits, max_tries=3, timeout=4s, terminator=#
pin = session:playAndGetDigits(1, 4, 3, 4000, '#', 'phrase:voicemail_enter_pass:#', '', '\\d+|#')
-- did we actually get a pin?
if pin == "" then
session:sayPhrase("voicemail_goodbye")
session:hangup()
end
db_cursor = assert(db_conn:execute(string.format("SELECT pin FROM agent WHERE id = %d", agent_id)))
row = db_cursor:fetch({}, "a")
if pin == row["pin"] then
freeswitch.consoleLog("INFO", string.format("Agent ID %d login successful\n", agent_id))
break
else
session:sayPhrase("voicemail_fail_auth")
end
max_attempts = max_attempts - 1
end
if max_attempts <= 0 then
session:sayPhrase("voicemail_abort")
session:hangup()
end
session:sayPhrase("welcome")
while session:ready() do
-- do something useful here
end
session:hangup()