From 657b2681f44d7d975f581606ec5d5497992edcc7 Mon Sep 17 00:00:00 2001 From: Mark Crane Date: Thu, 8 Nov 2012 20:16:32 +0000 Subject: [PATCH] Initial commit for directory.lua. --- includes/install/scripts/directory.lua | 415 +++++++++++++++++++++++++ 1 file changed, 415 insertions(+) create mode 100644 includes/install/scripts/directory.lua diff --git a/includes/install/scripts/directory.lua b/includes/install/scripts/directory.lua new file mode 100644 index 0000000000..94e1f24129 --- /dev/null +++ b/includes/install/scripts/directory.lua @@ -0,0 +1,415 @@ +-- directory.lua +-- Part of FusionPBX +-- Copyright (C) 2012 Mark J Crane +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions are met: +-- +-- 1. Redistributions of source code must retain the above copyright notice, +-- this list of conditions and the following disclaimer. +-- +-- 2. Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +-- POSSIBILITY OF SUCH DAMAGE. + +--ser the defaults + search_type = "last_name"; + digit_max_length = 3; + timeout_pin = 5000; + timeout_transfer = 5000; + max_tries = 3; + digit_timeout = 5000; + +--include the lua script + scripts_dir = string.sub(debug.getinfo(1).source,2,string.len(debug.getinfo(1).source)-(string.len(argv[0])+1)); + include = assert(loadfile(scripts_dir .. "/resources/config.lua")); + include(); + +--connect to the database + --ODBC - data source name + if (dsn_name) then + dbh = freeswitch.Dbh(dsn_name,dsn_username,dsn_password); + end + --FreeSWITCH core db handler + if (db_type == "sqlite") then + dbh = freeswitch.Dbh("core:"..db_path.."/"..db_name); + end + +--prepare the api object + api = freeswitch.API(); + +--get the session variables + if ( session:ready() ) then + --answer the session + session:answer(); + + --get the domain name + domain_name = session:getVariable("domain_name"); + + --set the sounds path for the language, dialect and voice + default_language = session:getVariable("default_language"); + default_dialect = session:getVariable("default_dialect"); + default_voice = session:getVariable("default_voice"); + if (not default_language) then default_language = 'en'; end + if (not default_dialect) then default_dialect = 'us'; end + if (not default_voice) then default_voice = 'callie'; end + end + +--get the domain_uuid + if (domain_name ~= nil) then + sql = "SELECT domain_uuid FROM v_domains "; + sql = sql .. "WHERE domain_name = '" .. domain_name .."' "; + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[conference] SQL: " .. sql .. "\n"); + end + status = dbh:query(sql, function(rows) + domain_uuid = string.lower(rows["domain_uuid"]); + end); + end + +--define explode + function explode ( seperator, str ) + local pos, arr = 0, {} + for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found + table.insert( arr, string.sub( str, pos, st-1 ) ) -- attach chars left of current divider + pos = sp + 1 -- jump past current divider + end + table.insert( arr, string.sub( str, pos ) ) -- attach chars right of last divider + return arr + end + +--define a function to convert dialpad letters to numbers + function dialpad_to_digit(letter) + letter = string.lower(letter); + if (letter == "a" or letter == "b" or letter == "c") then + digit = "2"; + elseif (letter == "d" or letter == "e" or letter == "f") then + digit = "3"; + elseif (letter == "g" or letter == "h" or letter == "i") then + digit = "4"; + elseif (letter == "j" or letter == "k" or letter == "l") then + digit = "5"; + elseif (letter == "m" or letter == "n" or letter == "o") then + digit = "6"; + elseif (letter == "p" or letter == "q" or letter == "r" or letter == "s") then + digit = "7"; + elseif (letter == "t" or letter == "u" or letter == "v") then + digit = "8"; + elseif (letter == "w" or letter == "x" or letter == "y" or letter == "z") then + digit = "9"; + end + return digit; + end + --print(dialpad_to_digit("m")); + +--define table_count + function table_count(T) + local count = 0 + for _ in pairs(T) do count = count + 1 end + return count + end + +--define trim + function trim (s) + return (string.gsub(s, "^%s*(.-)%s*$", "%1")) + end + +--define the call back function + function dtmf_callback(s, type, obj, arg) + if (arg) then + --io.write("type: " .. type .. "\n" .. "arg: " .. arg .. "\n"); + else + --io.write("type: " .. type .. "\n"); + end + if (type == "dtmf") then + --io.write("digit: [" .. obj['digit'] .. "]\nduration: [" .. obj['duration'] .. "]\n"); + --console_log( "info", "digit: "..obj['digit'].."\n" ); + if ( obj['digit'] == "#" ) then + --console_log( "info", "detected pound sign.\n" ); + exit = true; + return( false ); + end + if ( obj['digit'] == "*" ) then + --console_log( "info", "detected pound sign.\n" ); + exit = true; + return( false ); + end + if (dtmf_digits) then + dtmf_digits = dtmf_digits .. obj['digit']; + else + dtmf_digits = obj['digit']; + end + if ( string.len(dtmf_digits) >= digit_max_length ) then + exit = true; + return( false ); + end + return( true ); + else + --io.write(obj:serialize("xml")); + end + end + +--define prompt_select_entry function + function prompt_select_entry() + dtmf_digits = ""; + digit_timeout = "500"; + max_digits = 1; + max_tries = 1; + dtmf_digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-to_select_entry.wav", "", "\\d+"); + if (string.len(dtmf_digits) == 0) then + dtmf_digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/voicemail/vm-press.wav", "", "\\d+"); + end + if (string.len(dtmf_digits) == 0) then + digit_timeout = "3000"; + dtmf_digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/digits/1.wav", "", "\\d+"); + end + return dtmf_digits; + end + +--define prompt_select_entry function + function prompt_for_name(search_type) + --dtmf_digits = ""; + min_digits=0; max_digits=3; max_tries = 1; digit_timeout = "500"; + --if (search_type == "last_name") then + --dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", "phrase:directory_intro:last_name:en", "", "\\d+"); + --dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-enter_person.wav", "", "\\d+"); + --if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + --end + --if (string.len(dtmf_digits) == 0) then + dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-enter_person_first_or_last.wav", "", "\\d+"); + if (string.len(dtmf_digits) > 0) then + dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + end + --end + --if (string.len(dtmf_digits) == 0) then + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-last_name.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + --end + --if (string.len(dtmf_digits) == 0) then + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-to_search_by.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + --end + --if (string.len(dtmf_digits) == 0) then + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-first_name.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + --end + --end + --if (search_type == "first_name") then + -- --dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", "phrase:directory_intro:first_name:en", "", "\\d+"); + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-enter_person.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + -- if (string.len(dtmf_digits) == 0) then + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-first_name.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + -- end + -- if (string.len(dtmf_digits) == 0) then + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-to_search_by.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + -- end + -- if (string.len(dtmf_digits) == 0) then + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-last_name.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + -- end + --end + --if (string.len(dtmf_digits) == 0) then + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/voicemail/vm-press.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + --end + --if (string.len(dtmf_digits) == 0) then + -- digit_timeout = "3000"; + -- dtmf_digits = session:playAndGetDigits(min_digits, 1, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/digits/1.wav", "", "\\d+"); + -- if (string.len(dtmf_digits) > 0) then + -- dtmf_digits = dtmf_digits .. session:getDigits(max_digits-1, "#", 5000); + -- end + --end + return dtmf_digits; + end + +--define the directory_search function +function directory_search(search_type) + + --clear values + dtmf_search = 0; + dtmf_digits = ''; + + --get the digits for the name + dtmf_digits = prompt_for_name(search_type); + if (string.len(dtmf_digits) > 0) then + dtmf_digits = prompt_for_name(search_type); + end + dtmf_search = dtmf_digits; + freeswitch.consoleLog("notice", "[directory] dtmf_digits: " .. dtmf_digits .. "\n"); + + --toggle the search type + if (dtmf_search == "1") then + --console_log( "info", "press 1 detected: " .. dtmf_digits .. "\n" ); + --console_log( "info", "press 1 detected: " .. search_type .. "\n" ); + if (search_type == "last_name") then + --console_log( "info", "press 1 detected last_name: " .. search_type .. "\n" ); + search_type = "first_name"; + else + --console_log( "info", "press 1 detected first_name: " .. search_type .. "\n" ); + search_type = "last_name"; + end + dtmf_search = ""; + dtmf_digits = ""; + directory_search(search_type); + --return; + end + + --show the dtmf digits + freeswitch.consoleLog("notice", "[directory] first 3 letters of first or last name: " .. dtmf_digits .. "\n"); + + --notes + --session:execute("say", "en name_spelled pronounced mark"); + -- + --session:execute("say", "en number iterated 12345"); + --session:execute("say", "en number pronounced 1001"); + --session:execute("say", "en short_date_time pronounced [timestamp]"); + --session:execute("say", "en CURRENT_TIME pronounced CURRENT_TIME"); + --session:execute("say", "en CURRENT_DATE pronounced CURRENT_DATE"); + --session:execute("say", "en CURRENT_DATE_TIME pronounced CURRENT_DATE_TIME"); + + --get the extensions from the database + sql = "SELECT * FROM v_extensions WHERE domain_uuid = '" .. domain_uuid .. "' AND enabled = 'true' AND (directory_visible is null or directory_visible = 'true'); "; + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[directory] SQL: " .. sql .. "\n"); + end + x = 1; + directory = {} + dbh:query(sql, function(row) + --show all key value pairs + --for key, val in pairs(row) do + -- freeswitch.consoleLog("notice", "[directory] Key: " .. key .. " Value: " .. val .. "\n"); + --end + --add the entire row to the directory table array + --directory[x] = row; + --variables + effective_caller_id_name = row.effective_caller_id_name; + if (row.directory_full_name) then + name = row.directory_full_name; + else + if (row.directory_full_name) then + name = row.effective_caller_id_name; + end + end + if (name) then + name_table = explode(" ",name); + first_name = name_table[1]; + last_name = name_table[2]; + if (first_name) then + if (string.len(first_name) > 0) then + --freeswitch.consoleLog("notice", "[directory] first_name: --" .. first_name .. "--\n"); + first_name_digits = dialpad_to_digit(string.sub(first_name, 1, 1))..dialpad_to_digit(string.sub(first_name, 2, 2))..dialpad_to_digit(string.sub(first_name, 3, 3)); + end + end + if (last_name) then + if (string.len(last_name) > 0) then + --freeswitch.consoleLog("notice", "[directory] last_name: --" .. last_name .. "--\n"); + last_name_digits = dialpad_to_digit(string.sub(last_name, 1, 1))..dialpad_to_digit(string.sub(last_name, 2, 2))..dialpad_to_digit(string.sub(last_name, 3, 3)); + end + end + + end + --add the row to the array + table.insert(directory, {extension=row.extension,context=row.user_context,first_name=name_table[1],last_name=name_table[2],first_name_digits=first_name_digits,last_name_digits=last_name_digits,directory_exten_visible=row.directory_exten_visible}); + + --increment x + x = x + 1; + end); + + --show the results + for key,row in ipairs(directory) do + if (row.first_name and row.last_name) then + freeswitch.consoleLog("notice", "[directory] mmm3 ext: " .. row.extension .. " context " .. row.context .. " name " .. row.first_name .. " "..row.first_name_digits.." ".. row.last_name .. " "..row.last_name_digits.." "..row.directory_exten_visible.."\n"); + else + freeswitch.consoleLog("notice", "[directory] mmm3 ext: " .. row.extension .. " context " .. row.context .. "\n"); + end + end + + --session:execute("set", "tts_engine=flite"); + --session:execute("set", "tts_voice=rms"); --rms --kal --awb --slt + --session:execute("set", "playback_terminators=#"); + --session:speak("flite","kal","Thanks for.. calling"); + + search_dtmf_digits = dtmf_digits; + for key,row in pairs(directory) do + + --if (row.first_name and row.last_name) then + -- freeswitch.consoleLog("notice", "[directory] mmm3 ext: " .. row.extension .. " context " .. row.context .. " name " .. row.first_name .. " "..row.first_name_digits.." ".. row.last_name .. " "..row.last_name_digits.." "..row.directory_exten_visible.."\n"); + --else + -- freeswitch.consoleLog("notice", "[directory] mmm3 ext: " .. row.extension .. " context " .. row.context .. "\n"); + --end + + if (search_dtmf_digits == row.last_name_digits) or (search_dtmf_digits == row.first_name_digits) then + + --say first name and last name is at extension 1001 + --session:execute("speak", row.first_name); + --session:execute("speak", row.last_name); + if (row.first_name and row.last_name) then + --announce the first and last names + --session:execute("sleep", "2000"); + session:execute("say", "en name_spelled pronounced "..row.first_name); + --session:execute("sleep", "500"); + session:execute("say", "en name_spelled pronounced "..row.last_name); + + --announce the extension number + if (row.directory_exten_visible == "false") then + --invisible extension number + else + session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/directory/dir-at_extension.wav"); + session:execute("say", "en number pronounced "..row.extension); + end + + --select this entry press 1 + dtmf_digits = prompt_select_entry(); + if (string.len(dtmf_digits) == 0) then + dtmf_digits = prompt_select_entry(); + end + + --if 1 is pressed then transfer the call + if (dtmf_digits == "1") then + --console_log( "info", "directory: call transfered to: " .. row.extension .. "\n" ); + session:execute("transfer", row.extension.." XML "..row.context); + end + end + end + end +end + +if ( session:ready() ) then + --call the directory search function + directory_search(search_type); + + --end the call + --session:hangup("NORMAL_CLEARING"); +end