From 3c363517627bebc76be45504d85c9e52f4a7756c Mon Sep 17 00:00:00 2001 From: Mark Crane Date: Wed, 1 Apr 2015 07:56:56 +0000 Subject: [PATCH] Add an option to save the greeting to the database. --- .../resources/functions/record_greeting.lua | 101 ++++++++++++++++-- 1 file changed, 91 insertions(+), 10 deletions(-) diff --git a/resources/install/scripts/app/voicemail/resources/functions/record_greeting.lua b/resources/install/scripts/app/voicemail/resources/functions/record_greeting.lua index 940fc98f6d..59cd303fe0 100644 --- a/resources/install/scripts/app/voicemail/resources/functions/record_greeting.lua +++ b/resources/install/scripts/app/voicemail/resources/functions/record_greeting.lua @@ -52,15 +52,96 @@ macro(session, "record_greeting", 1, 100, ''); end - --record the greeting - if (session:ready()) then - max_len_seconds = 30; - silence_threshold = 30; - silence_seconds = 5; - mkdir(voicemail_dir.."/"..voicemail_id); - -- syntax is session:recordFile(file_name, max_len_secs, silence_threshold, silence_secs) - result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/greeting_"..greeting_id..".wav", max_len_seconds, silence_threshold, silence_seconds); - --session:execute("record", voicemail_dir.."/"..uuid.." 180 200"); + + --store the voicemail greeting + if (storage_type == "base64") then + --prepare to record the greeting + if (session:ready()) then + max_len_seconds = 30; + silence_threshold = 30; + silence_seconds = 5; + mkdir(voicemail_dir.."/"..voicemail_id); + -- syntax is session:recordFile(file_name, max_len_secs, silence_threshold, silence_secs) + result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/greeting_"..greeting_id..".wav", max_len_seconds, silence_threshold, silence_seconds); + --session:execute("record", voicemail_dir.."/"..uuid.." 180 200"); + end + + --include the base64 function + dofile(scripts_dir.."/resources/functions/base64.lua"); + + --show the storage type + freeswitch.consoleLog("notice", "[voicemail] ".. storage_type .. "\n"); + + --base64 encode the file + local f = io.open(voicemail_dir.."/"..voicemail_id.."/greeting_"..greeting_id..".wav", "rb"); + local file_content = f:read("*all"); + f:close(); + greeting_base64 = base64.encode(file_content); + + --delete the previous recording + sql = "delete from v_voicemail_greetings "; + sql = sql .. "where domain_uuid = '".. domain_uuid .. "' "; + sql = sql .. "and voicemail_id = '".. voicemail_id .."'"; + sql = sql .. "and greeting_id = '".. greeting_id .."'"; + dbh:query(sql); + + --get a new uuid + voicemail_greeting_uuid = api:execute("create_uuid"); + + --save the message to the voicemail messages + local array = {} + table.insert(array, "INSERT INTO v_voicemail_greetings "); + table.insert(array, "("); + table.insert(array, "voicemail_greeting_uuid, "); + table.insert(array, "domain_uuid, "); + table.insert(array, "voicemail_id, "); + table.insert(array, "greeting_id, "); + if (storage_type == "base64") then + table.insert(array, "greeting_base64, "); + end + table.insert(array, "greeting_name "); + table.insert(array, ") "); + table.insert(array, "VALUES "); + table.insert(array, "( "); + table.insert(array, "'"..voicemail_greeting_uuid.."', "); + table.insert(array, "'"..domain_uuid.."', "); + table.insert(array, "'"..voicemail_id.."', "); + table.insert(array, "'"..greeting_id.."', "); + if (storage_type == "base64") then + table.insert(array, "'"..greeting_base64.."', "); + end + table.insert(array, "'greeting_"..greeting_id..".wav' "); + table.insert(array, ") "); + sql = table.concat(array, "\n"); + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n"); + end + if (storage_type == "base64") then + array = explode("://", database["system"]); + local luasql = require "luasql.postgres"; + local env = assert (luasql.postgres()); + local db = env:connect(array[2]); + res, serr = db:execute(sql); + db:close(); + env:close(); + else + dbh:query(sql); + end + elseif (storage_type == "http_cache") then + freeswitch.consoleLog("notice", "[voicemail] ".. storage_type .. " ".. storage_path .."\n"); + session:execute("record", storage_path .."/"..recording_name); + else + --prepare to record the greeting + if (session:ready()) then + max_len_seconds = 30; + silence_threshold = 30; + silence_seconds = 5; + mkdir(voicemail_dir.."/"..voicemail_id); + -- syntax is session:recordFile(file_name, max_len_secs, silence_threshold, silence_secs) + result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/greeting_"..greeting_id..".wav", max_len_seconds, silence_threshold, silence_seconds); + --session:execute("record", voicemail_dir.."/"..uuid.." 180 200"); + end + end --play the greeting @@ -93,4 +174,4 @@ end end end - end + end \ No newline at end of file